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

API

Flyway brings the largest benefits when integrated within an application. By integrating Flyway you can ensure that the application and its database will always be compatible, with no manual intervention required. Flyway checks the version of the database and applies new migrations automatically before the rest of the application starts. This is important, because the database must first be migrated to a state the rest of the code can work with.

Supported Java Versions

Download

Maven
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.2.0</version>
</dependency>
Gradle
compile "org.flywaydb:flyway-core:5.2.0"
Binary flyway-core-5.2.0.jar
Source flyway-core-5.2.0-sources.jar
Installation Start by downloading and extracting the Flyway Trial Edition zip. Then execute the installToLocalMavenRepo script in the flyway-5.2.0 directory. This installs all necessary artifacts in your local Maven repository, making it possible to use them in Maven and Gradle builds.
Maven
<dependencies>
    <dependency>
        <groupId>org.flywaydb.trial</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.2.0</version>
    </dependency>
    ...
</dependencies>
Gradle
repositories {
    mavenLocal()
}

compile "org.flywaydb.trial:flyway-core:5.2.0"
Maven pom.xml
<repositories>
    <repository>
        <id>flyway-repo</id>
        <url>https://repo.flywaydb.org/repo</url>
    </repository>
    ...
</repositories>

<dependencies>
    <dependency>
        <groupId>org.flywaydb.pro</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.2.0</version>
    </dependency>
    ...
</dependencies>
settings.xml
<settings>
    <servers>
        <server>
            <id>flyway-repo</id>
            <username>your-flyway-pro-license-key</username>
            <password>flyway</password>
        </server>
    </servers>
    ...
</settings>
Gradle
repositories {
    maven {
        url "https://repo.flywaydb.org/repo"
        credentials {
            username 'your-flyway-pro-license-key'
            password 'flyway'
        }
    }
}

dependencies {
    compile "org.flywaydb.pro:flyway-core:5.2.0"
}
Maven pom.xml
<repositories>
    <repository>
        <id>flyway-repo</id>
        <url>https://repo.flywaydb.org/repo</url>
    </repository>
    ...
</repositories>

<dependencies>
    <dependency>
        <groupId>org.flywaydb.enterprise</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.2.0</version>
    </dependency>
    ...
</dependencies>
settings.xml
<settings>
    <servers>
        <server>
            <id>flyway-repo</id>
            <username>your-flyway-enterprise-license-key</username>
            <password>flyway</password>
        </server>
    </servers>
    ...
</settings>
Gradle
repositories {
    maven {
        url "https://repo.flywaydb.org/repo"
        credentials {
            username 'your-flyway-enterprise-license-key'
            password 'flyway'
        }
    }
}

dependencies {
    compile "org.flywaydb.enterprise:flyway-core:5.2.0"
}

The Flyway Class

The central piece of Flyway’s database migration infrastructure is the org.flywaydb.core.Flyway class. It is your one-stop shop for working with Flyway programmatically. It serves both as a configuration and a launching point for all of Flyway’s functions.

Programmatic Configuration (Java)

Flyway is super easy to use programmatically:

import org.flywaydb.core.Flyway;

...
Flyway flyway = Flyway.configure().dataSource(url, user, password).load();
flyway.migrate();

// Start the rest of the application (incl. Hibernate)
...
Tip: When running inside a Boxfuse instance (both locally and on AWS), Flyway will automatically use the JDBC url, user and password provided by Boxfuse.

Programmatic Configuration (Android)

In order to use Flyway on Android you have to add flyway-core as well as SQLDroid as dependencies. There are two things to keep in mind with Android: First, you have to load the migrations as assets, not resources and second, you have to let Flyway know your Android context, by calling ContextHolder.setContext().

Add the necessary dependencies to build.gradle:

dependencies {
    // Your other dependencies
    // ...

    compile 'org.flywaydb:flyway-core:5.2.0'
    compile 'org.sqldroid:sqldroid:1.1.0-rc1'
}

Make sure that your migrations are included as assets (notice that assets have to be declared in the project itself and not in a dependency. But you can use reference e.g. ../lib/src/main/resources to use the resources of a lib project)

android {
    // SDK, config, buildTypes, etc
    // ...

    sourceSets {
        // Place your db/migration folder here
        main { assets.srcDirs = ['src/main/assets'] }
    }

    // ...
}

Include the setup in your main activity onCreate or application onCreate:

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.android.ContextHolder;
import org.sqldroid.DroidDataSource;

...
DroidDataSource dataSource = new DroidDataSource(getPackageName(), "...");
ContextHolder.setContext(this);
Flyway flyway = Flyway.configure().dataSource(dataSource).load();
flyway.migrate();

Spring Configuration

As an alternative to the programmatic configuration, here is how you can configure and start Flyway in a classic Spring application using XML bean configuration:

<bean id="flywayConfig" class="org.flywaydb.core.configuration.ClassicConfiguration">
    <property name="dataSource" ref="..."/>
    ...
</bean>

<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <constructor-arg ref="flywayConfig"/>
</bean>

<!-- The rest of the application (incl. Hibernate) -->
<!-- Must be run after Flyway to ensure the database is compatible with the code -->
<bean id="sessionFactory" class="..." depends-on="flyway">
    ...
</bean>

API: Hooks