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

First Steps: SBT

Prerequisites

  • Java 6+
  • A working SBT 0.13.5 or newer install

Setting up the plugins file

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"

Setting up the build file

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"

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:

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

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:

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

Summary

In this brief tutorial we saw how to

  • integrate the Flyway SBT 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