Flyway is a popular open source database migration framework for Java. It brings structure and confidence to the evolution of your database schema. It is really easy to use, yet powerful and both developer and DBA-friendly. Flyway supports the industry’s most common databases including Oracle, SQL Server, DB2, MySQL, PostgreSQL, HsqlDB, H2 and Derby. Migrations can be written in plain old SQL or Java and can be executed through the API, the Maven plugin, the Ant tasks or the Command-Line tool.

In a Java project where data is persisted in a relational database, our software uses a JDBC driver through which it talks to the database to store and query data. So far, so good.

But on all, but the most trivial projects, this isn’t the whole truth. We usually have to deal with multiple environments (dev, test, production, etc) each containing separate instances of both the software and the database. On the software side of things, this problem has been widely addressed through the use of version control, automated and reproducible builds, continuous integration and in some cases automated deployment. On the database side of things, we usually find a different story. Many projects still rely on manually applied SQL scripts. And sometimes not even that (a quick SQL statement here or there to fix a problem). And soon many questions arise:

What state is the database in on this machine?

Has this script already been applied?

Has the quick fix in production been applied in test afterwards?

How do you set up a new database instance from scratch?

Will our software run against our database in its current state?

More often than not the answer to these questions is: We don't know.

Database migration tools let you regain control of this mess. They allow you to:

Recreate a database from scratch

Make it clear at all times what state a database is in

Review which changes have been applied when and by whom

Migrate in a deterministic way from the current version of the database to a newer one

Be confident that your software will run with the current state of the database

Simple yet Powerful

Flyway is designed and built on the idea that adding database migrations to your project should be as easy as possible. It is kept deliberately small. It strongly embraces convention over configuration and its API is composed of just six main methods: clean, init, migrate, validate, info and repair.

To know which state your database is in, Flyway relies on a special metadata table for all its internal bookkeeping. Flyway creates it automatically when it is run for the first time. After that, every time a migration is applied, its details are stored in that table. Flyway can then query it to know which migrations have been applied, what the current version of the schema is and whether the last migration succeeded or failed.

Flyway works very hard to support your existing infrastructure and to avoid lock-in. By default Flyway works with plain old SQL scripts. Its parser, however, is very sophisticated. It allows you to use both regular SQL statements and statements relying on database-specific extensions such as PL/SQL and T-SQL. This enables you to easily define advanced indexes, stored procedures, materialized views or any other object your database supports. You have the full power of your database available to you and DBAs can easily assist you with their expertise. It also makes it very easy to get started on existing projects. You can dump your schema using your database’s native tools and apply the resulting SQL script as a first migration with virtually no changes.

For more advanced scenarios where SQL may not be the best option anymore, Flyway supports migrations written in Java. This is especially useful when dealing with BLOBs and CLOBs, and for performing advanced data transformations such as smart column splitting (Think splitting the country and area code out of phone numbers for example).

Reliable

Relying on a tool to deal with changes to your database requires trust. Flyway works very hard to achieve and maintain this.

All migrations are run within transactions. On databases with support for DDL transactions (PostgreSQL, DB2, SQL Server and Derby), Flyway will automatically roll back the entire migration when a failure occurs and report an error. On all other databases the migration will be marked as failed in the metadata table and an error will be reported. You will then have to manually revert the changes and run Flyway’s repair command to fix the metadata table. This follows the fail fast rule. It surfaces errors as soon as possible to avoid having them sneak unnoticed through your system.

Flyway also relies on careful locking to orchestrate concurrent migration attempts. This is crucial for application startup in cluster environments.

Works within your environment

Flyway supports a large range of databases and can be run in a variety of ways to best suit your environment.

The most powerful and effective way to run Flyway is on application startup through the API. This allows you to bundle all migrations together with your application and have a single deliverable you can throw at any environment. It will then automatically detect the state of the database and migrate accordingly on startup, ensuring your application code will always work against the database it is currently accessing.

If for whatever reason this is not an option, Flyway supports other ways to run it, all equally powerful, exposing the same commands and configuration options.

For Maven users, there is a Maven plugin. Like Flyway itself, it is of course also available from Maven Central and can be defined like this in the POM:

<plugin>

<groupId>com.googlecode.flyway</groupId>

<artifactId>flyway-maven-plugin</artifactId>

<version>1.8</version>

</plugin>

Ant users have an Antlib available to them. Ivy users can easily reference it by adding this line to their configuration:

<dependency

org="com.googlecode.flyway"name="flyway-ant"rev="1.8"/>

And for those who prefer the command-line or shell scripting, there is a standalone command-line tool available supporting Windows, Linux and OSX.

Flyway supports the industry’s most common databases including Oracle, SQL Server, DB2, MySQL, PostgreSQL, HsqlDB, H2 and Derby. It also works on Google AppEngine with full support for Google Cloud SQL.

Getting started

Flyway is available for download from both Maven Central and the Flyway web site.

In this example we will use Maven as the build tool and H2 as the database.

So far so good. We can now create our first migration following Flyway’s default naming conventions. Flyway will scan the classpath and automatically pick it up. Through the naming convention it will automatically detect both the version and the description of the migration.

File: src/main/resources/db/migration/V1__Initial_Migration.sql

CREATE TABLE test_user (

name VARCHAR(25) NOT NULL,

PRIMARY KEY(name)

);

After running mvn compile flyway:info, we now have our first pending migration

Integrating Flyway directly in your application is very straightforward. It has zero required dependencies. All you need to get started is the flyway-core JAR, the JDBC driver for your database and Java 5+.

This bean can then be depended upon by ORM frameworks like Hibernate to ensure they always have a fully up to date schema before starting up.

<bean

id="sessionFactory"class="..."depends-on="flyway">

...

</bean>

If Spring isn’t your cup of tea, you can also rely on the easy Java API which effectively achieves the same thing.

import

com.googlecode.flyway.core.Flyway;

...

Flyway

flyway =newFlyway();

flyway

.setDataSource(...);

flyway

.migrate();

Additional Flyway Features

Behind this apparent simplicity lies a powerful array of features to deal with a variety of scenarios not discussed here, including:

Validation: Through checksums Flyway can detect if migration scripts have been altered after they have been applied. This can be a source of subtle errors and Flyway helps you guard against it. Checksums are automatically calculated for SQL migrations and can be optionally defined for Java ones.

Multiple schema support: Flyway has built-in support for migrating, validating and cleaning multiple schemas sharing the same lifecycle.

OSGi support: Flyway fully supports the Equinox OSGi container and goes to great length to ensure features like classpath scanning work flawlessly for both SQL and Java migrations.

Placeholder replacement: Placeholder replacement with configurable placeholder markers (default: ${ }) is supported out of the box for SQL migrations.

Multiple locations: Migrations can be loaded from multiple locations on the classpath. You can vary these locations in your configuration according to your environment. This enables you to have a location with test data being used in development, but excluded in production.

Configurable naming: Whether it is the name of the metadata table, the locations to load the migrations from, or the naming convention of the SQL and Java migrations, all are fully configurable to your liking.

Next steps

At http://flywaydb.orgyou will find extensive documentation, a FAQ, downloads, the changelog, the roadmap and more.

You will also find a comparison matrix of the most popular database migration tools available for the Java platform, so you can pick the right tool for the job.

For questions, there is a Flyway tag on StackOverflow available for you to use.