使用Flyway设置多个数据库

时间:2018-07-15 08:39:20

标签: spring-boot database-migration flyway

我正在尝试使用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时抛出错误。

如果将specific文件夹移到db/migration内,则相同版本的重复文件会出错。

任何建议如何配置多个数据库的迁移文件以与Flyway一起使用?

1 个答案:

答案 0 :(得分:2)

我怀疑您可能在这里使用Spring Boot 2.x?如果是这样,flyway.locations不再有效,将被忽略。

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

使用Spring Boot 2.x,flyway.locations must be prefixed with spring.

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

如果在位置使用{vendor}占位符,请从数据库驱动程序ID(h2,mysql,oracle等)的小写字母中使用Spring Boot will work out the directory,这很不错:

spring.flyway.locations=db/migration,db/specific/{vendor}
相关问题