Sub topic 10
Adopting Flyway on an existing database
This chapter assumes you have finished the earlier chapters on versioned migrations and the flyway_schema_history table. You should also be comfortable with spring.flyway properties.
The problem
Flyway expects to own the schema from the start. If you point it at a database that already has tables but no flyway_schema_history table, it refuses to continue. The error looks like this:
Found non-empty schema(s) "public" but no schema history table.
Use baseline() or set baselineOnMigrate to true to initialize the schema history table.Flyway stops here to protect you. If it ran V1__create_app_user.sql against a database that already has app_user, the migration would fail or damage data.
What a baseline does
A baseline is a marker in the schema history table. It tells Flyway to treat the database as already being at a given version. Flyway inserts one row with the type BASELINE and the chosen version. After that, Flyway skips every migration with a version less than or equal to the baseline version. It applies only the higher ones.
Step 1: capture the current schema
Create a migration that recreates the existing schema, and give it the baseline version. Use V1__baseline_existing_schema.sql. You can produce its content with a schema-only dump, such as pg_dump --schema-only for PostgreSQL. Clean the dump up by hand.
This file serves two purposes:
- A new, empty environment (a developer laptop or a CI database) runs
V1and ends up with the same schema as production. - Production never runs
V1, because it is baselined at version 1.
Step 2: configure the baseline
spring:
flyway:
baseline-on-migrate: true
baseline-version: 1
baseline-description: Existing production schemaWith baseline-on-migrate enabled, Flyway checks the database at startup and picks one of three paths:
- The history table exists. Flyway migrates as usual.
- The schema is empty. Flyway does not baseline. It runs
V1and the later migrations normally. - The schema is non-empty and has no history table. Flyway baselines at
baseline-version, then applies any migration above it.
Check the result
After the first startup against the existing database, query the history table:
SELECT installed_rank, version, description, type, success
FROM flyway_schema_history
ORDER BY installed_rank;The first row should have type = 'BASELINE' and version = 1. Any later migration, such as V2__add_display_name.sql, appears as a normal SQL row below it.
Common errors
- Baseline version too low. If the database matches your
V1but you baseline at0, Flyway runsV1against existing tables and it fails with "already exists". - Baseline version too high. Flyway silently skips migrations at or below that version. A change you wrote may never run.
- Dump drift. If
V1differs from the real production schema, new environments and production diverge without any error. Compare a schema-only dump of production with a database built fromV1. - Leaving
baseline-on-migrateon. It is safe to keep once the history table exists. It can hide a mistake, though, if someone points the application at the wrong non-empty database. Many teams turn it off after the first deployment.
Exercise
Your project has V1_baselineexistingschema.sql and V2adddisplayname.sql. Production already has the V1 schema and no flywayschema_history table. You set baseline-on-migrate to true and baseline-version to 1. What does Flyway do on the first startup against production?
Show answer
Flyway sees a non-empty schema with no history table, so it inserts a BASELINE row at version 1. It skips V1 because that version is not above the baseline. It then applies V2 and records it as a normal migration.