Flyway - Different Migrations for certain envioronments - Spring Boot

Viewed 660

We are configuraing/starting our flyway migrations in java in our Spring Boot App.

So we have both sql and java migration files.

In certain environments we want extra migrations to run. Basically in a QA env we want to prepare the DB with data for tests.

So I know you can configure multiple migration locations in java.

So I can do something like this

if(isQAenv == true)
{
Flyway.configure().locations("/db/migrations/","db/qaMigrations/")
else{
Flyway.configure().locations("/db/migrations/")
}

My issue is with the versioning and making sure stuff gets executed as intended in all envs.

Do I need to maintain the numbering system to be the correct order?

So lets say I have the following migration scripts that should run in all envs

db/migrations/V1__table1.sql
db/migrations/V2__table2.sql
db/migrations/V4__table2.sql

And this migration only runs in QA env

db/qaMigrations/V3__insert_statements.sql

Now in a non QA env, my beacon_history_table has migration for 1,2,4. Is there any way to avoid this and have something that looks cleaner in the history table?

1 Answers

You can easily do that using profiles, so suppose you have profiles i.e dev, prod, then you can easily define the spring.flyway.locations path in your properties file for that profile or in a section for that profile in application.yml file.

spring:
  profiles: prod
  flyway:
    locations: classpath:/db/migration,classpath:/prod/db/migration
---
spring:
  profiles: dev
  flyway:
    locations: classpath:/db/migration,classpath:/dev/db/migration

Or if you have properties file then you should have file i.e application-dev.properties and then

spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration

All the paths, mentioned above should be present inside the resources folder.

Related