Flyway 6 JavaMigrations with Native Dependency Injection for Spring Beans

Viewed 921
1 Answers

Mark your migrations as @Component and put them in a folder that is scanned by spring (e.g. within your application package and not in db.migrations). This will ensure @Autowired can be used because the bean is instantiated by spring. (The migrations in db.migrations will be scanned by flyway automatically and are not instantiated by spring.)

Then implement a FlywayConfigurationCustomizer to add the migrations by loading them from the spring context:

@Configuration
class FlywayConfiguration implements FlywayConfigurationCustomizer {
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void customize(FluentConfiguration configuration) {
        JavaMigration[] migrationBeans = applicationContext
         .getBeansOfType(JavaMigration.class)
         .values().toArray(new JavaMigration[0]);
        configuration.javaMigrations(migrationBeans);
    }
}
Related