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.
Binary | Source |
---|---|
flyway-core-4.2.0.jar | flyway-core-4.2.0-sources.jar |
<dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>4.2.0</version> </dependency>
compile "org.flywaydb:flyway-core:4.2.0"
<dependency org="org.flywaydb" name="flyway-core" rev="4.2.0"/>
"org.flywaydb" % "flyway-core" % "4.2.0"
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.
Flyway is super easy to use programmatically:
import org.flywaydb.core.Flyway; ... Flyway flyway = new Flyway(); flyway.setDataSource(...); flyway.migrate(); // Start the rest of the application (incl. Hibernate) ...
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
.
1. Add the necessary dependencies to build.gradle
:
dependencies { // Your other dependencies // ... compile 'org.flywaydb:flyway-core:4.2.0' compile 'org.sqldroid:sqldroid:1.0.3' }
2. 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'] } } // ... }
3. 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 = new Flyway(); flyway.setDataSource(dataSource); flyway.migrate();
As an alternative to the programmatic configuration, here is how you can configure and start Flyway in a typical Spring application:
<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate"> <property name="dataSource" ref="..."/> ... </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>