使用两个数据源更正执行顺序以进行 Flyway 迁移 Spring 启动

Correct Execution Order with two Datasources for Flyway Migration Spring Boot

我们有一个核心库和几个集成项目。每个集成项目都使用核心库并继承核心 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.

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

@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 实现这一目标?

在一个 migrate 中给出多个位置 - 即:

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

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

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

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