【问题标题】:Correct Execution Order with two Datasources for Flyway Migration Spring BootFlyway 迁移 Spring Boot 的两个数据源的正确执行顺序
【发布时间】:2021-09-22 11:58:28
【问题描述】:

我们有一个核心库和几个集成项目。每个集成项目都使用核心库并继承核心 DB flyway 迁移。在核心中,有创建模式和一些初始数据的 flyway 迁移脚本。集成项目还包含迁移脚本。 到目前为止,迁移脚本的执行顺序是核心迁移始终在集成项目之前运行,如下所示:

V1_1__core.sql
V1_2__core.sql
V2_1__core.sql
V1_1__integration.sql
V2_1__integration.sql
etc.

但是,我想要的是版本优先而不是项目,例如:

V1_1__core.sql
V1_2__core.sql
V1_1__integration.sql
V2_1__core.sql
V2_1__integration.sql
etc.

我在核心中使用了以下类:

@Component
public class FlywayModuleMigrationConfig implements FlywayMigrationStrategy {

    @Autowired(required = false)
    private FlywayModuleMigrationConfigService flywayModuleMigrationConfigService;

    @Override
    public void migrate(Flyway flyway) {
        DataSource dataSource = flyway.getConfiguration().getDataSource();
        
        Flyway coreModule = Flyway.configure()
                .schemas("public")
                .locations("db/migration/core")
                .table("core_schema_version")
                .dataSource(dataSource).load();

        coreModule.migrate();

        //Load module specific Flyway migration scripts
        if (flywayModuleMigrationConfigService != null) flywayModuleMigrationConfigService.migrate(flyway);

    }
}

以及集成项目中的以下内容:

public class CanbFlywayModuleMigrationConfig implements FlywayModuleMigrationConfigService {

    @Override
    public void migrate(Flyway flyway) {
        DataSource dataSource = flyway.getConfiguration().getDataSource();

        Flyway module = Flyway.configure()
                .schemas("public")
                .locations("db/migration/canb")
                .table("canb_schema_version")
                .baselineOnMigrate(true)
                .dataSource(dataSource).load();

        module.migrate();
    }
}

Flyway 如何实现这一点?

【问题讨论】:

    标签: spring-boot flyway


    【解决方案1】:

    给定一个migrate 中的多个位置 - 即:

    flyway = Flyway.configure().locations("db/migration/core,db/migration/canb")...
    

    然后 Flyway 将扫描位置并按照严格的版本顺序运行它找到的所有内容。 然而它只会使用一个历史表,这意味着你不能在核心和集成项目中拥有相同的版本号,就像你在上面的例子中那样 - 你需要什么做的是:

    V1_1__core.sql
    V1_2__core.sql
    V1_2_1__integration.sql
    V2_1__core.sql
    V2_1_1__integration.sql
    

    以便集成脚本从相应的所需核心版本中获取其版本号。如果您采用了这种模式,您还需要将过去的记录从一个历史记录表移动到另一个历史记录表,以便在一个地方拥有完整的历史记录。

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 2018-07-29
      • 2020-12-30
      • 2018-12-19
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2020-10-17
      相关资源
      最近更新 更多