本文介绍了在SpringBoot应用程序中进行迁移之前,如何运行flyway:clean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Springboot和Flyway.迁移工作正常,但我希望在应用程序上下文加载了test配置文件时能够执行clean flyway命令.

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 test profile.

是否可以将SpringBoot配置为先执行clean,然后再执行migrate(如果活动配置文件是test)?

Is it possible to configure SpringBoot to do clean and then migrate if active profile is test?

推荐答案

您可以像这样覆盖Flyway自动配置:

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;
}

在Spring Boot 1.3(当前版本为1.3.0.M1,计划于9月发布GA)中,您可以使用FlywayMigrationStrategy Bean定义要运行的操作:

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;
}

这篇关于在SpringBoot应用程序中进行迁移之前,如何运行flyway:clean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:57
查看更多