Run Flyway Java-based callbacks with Spring Boot

You can have a configuration like this and it will work:

@Configuration
public class FlywayFactory {

    @Bean
    public FlywayMigrationInitializer flywayInitializer(Flyway flyway, FlywayCallback flywayCallback) {
        flyway.setCallbacks(flywayCallback);
        return new FlywayMigrationInitializer(flyway);
    }

    @Bean
    public FlywayCallback flywayCallback() {
        return new LogMaintenanceFlywayCallback();
    }
}

There seems to be no possibility to set the callbacks in the Spring Boot autoconfiguration (See FlywayAutoConfiguration.java)

There are 2 things you can do:

  1. Create your own Flyway instance in one of your Configuration classes. Spring Boot will not create his instance in case you do that.
  2. Autowire the Flyway instance in one of your Configuration classes and call the setCallbacks method in a PostConstruct method (But it might be tricky to make sure you call the setter before the migration starts)

Since method setCallbacks(Callback... callbacks) of the Flyway has been deprecated and will be removed in Flyway 6.0, you can use new API and FlywayConfigurationCustomizer to set up custom Java-based callbacks. Then the configuration is as below:

@Configuration
public class FlywayFactory {

    @Bean
    public FlywayConfigurationCustomizer flywayConfigurationCustomizer() {
        return configuration -> configuration.callbacks(new LogMaintenanceFlywayCallback());
    }
}