22

Spring Boot's FlywayProperties.java supports many of the Flyway settings but not 'baselineVersion' or 'baselineOnMigrate'. I am converting an existing application to Flyway and these setting appear to be designed for this purpose. Our production environment is highly controlled and running a commandline version of flyway there to achieve this is not practical.

Is creating a custom Flyway @Bean the only option here?

Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137
dropofahat
  • 283
  • 1
  • 2
  • 12

7 Answers7

19

You can set any of flyways properties be prefixing them with flyway in your application.yml/.properties.

It is made possible by org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.FlywayConfiguration#flyway which is annotated with @ConfigurationProperties(prefix = "flyway").

If you are using an application.yml add the following:

spring:
  flyway:
    baselineOnMigrate: true

If using an application.properties add the following:

spring.flyway.baselineOnMigrate = true

Update: added prefix spring (see @pdem comment).

towi
  • 21,587
  • 28
  • 106
  • 187
Marco Schulte
  • 392
  • 3
  • 10
9

It is impossible. I spent some time today analyzing code of Spring Boot to try to find a solution to this. There is nothing about setting any of these properties in FlywayAutoConfiguration. Also I found that Spring is never calling configure method on Flyway object what would be the only other option for flyway.properties to work. Spring is abusing flyway.properties a little bit and instead of providing this file further to Flyway they use it themselves as a source of properties. That is why the set of possible options when using FlywayAutoConfiguration is so limited. So using FlywayAutoConfiguration is not a good option if you need any more advanced features of Flyway. But using @Bean is not a tragedy here. Below you may see an example of using @Bean this way that implementing this behavior would be impossible with any property files:

@Profile(value = "!dbClean")
@Bean(name = "flyway", initMethod = "migrate")
public Flyway flywayNotADestroyer() {
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource());
    flyway.setBaselineOnMigrate(true);
    return flyway;
}

@Profile(value = "dbClean")
@Bean(name = "flyway", initMethod = "migrate")
public Flyway flywayTheDestroyer() {
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource());
    flyway.setBaselineOnMigrate(true);
    flyway.clean();
    return flyway;
}

As you can see I have two Spring profiles here. One default that will not clean your database, and one with full clean of the database. Very handy.

goroncy
  • 2,053
  • 1
  • 19
  • 16
9

I had success using a FlywayMigrationStrategy.

@Component
public class BaselineOnMigrateMigrationStrategy implements FlywayMigrationStrategy {
    @Override
    public void migrate(Flyway flyway) {
        flyway.setBaselineOnMigrate(true);
        flyway.migrate();
    }
}
Seth
  • 446
  • 6
  • 8
8

You can to use on application.properties file, but you need to add spring. prefix to it for springboot 2 like @pdem marked in this answer comments https://stackoverflow.com/a/39244097/273119.

spring.flyway.baseline-on-migrate=true

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
4

I am using flyway 5.1.4, for me adding these in application.properties worked flyway.enabled = true flyway.baseline-on-migrate = true

mc20
  • 1,145
  • 1
  • 10
  • 26
2

After digging into the source and running some experiments, it would appear that because the setBaselineVersion() is overloaded in the Flyway class, Spring is unable to inject the property value.

Changing to flyway.baselineVersionAsString=2 works as desired.

dropofahat
  • 283
  • 1
  • 2
  • 12
0

Answer of Seth is worked for me. But I changed

flyway.setBaselineOnMigrate(true);

for

flyway.baseline();
JavaSash
  • 19
  • 4