Let's start by creating project/plugins.sbt
that integrates the Flyway plugin into our project:
addSbtPlugin("org.flywaydb" % "flyway-sbt" % "4.2.0") resolvers += "Flyway" at "https://flywaydb.org/repo"
Now we can create build.sbt
that configures Flyway to connect to H2:
libraryDependencies ++= Seq( "com.h2database" % "h2" % "1.4.191" ) flywayUrl := "jdbc:h2:file:./target/foobar" flywayUser := "SA"
We create a first migration called src/main/resources/db/migration/V1__Create_person_table.sql
:
create table PERSON ( ID int not null, NAME varchar(100) not null );
It's now time to execute Flyway to migrate our database:
> sbt flywayMigrate
If all went well, you should see the following output:
[info] Creating Metadata table: "PUBLIC"."schema_version" [info] Current version of schema "PUBLIC": << Empty Schema >> [info] Migrating schema "PUBLIC" to version 1 - Create person table [info] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.050s).
If we now add a second migration called src/main/resources/db/migration/V2__Add_people.sql
:
insert into PERSON (ID, NAME) values (1, 'Axel'); insert into PERSON (ID, NAME) values (2, 'Mr. Foo'); insert into PERSON (ID, NAME) values (3, 'Ms. Bar');
and execute it by issuing:
> sbt flywayMigrate
We now get:
[info] Current version of schema "PUBLIC": 1 [info] Migrating schema "PUBLIC" to version 2 - Add people [info] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.037s).
In this brief tutorial we saw how to
These migrations were then successfully found and executed.