Lucian Thorr
Lucian Thorr

Reputation: 2267

How to handle multiple schemas in a single migrate with flyway

I'm totally new to Flyway but I'm trying to migrate a number of identical test databases using the docker-compose flyway+mysql arrangement described in https://github.com/flyway/flyway-docker

As far as I can tell, the migrate command can take multiple schemas in its -schemas argument but it only seems to apply the actual SQL migration to the first schema in the list.

For example, when I run the migrate with schemas=test_1,test_2,test_3, flyway creates all three databases but only creates the tables specified in the migration file on the first test_1 database.

Is there a way to apply the SQL migration file to all the schemas in the list?

Upvotes: 2

Views: 2398

Answers (2)

Naruto Sempai
Naruto Sempai

Reputation: 7181

For me what worked was breaking up my migrations into separate executions in my docker-compose file along with docker-postgresql-multiple-databases as follows:

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

Upvotes: 0

Lucian Thorr
Lucian Thorr

Reputation: 2267

I'm going to leave this question up in case someone can still answer how multiple schemas is useful if the migration file isn't applied to all databases in the list. But, I was able to handle multiple databases in a docker-compose by overriding the flyway entrypoint and command.

So now my docker-compose service looks like:

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"

Upvotes: 1

Related Questions