How to run flyway:clean before migrations in a SpringBoot app?

前端 未结 2 793
一生所求
一生所求 2021-02-02 15:11

I am using Springboot and Flyway. The migrations work just fine but I wanted to be able perform a clean flyway command when the application context gets loaded with

2条回答
  •  借酒劲吻你
    2021-02-02 15:31

    You can overwrite the Flyway autoconfiguration like this:

    @Bean
    @Profile("test")
    public Flyway flyway(DataSource theDataSource) {
        Flyway flyway = new Flyway();
        flyway.setDataSource(theDataSource);
        flyway.setLocations("classpath:db/migration");
        flyway.clean();
        flyway.migrate();
    
        return flyway;
    }
    

    In Spring Boot 1.3 (current version is 1.3.0.M1, GA release is planned for September), you can use a FlywayMigrationStrategy bean to define the actions you want to run:

    @Bean
    @Profile("test")
    public FlywayMigrationStrategy cleanMigrateStrategy() {
        FlywayMigrationStrategy strategy = new FlywayMigrationStrategy() {
            @Override
            public void migrate(Flyway flyway) {
                flyway.clean();
                flyway.migrate();
            }
        };
    
        return strategy;
    }
    

提交回复
热议问题