Want to deploy your JVM, Node.js and Go apps effortlessly to AWS? Try our service Boxfuse

First Steps: Ant

Prerequisites

  • Java 6+
  • A working Ant install

Creating the project

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

Downloading and extracting Flyway and H2

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.

Integrating Flyway

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>

Creating the first migration

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
);

Migrating the database

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).

Adding a second migration

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).

Summary

In this brief tutorial we saw how to

  • integrate the Flyway Ant tasks into a project
  • configure them so they can talk to our database
  • write our first couple of migrations

These migrations were then successfully found and executed.

Read the documentation