使用 Flyway 设置多个数据库

Setting up multiple database with 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 无法在 specific 文件夹下找到迁移文件,并在为 table not found 应用 V1.1__Insert_Records.sql 时抛出错误。

如果我将 specific 文件夹移动到 db/migration 中,我会收到相同版本的重复文件的错误消息。

关于如何为多个数据库配置迁移文件以使用 Flyway 的任何建议?

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

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

用Spring开机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}