How to deploy web app and database in one click with Flyway (on Tomcat server)

I’ve came to this situations many times. Before deploying my Spring MVC application (for testing) on Tomcat server I need to make some changes in the database that is connected to it. It requires few steps to perform (like open database visual tool, execute the script etc.). Right now it might sound silly, but if do it several times per day it would be better to automate, so my app and database are deployed simultaneously with one click.

“person using phone leaning on wall in silhouette photography” by Warren Wong on Unsplash

Create user and database in MySQL Workbench

Before we can move to Eclispe, we need to first create the user and the database (schema). For this go to MySQL Workbench and login to it as a root user.

Add dependencies to build.gradle

First we need to add Flyway dependencies and also define flyway section in the build.gradle. NOTE that for simplicity reason I didn’t provide all required dependencies (e.g. for Tomcat, Spring, etc), all of them could be found in a final project (link at the end of the post).

Add SQL scripts to the project

In order to create database tables we need to prepare SQL scripts, like the following:

  • Version — indicates version number of the migration file. First file would be 1, next 2 and so on. It’s really important to provide it because based on it Flyway will know if changes were made in the script.
  • Separator — (default __ — two underscores), this is could be also configured.
  • Description — it’s a simple name of migration file.
  • Suffix — (.sql) indicates type of the file.

Run tomcatRun Gradle task

And that’s it! We only need to run tomcatRun task to perform both app deployment on Tomcat web server and database migration.

New database version

Once we’re done with our first migration we could perform next one :). So let’s suppose that we want to add a new table (user) and to update the old one (book). Therefore we need to create a new file, V2__library.sql.

Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.