Flyway command-line easy setup

One of the reasons for adopting Flyway command-line was the easy setup process (no need to install). First, I will show how to setup Flyway for a single database, i.e., as if have only one database in your server, and than the setup for multiple databases in a server.

Single database

You can download Flyway command-line here.

After download and extract you have the following folders/files structure:

flyway_sctructure

In this case it’s only necessary to work the “flyway.conf” file (you can find it in conf folder):

  1. Set url to target server/database
  2. Set user and password to target server/database

Alternatively you don´t need to set user and password on the configuration file, it can be provided as argument.

Multiple databases

Because numbers matter! If you have two or more databases in your server, a option is to apply the single database setup for every database. This approach will lead to an unnecessary files multiplication. As alternative we can create a centralized folder dedicated to the Flyway application.

  1. Extract Flyway for a folder and change the name of the folder for flyway;
  2. Create a folder each one of your databases (MyDatabase1, MyDatabase2, …);
  3. Create the folder flyway_conf and copy the file flyway.conf inside;
    1. For flyway.conf file set url, user and password for the target database;
    2. Uncomment the locations configuration and set his value with “filesystem:.” (flyway.locations=filesystem:.). This means that Flyway will scan recursively for migrations the folder that contains the flyway.cmd (MyDatabase1 for example);

  1. Create the file flyway.cmd with the following code inside:

  1. After this you can execute the flyway command from each database.

And that’s it! You are ready for manage the changes of multiple databases.

Multiple servers

If you have multiple servers like: dev, test, staging, … the solution is to create a flyway.cmd and flyway.conf for each server/environment.

  1. Inside flyway/flyway_conf folder create a flyway_servername.conf file for each server (flyway_dev.conf, flyway_test.conf) and set the url, user and password for the target server and database;
  2. Create a flyway_servername.cmd file for each server (flyway_dev.cmd, flyway_test.cmd). Muke sure that you are using the correct configuration file;
    1. Inside of each cmd file should be the following code:

You just have to execute the flyway command for the intended server/environment.

User and Password

Because we want to source control all migrations and Flyway related files, saving the user name and the password in plain text in the configuration file it’s not such a good idea. So, where’s a solution:

  1. Remove your user name and password from your configuration file;
  2. Create the folder “C:\Program Files (x86)\flyway” and inside this folder create the file flyway_ep_dev.cmd
  3. The file flyway_ep_dev.cmd should contain the following code:

  1. Add the folder “C:\Program Files (x86)\flyway” to you PATH.

This way you just have removed your credentials from you source control. You should run the new cmd file from your target database.

Leave a Reply

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