Quickstart - API

This brief tutorial will teach how to get up and running with the Flyway API. It will take you through the steps on how to configure it and how to write and execute your first few database migrations.

This tutorial should take you about 5 minutes to complete.

Prerequisites

  • Java 17
  • Maven 3.x

Creating the project

We're going to create our project using the Maven Archetype Plugin by issuing the following command:

> mvn archetype:generate -B ^
    -DarchetypeGroupId=org.apache.maven.archetypes ^
    -DarchetypeArtifactId=maven-archetype-quickstart ^
    -DarchetypeVersion=1.1 ^
    -DgroupId=foo ^
    -DartifactId=bar ^
    -Dversion=1.0-SNAPSHOT ^
    -Dpackage=foobar

We are now ready to get started. Let's jump into our project:

> cd bar

Adding the dependencies

Let's add Flyway and H2 to our new pom.xml:

<project xmlns="...">;
    <dependencies>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.2.224</version>
        </dependency>
        ...
    </dependencies>
    ...
</project>

Integrating Flyway

Now it's time to integrate Flyway into src/main/java/foobar/App.java and point it to our database:

package foobar;

import org.flywaydb.core.Flyway;

public class App {
    public static void main(String[] args) {
        // Create the Flyway instance and point it to the database
        Flyway flyway = Flyway.configure().dataSource("jdbc:h2:file:./target/foobar", "sa", null).load();

        // Start the migration
        flyway.migrate();
    }
}

Creating the first migration

We create the migration directory src/main/resources/db/migration.

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

Executing our program

It's now time to execute our program by issuing this command:

bar> mvn package exec:java -Dexec.mainClass=foobar.App

If all went well, you should see the following output (timestamps omitted):

INFO: Creating schema history table: "PUBLIC"."flyway_schema_history"
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.062s).

Adding a second migration

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:

bar> mvn package exec:java -Dexec.mainClass=foobar.App

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

Summary

In this brief tutorial we saw how to

  • integrate Flyway 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.


Didn't find what you were looking for?