Flyway — Database Migrations made easy

& How not to accidentally Rollback all of your migrations

Sathiya Sarathi
Datawrangler
4 min readMar 24, 2019

--

Flyway — by boxfuse: This is a schema migration tool and it acts more of like version control for your relational databases.

If you are manually executing your SQL scripts or if your administrator is manually executing the SQL scripts, on your production or UAT environment, you definitely need this tool to be set up in all of your environments.

Before we proceed:

Statutory Warning:

Never execute the following command, be it your production or UAT environment:

$ flyway clean # Do not execute this, ever!!!!

Wondering what it does? It rolls back whatever table migrations/changes you have done through flyway, along with their data.
In short, Don’t ever execute this command.

Now that we are done with all the warnings:

Installation:

It is fairly straight forward:

Run the above command in a shell prompt.
Running the above creates a directory called as flyway-x.x.x/
Inside this directory are many other directories of which, the two most import directories are:

  • conf/ — Configuration for each of the databases are kept here as individual conf files
  • SQL/ — SQL migrations are kept under different directories for each of the above configurations

Setting up the Configuration file:

If this is your first time with flyway, I would urge you to go through the configuration file from top to bottom, it’s kinda fun, comical, and scary too. Especially, this part — quote and quote from the default configuration:

# Whether to disabled clean. (default: false)
# This is especially useful for production environments where running clean can be quite a career-limiting move.
flyway.cleanDisabled=false

It’s all fun until one day you accidentally do a clean.

Again, make sure that this option flyway.cleanDisabled is set to true, at all costs.

First Things First — User creation in the Database:

Make sure you have two users created in your database.

1) A normal user which should be used at all times — doesn’t have delete or drop privileges:

E.g.: In MySQL:

2) And a deleteOnlyUser which should be used only during repair operations and delete/drop operations in a database. The reason why we have such an alternate user is to have much more clear access control over the database.

E.g.: In MySQL

SQL Setup:

Place all the SQL files in their individual directories corresponding to each of the databases under the SQL directory inside flyway-x.x.x.

Each of the SQL files should be named with a flyway friendly convention, as:

V1.0__some_random_text.sql

Make sure that the V in the filename is an uppercase.

Configuration Setup:

Delete the default configuration file under conf and substitute it with something like the following. Once again, there will be two configurations one for the default user and another for the deleteOnlyUser as:

  1. DefaultUser Configuration:

2. DeleteUser Configuration:

All Set for migration:

Now, there are some basic commands in the flyway for migration, repair, and displaying the information.

$ flyway -configFiles=’flyway-x.x.x/conf/$file_name.conf’ info

Displays the schema versions and baseline related information from the MySQL schema_version table.

Migrate:

$ flyway -configFiles=’flyway-x.x.x/conf/$file_name.conf’ migrate

Migrate command scans the filesystem for available migrations. It also compares these with the completed migrations. It is the centerpiece, aiding in the migration of the SQL files.

Repair:

$ flyway -configFiles=’flyway-x.x.x/conf/$file_name.conf’ repair

When there’s a failed migration, upon correction; the checksums need to be Realigned of the applied migrations with the ones of the available migrations.

Clean:

$ flyway -configFiles=’flyway-x.x.x/conf/$file_name.conf’ repair
Don’t even think about it. If you are still wondering, it rolls back all of your migrations. Not suitable for Production/UAT/Pre prod or anywhere else.

BONUS: Migrating to a different version of flyway or Starting afresh with a new set of SQL scripts:

Let’s say, our database grows in size, and there comes a scenario where the old migrations need to be archived. In that case, the following maintenance needs to be done.

mysql> drop table flyway_schema_history;
mysql> drop table schema_version;

Alter your configuration file to locate the recent SQL files and set the baseline to a different version number.

$ flyway -configFiles=’flyway-x.x.x/conf/$file_name.conf’ baseline

This baselines the database with the mentioned version. This will cause migrate to ignore all migrations up to and including that particular version.

That wraps up our discussion and flyway.

And remember kids; Always set your flyway.cleanDisabled as True.

As quoted from the flyway.conf file:

# Whether to disabled clean. (default: false)
# This is especially useful for production environments where running clean can be quite a career-limiting move.

Originally published at https://www.datawrangler.in on March 24, 2019.

--

--