Let's start by creating build.gradle that integrates and configures Flyway to connect to H2:
buildscript {
dependencies {
classpath 'com.h2database:h2:1.4.191'
}
}
plugins {
id "org.flywaydb.flyway" version "4.2.0"
}
flyway {
url = 'jdbc:h2:file:./target/foobar'
user = '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:
> gradle flywayMigrate -i
If all went well, you should see the following output:
Creating Metadata table: "PUBLIC"."schema_version" Current version of schema "PUBLIC": << Empty Schema >> Migrating schema "PUBLIC" to version 1 - Create person table Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.062s).
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:
> gradle flywayMigrate -i
We now get:
Current version of schema "PUBLIC": 1 Migrating schema "PUBLIC" to version 2 - Add people Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.090s).
In this brief tutorial we saw how to
These migrations were then successfully found and executed.