【问题标题】:Flyway - Different Migrations for certain envioronments - Spring BootFlyway - 特定环境的不同迁移 - Spring Boot
【发布时间】:2021-07-10 10:11:28
【问题描述】:

我们正在 Spring Boot 应用程序中的 java 中配置/启动 flyway 迁移。

所以我们有 sql 和 java 迁移文件。

在某些环境中,我们希望运行额外的迁移。基本上,在 QA 环境中,我们希望为数据库准备用于测试的数据。

所以我知道你可以在 java 中配置多个迁移位置。

所以我可以做这样的事情

if(isQAenv == true)
{
Flyway.configure().locations("/db/migrations/","db/qaMigrations/")
else{
Flyway.configure().locations("/db/migrations/")
}

我的问题在于版本控制并确保在所有环境中都按预期执行。

我需要维护编号系统的正确顺序吗?

假设我有以下应该在所有环境中运行的迁移脚本

db/migrations/V1__table1.sql
db/migrations/V2__table2.sql
db/migrations/V4__table2.sql

而且此迁移仅在 QA 环境中运行

db/qaMigrations/V3__insert_statements.sql

现在在非 QA 环境中,我的 beacon_history_table 已迁移 1、2、4。有什么办法可以避免这种情况并在历史记录表中显示更清晰的内容?

【问题讨论】:

    标签: java spring-boot flyway


    【解决方案1】:

    您可以使用配置文件轻松地做到这一点,因此假设您有配置文件,即 dev、prod,那么您可以轻松地在属性文件中为该配置文件定义 spring.flyway.locations 路径,或者在 application.yml 文件中的该配置文件的部分中定义.

    spring:
      profiles: prod
      flyway:
        locations: classpath:/db/migration,classpath:/prod/db/migration
    ---
    spring:
      profiles: dev
      flyway:
        locations: classpath:/db/migration,classpath:/dev/db/migration
    

    或者如果你有 properties 文件,那么你应该有文件,即 application-dev.properties 然后

    spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration
    

    上面提到的所有路径都应该存在于resources 文件夹中。

    【讨论】:

    • 如何控制脚本的运行顺序?例如。 dev/db 文件夹下填充表 X 的那些需要在创建表 X 的 /db 文件夹下的脚本之后和 /db 下的脚本之前运行,将表重命名为 Y
    • 顺序将取决于您使用脚本指定的版本,因此如果 dev 文件夹版本大于常用版本,它将稍后应用,如果它是第一个,那么它将在 commons 之前先应用
    猜你喜欢
    • 2018-04-06
    • 2020-02-09
    • 2018-08-16
    • 2018-12-11
    • 2020-12-31
    • 2019-08-07
    • 2023-03-27
    • 2018-07-29
    • 2018-10-07
    相关资源
    最近更新 更多