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
Written byBilel Benamira

Bilel Benamira

Backend software engineer at Qovery

See all articles
Engineering

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.

Engineering

You might also like

A Guided Tour of Streams in Rust

When collecting information on how to write GRPC or Websocket servers for our Qovery infrastructure, I came across a lot of resources. But while many guides provided an in-depth insight into futures, they sorely lacked information on how the Stream API works in Rust. And, more importantly, on how to use it properly. Sadly, you can't turn a blind eye on streams. As soon as you go beyond the simple request/response protocol of our beloved REST APIs, the notions of flow, async generator, and so on, inevitably arise. This is especially true when it comes to Rust. When you decide to use tonic for your GRPC or tokio tungstenite for your Websocket, the only usable interfaces with those libraries revolve around streams. This is why this article focuses on introducing streams in the context of Rust. - Read more

May 13, 2022 · 12 min read
A Guided Tour of Streams in Rust

Announcement: Pleco - the open-source Kubernetes and Cloud Services garbage collector

TLDR; Pleco is a service that automatically removes Cloud managed services and Kubernetes resources based on tags with TTL. When using cloud provider services, whether using UI or Terraform, you usually have to create many resources (users, VPCs, virtual machines, clusters, etc...) to host and expose an application to the outside world. When using Terraform, sometimes, the deployment will not go as planned. Terraform will create some resources, and others will not if it fails for any reason during the process. But what to do with those dangling resources? Delete them by hand? Too long and laborious. Manage the Terraform crash, its tfstate, and the probable state lock? Certainly not; what a pain! That's why at Qovery, we created Pleco. Pleco is a program that checks that the resources present are useful at regular intervals - reducing cloud cost and increasing engineering team efficiency. - Read more

December 24, 2021 · 7 min read
Announcement: Pleco - the open-source Kubernetes and Cloud Services garbage collector