【问题标题】:How to handle multiple schemas in a single migrate with flyway如何使用 flyway 在单个迁移中处理多个模式
【发布时间】:2020-06-11 16:02:03
【问题描述】:

我对 Flyway 完全陌生,但我正在尝试使用 https://github.com/flyway/flyway-docker 中描述的 docker-compose flyway+mysql 安排迁移许多相同的测试数据库

据我所知,migrate 命令可以在其-schemas 参数中采用多个模式,但它似乎只将实际的 SQL 迁移应用于列表中的第一个模式。

例如,当我使用schemas=test_1,test_2,test_3 运行迁移时,flyway 会创建所有三个数据库,但只会在第一个test_1 数据库上创建迁移文件中指定的表。

有没有办法将 SQL 迁移文件应用于列表中的所有架构?

【问题讨论】:

    标签: flyway


    【解决方案1】:

    对我来说,有效的方法是将我的迁移分解为我的 docker-compose 文件中的单独执行以及docker-postgresql-multiple-databases,如下所示:

    version: '3.8'
    services:
      postgres-db:
        image: 'postgres:13.3'
        environment:
          POSTGRES_MULTIPLE_DATABASES: 'customers,addresses'
          POSTGRES_USER: 'pocketlaundry'
          POSTGRES_PASSWORD: 'iceprism'
        volumes:
          - ./docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d
        expose:
          - '5432' # Publishes 5432 to other containers (addresses-flyway, customers-flyway) but NOT to host machine
        ports:
          - '5432:5432'
    
      addresses-flyway:
        image: flyway/flyway:7.12.0
        command: -url=jdbc:postgresql://postgres-db:5432/addresses -schemas=public -user=pocketlaundry -password=iceprism -connectRetries=60 migrate
        volumes:
          - ./sports-ball-project/src/test/resources/db/addresses/migrations:/flyway/sql
        depends_on:
          - postgres-db
        links:
          - postgres-db
    
      customers-flyway:
        image: flyway/flyway:7.12.0
        command: -url=jdbc:postgresql://postgres-db:5432/customers -schemas=public -user=pocketlaundry -password=iceprism -connectRetries=60 migrate
        volumes:
          - ./sports-ball-project/src/test/resources/db/customers/migrations:/flyway/sql
        depends_on:
          - postgres-db
        links:
          - postgres-db
    

    【讨论】:

      【解决方案2】:

      如果迁移文件未应用于列表中的所有数据库,我将保留这个问题,以防有人仍然可以回答多个模式的用处。但是,通过覆盖 flyway entrypointcommand,我能够在 docker-compose 中处理多个数据库。

      所以现在我的 docker-compose 服务看起来像:

      services:
        flyway:
          image: flyway/flyway:6.1.4
          volumes:
            - ./migrations:/flyway/sql
          depends_on:
            - db
        entrypoint: ["bash"]
        command: > -c "/flyway/flyway -url=jdbc:mysql://db -schemas=test1 migrate;
                       /flyway/flyway -url=jdbc:mysql://db -schemas=test2 migrate"
      

      【讨论】:

        猜你喜欢
        • 2017-08-14
        • 2015-02-17
        • 2017-08-29
        • 2020-11-13
        • 2017-03-03
        • 2012-08-06
        • 2017-05-21
        • 2015-05-04
        • 2013-06-02
        相关资源
        最近更新 更多