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

First Steps: Gradle

Prerequisites

  • Java 6+
  • A working Gradle install

Setting up the build file

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'
}

Creating the first migration

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

Migrating the database

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

Adding a second migration

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

Summary

In this brief tutorial we saw how to

  • integrate the Flyway Gradle plugin into a project
  • configure it so it can talk to our database
  • write our first couple of migrations

These migrations were then successfully found and executed.

Read the documentation