Jun 21, 2017

Database Migrations in Flyway

The concept of data migrations has been around for a long time and in the past, they have been projects that are separated from the main project, mostly with SQL script to change the tables, update data, etc. But application frameworks such as Ruby on Rail and Django have had them for a long time. In the JVM world, Flyway appears to be the leader in application-integrated database migrations.

Flyway integrates with Maven and Gradle, which makes using it turn out to be like a command-line tool. Once you get it setup in Gradle, for instance, it’s as easy as running gradle flywayMigrate -i and your database it updated. Flyway also has Spring Boot integration which runs all the migrations on application startup. This is the easiest method and is what I have using in my sample application.

Flyway keeps track of what migrations have run in the database in a table called schema_version. If you run the sample application, this is what the schema_version table looks like:

This table shows two migrations – one with a description of “Create Tables” and another with “insert data”. They are derived from the name of the file – the “script name” (which you can see in the script column). Flyway has pretty strict conventions about naming your migrations, which helps it parse and put information into this table. The names of these two files are V1__Create_tables.sql and V2__insert_data.java. Most of this naming convention is configurable but I’d argue that it’s a lot of work to roll your own.

The V1__Create_tables.sql file is just a common SQL file with multiple statements. Flyway runs these statements in transactions so if one command fails, it backs them all out and you are not left with the database in a bad state. I think it’s important to note that there is nothing special about these SQL statements. There are no specific Flyway code or anything. It just runs plain SQL.

The V2__insert_data.java file is different. This is a Java-based migration. I used this to parse a CSV file and put it into the database (which I think is a common task). This uses Spring’s JDBC Template, which makes it really easy to do inserts. But, if you aren’t using Spring, you can use the raw connection and get PreparedStatements from that connection, but JdbcTemplate is much easier to work with. One thing I don’t care for in the Java migrations is the class name. Since the file name is required to be V2__insert_data.java, then the class is named V2__insert_data. I think this a poor name for the class. I wish you could set these with properties inside the class instead of just using the filename.

Both of these migrations are Versioned migrations, meaning they are only designed to run once. Flyway also supported Repeatable migrations, which run every time those files change and the Flyway task is run. It does this by checking the checksum on the file. Going back to the schema_version table, note that the SQL migration has a checksum and the Java migration does not. By default, Java migrations do not have checksums but you can generate it by implementing MigrationChecksumProvider in your class and generating it your own. That seems dangerous to me, so use repeatable Java migrations with caution.

So now you have an overview of using Flyway in your Java projects. Feel free to checkout my sample application and play around with it. I think it’s a simple application that can solve a lot of problems.


About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Database Migrations in Flyway

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]