Perform Database Schema Migrations in Just 5 min Using Flyway and Spring Boot

At Qovery, we rely on Kotlin with Spring Boot to develop our API and Postgres as a database. When I joined the backend team a couple of months ago, we didn’t have a straightforward solution to handle database migrations. As production access is strictly limited, I had to depend on a teammate with admin rights every time I wanted to update the schema of the database.

We needed to find a more appropriate long-term solution, as our team is fast-growing, and managing authorizations is bound to become even more painful. Eventually, we agreed to start using a database-migration tool and picked Flyway as the best tool for this job.

The following article is about to show you how easily Flyway can integrate with an existing database using the Spring framework. It is not meant to provide a deep insight into Flyway, nor is it a step-by-step tutorial to set up the tool.

Bilel Benamira

Bilel Benamira

May 19, 2022 · 2 min read
Perform Database Schema Migrations in Just 5 min Using Flyway and Spring Boot - Qovery

#What is Flyway?

Flyway is an open-source database migration tool that can handle more than a dozen SQL databases, including Postgres. It strongly favors simplicity and convention over configuration. Migrations can be written in either SQL or Kotlin, which is extremely useful when you need to migrate data with complex business logic. But most of all, Flyway offers plugins for many Java frameworks, such as Spring Boot—an obvious perk for us at Qovery, as it makes its integration super easy for us. Let me show you how it works!

#Step 1 - Importing Flyway Into our Project

In our Gradle file, we need to add Flyway’s plugin and implementation as such:

// Add this Flyway plugin to the plugins section
plugins {
    id "org.flywaydb.flyway" version "8.5.5"
}

// Add this Flyway plugin to the plugins section
dependencies {
		implementation("org.flywaydb:flyway-core:8.4.3")
}

#Step 2 - Updating our Spring Application Properties

For both application.properties files within the main and test folders, we need to add the following lines:

// Set to true if you are going to use Flyway with an existing database 
spring.flyway.baseline-on-migrate=true

// Version to use for the baseline, we used a timestamp of the date when we added Flyway
spring.flyway.baseline-version=1643814687 

// Very important to disable the dangerous clean command on production environment
spring.flyway.clean-disabled=true

And that’s pretty much it! With this done, Flyway will create a flyway_schema_history table to store information about the state of your database.

If you are following Flyway’s conventions, you just need to add migration scripts to the src/main/resources/db/migration folder. They will then be executed when the application starts.

To learn more about Flyway’s conventions, feel free to check out their documentation on migrations.

#[Bonus] Step 3 - Migration Generator

To make the process even smoother, we created a bash script to generate our migration files while respecting Flyway’s naming convention.

Every migration file’s name starts with V, followed by the version of the migration (in our case, we decided to use a timestamp to keep things simple). It is then followed by __ and the migration name.

Here’s how we create a migration:

> ./scripts/generate_migration AddInstallationIdToUser
> Created: .../src/main/resources/db/migration/V1648999476__add_installation_id_to_user.sql

#Conclusion

Flyway has been great so far! All we had to do was set up the tool, go through its documentation to gain a proper understanding of how it works, and... that’s about it! No further tweaking involved! Thanks to this seamless implementation using Spring Boot, our development process has been significantly improved in a short amount of time and with very limited effort.

Qovery white logo

Your Favorite Internal Developer Platform

Qovery is an Internal Developer Platform Helping 50.000+ Developers and Platform Engineers To Ship Faster.

Try it out now!
Engineering