3

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 Answers2

0

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

-1

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.

Chris Savory
  • 2,597
  • 1
  • 17
  • 27
  • This is a way, anyway I preferred to let hibernate create schema in order to avoid export it everytime there is a change – Antonio Vivalda Oct 23 '19 at 09:19
  • Eventually, as your application grows and becomes more complex there will be scenarios that cannot be handled by hibernate. E.g. changing the database column name. We have found that for bigger project where more stability is required, it's best to have all your DDL in schema migrator like Flyway. But the hibernate way can work for you if that is what you want. – Chris Savory Oct 23 '19 at 16:15