First Steps: Command-line

Prerequisites

  • Java 6+

Downloading and extracting Flyway and H2

We start by downloading the Flyway Command-line Distribution and extracting it.

Let's jump into our new directory:

> cd flyway-3.0

We then download the H2 jar and place it in the /jars subdirectory.

Configuring Flyway

We can now configure Flyway by editing /conf/flyway.properties to make it look like this:

flyway.url=jdbc:h2:file:./foobardb
flyway.user=SA

Creating the first migration

Now we can create a first migration in the /sql 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:

flyway-3.0> flyway migrate

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
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 /sql 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:

flyway-3.0> flyway migrate

We now get:

Current version of schema "PUBLIC": 1
Migrating schema "PUBLIC" to version 2
Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.090s).

Summary

In this brief tutorial we saw how to

  • install the Flyway Command-line tool
  • 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