代码之家  ›  专栏  ›  技术社区  ›  Meena Chaudhary

利用flyway建立多数据库

  •  2
  • Meena Chaudhary  · 技术社区  · 6 年前

    我试图用flyway 5.0.7建立两个不同的数据库,mysql用于开发,h2用于测试。我已经在各自的文件中配置了这两个数据库。

    为了 发展 我是说, src/main/resource/application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/moment
    spring.datasource.username=root
    spring.datasource.password=root
    
    flyway.locations=db/migration,db/specific/mysql
    

    为了 测试 我是说, src/test/resource/application.properties

    spring.datasource.driver-class-name=org.h2.Driver
    spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    spring.datasource.username=sa
    spring.datasource.password=sa
    
    flyway.locations=db/migration,db/specific/h2
    

    以下是Flyway迁移文件的文件夹结构

    Flyway migration file structure

    在这种情况下,Flyway无法在 specific 文件夹并在应用时抛出错误 V1.1__Insert_Records.sql 对于 table not found 是的。

    如果我移动 具体的 里面的文件夹 db/migration ,相同版本的重复文件出现错误。

    有什么建议我应该如何为多个数据库配置迁移文件以使用Flyway?

    1 回复  |  直到 6 年前
        1
  •  5
  •   codemonkey    6 年前

    我怀疑你可能在这里用的是SpringBoot2.x?如果是的话, flyway.locations 不再有效,将被忽略。

    然后,Flyway将只使用默认位置( db/migration ,它只能找到 V1.1__Insert_Records.sql 脚本但不是 V1__Create_table.sql 脚本。

    带2.x弹簧套, flyway.locations must be prefixed with spring. 以下内容:

    spring.flyway.locations=db/migration,db/specific/h2
    

    顺便说一下,如果你用 {vendor} 位置中的占位符, Spring Boot will work out the directory 从数据库驱动程序id(h2、mysql、oracle等)的小写字母开始,这很好:

    spring.flyway.locations=db/migration,db/specific/{vendor}