I was playing around with Flyway and Spring boot recently.

The repo here answers all basic questions like :

  1. What is Flyway?
  2. How does Flyway integrate with spring boot?
  3. How to maintain backward compatibility?
  4. How does Flyway rollback changes when a migration fails?

In this post I will elaborate few use cases that I found to be a worthy mention.

Say you have a CacheService that loads the data from the database when it initializes. And you are using Flyway for migration.

Ideally you would expect the flyway migration to happen first and then dao beans to get injected. But does not happen in Spring Boot.

The above will fails — for the first time run — reporting that employee table does not exists. In subsequent runs when the migration has happened already, you might end up not caching the latest value migrated in that app run — i.e. say you updated name of employee_id = 1 from foo to bar, CacheService will hold old value “foo”

To fix this, force the org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer to initialize before the dao beans are created. Like below :

Fix for this is available at springboot 2.3.0 M3. Reference :

Placeholders are helpful to pass values to the .sql files, usually depending on the environment the application is deployed to.

Another usecase is where the flyway placeholders need to be “decided” at the application startup and then passed to the .sql files.

This is where org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer comes in handy.

Check out my repository for runnable code. It would require postgres setup though.

Not a blogger. I mostly move my well-written notes here.