如何在单个架构和多个项目中使用Flyway迁移 [英] How can i use Flyway migration with single schema and multiple projects

查看:99
本文介绍了如何在单个架构和多个项目中使用Flyway迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何管理处理同一数据库模式的多个项目.如果每个项目中的Flyway迁移脚本都被另一个项目修改,则不允许启动.

How to manage multiple projects dealing with the same DB schema. The Flyway migration scripts in each of the project does not allow to start if it is modified by the other project.

例如:

我有一个带有FlywayInitializer类的Spring Boot ProjectX.

I have a spring boot Project X with a FlywayInitializer class.

   @PostConstruct
    public void migrateFlyway() {
        final Flyway flyway = new Flyway();

        flyway.setSchemas("schema1");
        flyway.setLocations("classpath:x.migration");
        flyway.migrate();
    }

我有一个子模块Project Y,也有他自己的FlywayInitializer类

And i have a submodule Project Y with also his own FlywayInitializer class

   @PostConstruct
    public void migrateFlyway() {
        final Flyway flyway = new Flyway();

        flyway.setSchemas("schema1");
        flyway.setLocations("classpath:y.migration");
        flyway.migrate();
    }

项目结构:

Project X
    src
      |
      main
          |
           java 
                FlywayInitializerX.java 
          |
           resources
               V1.0_create_tableX.sql
               V1.1_update_tableX.sql 

Project Y 
    src
      |
      main
          |
           java 
                FlywayInitializerY.java 
          |
            resources
               V1.0_create_tableY.sql 
               V1.1_update_tableY.sql 

我如何在Flyway中为Project X和Y使用相同的架构名称"schema1"?

How can i use the for both Project X and Y the same schemaname "schema1" with Flyway ?

谢谢@jesper_bk对我有帮助.正是我想要的,这两个项目在同一架构中具有完全独立的生活".但是现在我有以下问题:

Thanks @jesper_bk that helped me. Its exactly what i wanted, that the two projects have completely "independent lives" in the same schema. But now i have the following problem:

第一个执行的项目X正确创建了表,但是如果启动了项目Y,则会收到错误找到没有元数据表的非空模式.所以我必须将BaselineOnMigrate设置为true.但是,如果我将BaselineOnMigrate设置为true,则Project Y跳过sql文件 V1.0_create_tableY.sql ,并以 V1.1_update_tableY.sql 开头.我如何达到也为Project Y执行了第一个sql脚本V1.0_create_tableY.sql?

The first executed project X create tables correcty, but if Project Y is started i get the error Found non-empty schema without metadata table. So i have to set BaselineOnMigrate to true. But if i set BaselineOnMigrate to true the Project Y skip the sql file V1.0_create_tableY.sql and starts with V1.1_update_tableY.sql. How can i reach, that the first sql script V1.0_create_tableY.sql is also executed for Project Y?

 @PostConstruct
    public void migrateFlyway() {
        final Flyway flyway = new Flyway();

        flyway.setBaselineVersionAsString("1");
        flyway.setBaselineOnMigrate(true);

        flyway.setSchemas("schema1");
        flyway.setLocations("classpath:y.migration");
        flyway.migrate();

    }

推荐答案

如果您可以在同一个架构中使用两个具有完全独立生命"的项目,则可以为这两个使用单独的版本表,即:

If you can live with two projects having completely "independent lives" in the same schema, you could use separate version tables for the two, i.e.:

@PostConstruct
public void migrateFlyway() {
    final Flyway flyway = new Flyway();

    flyway.setSchemas("schema1");
    flyway.setLocations("classpath:x.migration");
    flyway.setTable("schema_version_y");
    flyway.migrate();
}

如果希望他们使用相同的版本控制方案,最好将所有SQL脚本放在单独的第三个项目中,或者-更复杂的是-拥有第三个项目来自动从主数据库中收集和枚举SQL脚本,这可能会更好.项目.

If you want them to use the same versioning scheme, you are probably better served with putting all SQL scripts in separate third project, or - even more complicated - have a third project that automatically gathers and enumerates the SQL scripts from the main project.

关于第二个问题, baselineVersionAsString 应该为<1(例如0).如果基准版本为1,它将确定您的第一个1.0版脚本与基准匹配,并且应该已经执行.

Concerning your second question, baselineVersionAsString should be < 1 (e.g. 0). If the baseline version is 1, it will determine that your first script with version 1.0 matches the baseline, and should already have been executed.

这篇关于如何在单个架构和多个项目中使用Flyway迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆