We're going to create the directory structure for our project by issuing the following commands:
> mkdir foobar > mkdir "foobar/libs" > mkdir "foobar/migrations"
We are now ready to get started. Let's jump into our project:
> cd foobar
We start by downloading the H2 jar and we place it in our project as libs/h2.jar.
After which we download the Flyway Ant Distribution and also extract its jar files into libs.
The libs directory should now contain 3 files: h2.jar, flyway-core-4.2.0.jar and flyway-ant-4.2.0.jar.
Let's start by creating a build.xml file that'll correctly load and configure Flyway:
<project name="foobar" default="migrate-db" xmlns:flyway="antlib:org.flywaydb.ant">
<target name="migrate-db">
<taskdef uri="antlib:org.flywaydb.ant" resource="org/flywaydb/ant/antlib.xml">
<classpath>
<pathelement location="libs/flyway-core-4.2.0.jar"/>
<pathelement location="libs/flyway-ant-4.2.0.jar"/>
</classpath>
</taskdef>
<path id="flyway.classpath">
<fileset dir="./libs" includes="h2.jar"/>
</path>
<property name="flyway.locations" value="filesystem:./migrations"/>
<flyway:migrate url="jdbc:h2:file:./foobardb" user="SA"/>
</target>
</project>
Now we can create a first migration in the migrations directory called
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:
foobar> ant
If all went well, you should see the following output:
[flyway:migrate] Creating Metadata table: "PUBLIC"."schema_version" [flyway:migrate] Current version of schema "PUBLIC": << Empty Schema >> [flyway:migrate] Migrating schema "PUBLIC" to version 1 - Create person table [flyway:migrate] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.062s).
If we now add a second migration to the migrations directory called 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:
foobar> ant
We now get:
[flyway:migrate] Current version of schema "PUBLIC": 1 [flyway:migrate] Migrating schema "PUBLIC" to version 2 - Add people [flyway:migrate] 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.