Spring Boot deferred Flyway inizialization circular dependency

Viewed 1496

I need to execute Flyway migration after Hibernate generates all the schema table. Before migrating to Spring Boot 2.2 this code worked fine

@Configuration
public class BaseFlywayConfiguration {

    /**
     * Override default flyway initializer to do nothing
     */
    @Bean
    FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
        return new FlywayMigrationInitializer(flyway, (f) -> {
        });
    }

    /**
     * Create a second flyway initializer to run after jpa has created the schema
     */
    @Bean
    @DependsOn("transactionManager")
    FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {
        return new FlywayMigrationInitializer(flyway, null);
    }

}

Unfortunately after migrating to Spring Boot 2.2.0 I receive an Exception related to a circular dependency

This is the log:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐ | transactionManager defined in class path resource [com/myFleetSolutions/myFleet/organization/configuration/jpa/JPAConfigurationDev.class] └─────┘

How can I solve it?

2 Answers

I've simply Injected Flyway bean in the @SpringBootApplication class, and executed flyway.migrate() in the CommandLineRunner init method. It's executed at the end of the system startup and works fine

Thanks

Antonio

Ideally, you would have all your schema create (table, sequence, etc...) in flyway and not use Hibernate to generate that. I would recommend exporting your current schema and then creating a V1__init-schema.sql for flyway to init. Then set hibernate.hbm2ddl.auto to either validate or none.

Related