Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

You must introduce Flyway into a service whose production database already has 80 tables created by hand and by Hibernate ddl-auto=update. How do you do it safely?

First we make the environments agree: compare the schemas of production, staging and development and fix the drift. Then we capture the production schema as a baseline script, V1, and configure Flyway with baselineOnMigrate and baseline version 1 so existing databases are marked as already at V1 while new ones run the script. We switch Hibernate's ddl-auto to validate or none so only Flyway changes the schema, and we run every new migration in CI against a database with production's structure before it ships.

Why this question is asked

Adopting a migration tool on a young project is easy. Adopting it on a database that has lived for years, with 80 tables created partly by hand and partly by Hibernate's ddl-auto=update, is a different job: nobody is quite sure what the schema is, and every environment is a little different. Interviewers ask this scenario because the risky part is not Flyway itself but the steps around it. In this answer, we will see why the current setup is dangerous, measure and fix the drift, generate a baseline, introduce Flyway with baseline-on-migrate, turn off Hibernate's schema changes, and set up CI so the first real migration is safe.

1Why the Current Setup Is Risky

With ddl-auto=update, Hibernate compares the entity classes with the database at startup and issues ALTER TABLE statements to add what is missing. It adds tables and columns, but it never drops columns, never changes types safely, never renames anything and never backfills data. Combined with manual changes, it means the schema is the accidental result of every startup that ever ran, on every environment.

So the main risk is drift, differences between the schemas of environments that should be identical. A migration written and tested against staging can fail in production because a column is a different type there, or an index someone added by hand is missing. Before Flyway can give us one history, we need one starting point.

2Measure and Fix the Drift

We dump the schema of each environment without data and compare the dumps. For PostgreSQL, pg_dump with --schema-only gives a text file we can diff; schema comparison tools give a more readable report.

Java
pg_dump --schema-only --no-owner --no-privileges -d prod > prod.sql
pg_dump --schema-only --no-owner --no-privileges -d staging > staging.sql
diff prod.sql staging.sql
# < CREATE INDEX idx_customer_email ON customer (email); only in prod (added by hand)
# > ALTER TABLE orders ADD COLUMN legacy_flag boolean; only in staging (old ddl-auto)
# < ALTER TABLE invoice ALTER COLUMN amount TYPE numeric(12,2) prod; staging has numeric(10,2)

Each difference needs a decision: which side is correct? Usually production is the reference, because it is what the business runs on, but not always; a missing index in production may be a performance bug. We fix the other environments with one-off scripts until their dumps match production's. This is tedious, but every difference we leave in place becomes a migration that works in one environment and fails in another.

3Generate the Baseline Script

Next we turn production's schema into the first migration. We take the cleaned-up schema dump, remove anything environment-specific such as owners, grants for personal accounts and tablespace names, and save it as V1__baseline.sql. A fresh database built from this script must produce the same schema as production; we prove it by running the script against an empty database and diffing its dump with production's.

SQL
-- src/main/resources/db/migration/V1__baseline.sql
-- Schema of production as of 2026-11-05: 80 tables, generated from pg_dump --schema-only
CREATE TABLE customer (...);
CREATE TABLE orders (...);
-- ... 78 more tables, indexes, constraints, sequences

Notice that existing databases will never run this script; they already have these tables. It exists for new environments: test databases, developer laptops, CI and any future region. That is why it must match production exactly.

4Introduce Flyway With a Baseline

Now we add Flyway with two settings that tell it how to treat a database that already has tables but no history.

Properties
spring.flyway.baseline-on-migrate=true
spring.flyway.baseline-version=1
spring.jpa.hibernate.ddl-auto=validate
# First startup against production:
# flyway_schema_history created
# 1 | 1 | << Flyway Baseline >> | BASELINE (V1 is treated as already applied)
# First startup against an empty test database:
# 1 | 1 | V1__baseline.sql | SQL (V1 runs and creates all tables)
Step throughIntroducing Flyway to an existing database
  1. Diff the schemas of every environmentpg_dump --schema-only
  2. Fix drift until all match productionone-off scripts, decided case by case
  3. Save production's schema as V1__baseline.sqlprove it on an empty database
  4. Enable Flyway with baseline version 1existing databases get a baseline row
  5. Set ddl-auto to validateonly Flyway changes the schema now
  6. Add V2 and later, tested in CIagainst production's structure

6 steps in order. Play them, or step through them one at a time.

With baseline-on-migrate, when Flyway finds a non-empty schema without a history table, it creates the table and writes a baseline row at version 1 instead of running V1. From then on it only runs migrations above version 1. On an empty database, there is nothing to baseline, so V1 runs normally. The same build therefore works for both, which is exactly what we want.

There is a catch. baseline-on-migrate will baseline any non-empty schema without history, including a staging database that still has drift we missed. That is why step 1 comes first, and why some teams prefer to run flyway baseline explicitly once per existing environment and leave baseline-on-migrate off.

5Stop Hibernate From Changing the Schema

Once Flyway owns the schema, Hibernate must stop changing it. We set spring.jpa.hibernate.ddl-auto to validate, which makes Hibernate check at startup that every entity matches a table and column of a compatible type, and fail if not. Some teams prefer none, which skips the check entirely; validate is usually more useful because it catches an entity change that someone forgot to write a migration for. Leaving update in place would mean two tools changing the same schema, and the history would immediately stop being complete.

6Test Every New Migration Against Production's Structure

From now on every schema change is a new migration, starting at V2. In CI, we start a real PostgreSQL of the same version as production with Testcontainers, run V1 and all later migrations from empty, and then run the integration tests with ddl-auto=validate. Because V1 matches production, this tests the new migration against production's structure.

For larger tables, we also apply the new migrations to a restored copy of production, or at least of its structure with realistic data volumes, before the release, and watch how long each step takes. A migration that runs in one second on an empty table may lock a table with 50 million rows for minutes.

7Follow-up questions

Why not let Flyway start from version 0 with an empty baseline?

Then new databases would have no tables, because nothing would create the 80 existing ones. The baseline script is what lets a new environment reach today's schema; the baseline version is what tells existing databases to skip it.

What about reference data that the application needs, such as country codes?

It belongs in a migration too, either inside the baseline or as a separate V2 that inserts the rows with INSERT ... ON CONFLICT DO NOTHING, so new environments get it and existing ones are unaffected. Test data does not belong in production migrations.

How do we handle the stored procedures and views created by hand?

Views and functions fit well as repeatable migrations, R__ scripts written with CREATE OR REPLACE. We extract them from production, put each in its own file, and let Flyway reapply them whenever they change.

8Conclusion

In this answer, we introduced Flyway into a database whose schema grew by hand and by ddl-auto=update. We measured and fixed drift between environments first, captured production's schema as a V1 baseline and proved it on an empty database, used baseline-on-migrate with baseline version 1 so existing databases skip it while new ones run it, switched Hibernate to validate, and tested every later migration in CI against production's structure.

What to say in the interviewAbout 90 seconds aloud

Open with

The main risk here is not Flyway, it is that nobody knows the real schema, because ddl-auto=update and manual changes have made every environment slightly different. So I would start by dumping the schema of production, staging and development with pg_dump --schema-only and diffing them, then decide difference by difference which is correct and fix the others until they match production.

Then, if they want more

Then I capture production's schema as V1__baseline.sql, strip owners and grants, and prove it by running it on an empty database and diffing the result with production. Next I enable Flyway with baseline-on-migrate and baseline version 1.

On an existing database without a history table, Flyway writes a baseline row at version 1 and only runs migrations above it, while an empty database runs V1 and gets the full schema, so the same build works everywhere.

I would be careful that baseline-on-migrate does not baseline an environment with drift I missed, or run the baseline command explicitly per environment instead. At the same time I switch spring.jpa.hibernate.ddl-auto to validate, so Hibernate only checks the mapping and Flyway is the only thing that changes the schema.

From then on every change is a new migration, tested in CI with Testcontainers on the same PostgreSQL version, and for large tables rehearsed against a copy of production's structure and volume.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You must introduce Flyway into a service whose production database already has 80 tables...": https://scrivio-interview-library.netlify.app/databases/schema-migration/22b2273f02/