How to configure Flyway in Spring Boot in code

Viewed 2125

Currently I have Spring + Flyway + Gradle setup. Everything works fine. Spring does its automagic to run Flyway migrations. Now we need to set 2 flags for Flyway in code, to allow out-of-order migrations, and ignore missing migrations. I can see from documentations that a class (FlywayProperties) exists for this, but I can't find a way to use it inside my code.

Do I need to create some bean that changes it or how do I do it?

2 Answers

These properties can be configured directly in the Spring Boot application.properties or application.yml as follows:

application.properties:

spring.flyway.ignore-missing-migrations = true
spring.flyway.out-of-order = true

application.yml:

spring:
  flyway:
    ignore-missing-migrations: true
    out-of-order: true

The full list of supported Flyway properties can be found in the Spring Boot documentation.

For sure there are other maybe easier ways but I just can give a snippet how we do it via SpringApplicationBuilder

SpringApplicationBuilder builder = new SpringApplicationBuilder()
Map<String, Object> flywayConfig = new HashMap<>();
flywayConfig.put("spring.flyway.enabled", "true");
flywayConfig.put("spring.flyway.locations", "classpath:flyway/oracle/migration");
builder.properties(flywayConfig);

Check here for configuring the Flyway object directly: flyway outOfOrder is not working as expected

Related