Video in progress
How do you connect Flyway to a prod database that is already running and was created without migrations? What does `baseline` do?
baseline tells Flyway "the database is at this version right now, don't apply the scripts before it", and adds one entry to the history table. I dump the current schema as V1__baseline.sql, set the baseline to 1 on prod, and later changes start from V2.
📖 Answer
The problem: the prod database has 80 tables but no flyway_schema_history. If you turn Flyway on, it sees a non-empty schema and fails.
The steps:
- I take the prod schema and save it as
V1__baseline.sql(pg_dump --schema-only, then cleaned up). This file is needed to build new environments (local, test) from scratch. - The configuration:
spring:
flyway:
baseline-on-migrate: true
baseline-version: 1
- On the first start on prod, Flyway creates the history table, writes
1 << Flyway Baseline >>into it, and does not runV1. On a new empty database,V1is applied as a normal migration. - Later changes start from
V2__....
Things to be careful about: I don't leave baseline-on-migrate on permanently, because if someone connects to the wrong database, Flyway will silently baseline it. I turn it off after the one-time job. Another thing: I first check whether the schemas of prod and the other environments are actually the same (a pg_dump -s diff). Often an index or column added by hand turns up in the test database.
Sign up to mark studied questions and save your progress.