【问题标题】:Setting up multiple database with Flyway使用 Flyway 设置多个数据库
【发布时间】:2018-12-23 02:37:19
【问题描述】:

我正在尝试使用 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吗?如果是这样,下面的答案有帮助吗?如果没有,您使用的是什么版本?

标签: spring-boot database-migration flyway


【解决方案1】:

我怀疑您可能在这里使用 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}占位符,则Spring Boot will work out the directory来自数据库驱动程序ID(h2、mysql、oracle等)的小写,这很好:

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

【讨论】:

  • 我必须通过前缀 classpath: spring.flyway.locations=classpath:/db/migration,classpath:/db/specific/h2 来具体说明位置
猜你喜欢
  • 2014-06-26
  • 2013-03-06
  • 2019-05-21
  • 2017-02-11
  • 2011-11-10
  • 2021-09-30
  • 2011-11-06
  • 2013-08-10
  • 2017-10-27
相关资源
最近更新 更多