Quick revision
The short answer
I treat the migration history as an audit trail worth keeping, but not something every new environment should replay. Existing databases keep their full history, while new environments start from a squashed baseline of the schema at a recent version, or from a ready-made schema snapshot, and then apply only the newer migrations. Data fixes and environment-specific steps move out of the schema migrations into separate jobs, and conventions set early keep the history readable for the next ten years.
What to remember
In this answer, we treated 1,400 migrations as both an asset and a cost. The history is a valuable audit trail, so existing databases keep it and the old scripts stay in git, but new environments should not replay it: they start from a proven, squashed baseline or a migrated snapshot and run only recent migrations. Data fixes and environment-specific steps move into separate jobs, tests use templates while one job still exercises the real path, and conventions set early keep the history fast and readable over many years.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I treat the migration history as an audit trail worth keeping, but not something every new environment should replay. Existing databases keep their full history, while new environments start from a squashed baseline of the schema at a recent version, or from a ready-made schema snapshot, and then apply only the newer migrations. Data fixes and environment-specific steps move out of the schema migrations into separate jobs, and conventions set early keep the history readable for the next ten years.
Why this question is asked
Every migration script starts life as a small, sensible change. After eight years and 1,400 of them, the folder has become a slow, fragile time machine: every new test database, developer laptop and CI run replays the entire life of the schema, including tables that were created and dropped years ago, data fixes for bugs nobody remembers and steps that only ever made sense in one region. Forty minutes per new environment is a cost paid many times a day. Interviewers ask this at the most senior level because the answer is a judgement about an asset that is also a liability. In this answer, we will look at what the history is worth, why it became slow, how to squash it safely, what to move out, how snapshots help tests, and which conventions keep it healthy.
1What the History Is Worth, and What It Costs
The migration history is the only complete record of how the schema got to where it is. It tells us when a column appeared, which change introduced a constraint and what was done to data during a past incident. Auditors may ask for it in regulated businesses, and engineers use it when debugging a strange schema detail. Those are reasons to keep it.
But the history has a running cost that grows with every migration. A new environment must replay all 1,400 steps, many of which were undone later: a table created in 2019 and dropped in 2021 still costs two migrations. Some steps are slow because they rewrite data, and some depend on the state of a long-gone environment and break randomly. Forty minutes of replay per environment also slows down CI, which pushes teams to share test databases, which brings back the drift migrations were supposed to remove. The goal is to keep the record and stop paying for the replay.
2Squash Old Migrations Into a New Baseline
Squashing means replacing the first N migrations, for new environments only, with a single script that creates the schema as it was at version N. Existing databases are already past version N and keep their full history untouched. New databases start at version N and then run the migrations after it.
| Full replay | Baseline at 1300 | |
|---|---|---|
| New environment build | 1,400 migrations, about 40 minutes | 1 baseline + 100 migrations, about 2 minutes |
| Existing production database | unchanged | unchanged: full history kept |
| Audit trail | in the migration folder | in git (archive) and the history table |
| Breakage from obsolete steps | frequent | only in the last 100 steps |
Flyway's baseline migrations, files with a B prefix, are designed for this: a new database runs the latest baseline and the versioned migrations after it, while an existing database ignores the baseline because its history already passed that version. Check whether your Flyway edition and version support them. Where they are not available, the same effect is reached by moving the old scripts out of the location and having new environments start from a restored snapshot with a matching history table, as described below.
Two rules make a squash safe. The baseline must be generated from a database actually migrated to version 1300, not written by hand, and we prove it by comparing a database built from the baseline with one built from the full history. And the old scripts stay in git, so the audit trail is not lost; only the replay is.
3Move Data Fixes and Environment Steps Out
Looking through 1,400 migrations usually shows that many are not schema changes at all. Some backfill or correct data, such as fixing the currency of 30,000 invoices after a bug, which may take minutes on a large table. Others only make sense in one place, such as granting rights to a reporting user in one region or creating a partition for one customer. These steps make the replay slow and fragile, and they mix two different kinds of change.
Going forward, schema migrations should contain only schema changes and small reference data. Data corrections run as separate, reviewed jobs with their own logs, and they are recorded in their own history. Environment-specific setup belongs in infrastructure code for that environment. The squash is a good moment to drop such steps from the baseline, since a new environment does not need to repeat a data fix for rows it never had.
4Keep Schema Snapshots and Templates for Tests
For automated tests, we can go further than a squashed baseline. A CI job can build a database image or a database template at each release, already migrated to the latest version, and tests start from it in seconds. In PostgreSQL, CREATE DATABASE test_42 TEMPLATE app_template copies a fully migrated database almost instantly, and with Testcontainers a pre-migrated image can be pulled instead of running migrations every time.
There is a trade-off: if every test starts from a snapshot, nothing proves the migrations themselves still work. So one pipeline job still runs the real path, baseline plus recent migrations from empty, and another applies the newest migrations to the previous release's snapshot, which is what production will actually do.
5Set Conventions Early
A history that will live for many years needs rules from the start, the same way a codebase needs a style guide. Useful conventions include a version scheme that avoids collisions between teams, such as a timestamp like V20261105_1430, descriptions that say what the change is for, one logical change per migration, no data fixes in schema migrations, no environment conditions inside scripts, and a planned squash every year or two so the replay never grows beyond a few hundred steps. Each convention is cheap at migration 10 and very expensive to introduce at migration 1,400.
6Follow-up questions
Is it safe to delete the old migration files completely?
Deleting them from git loses the audit trail, so we archive them instead. Removing them from the migrate location is safe only when existing databases are configured to ignore applied migrations that are missing locally, or when a baseline mechanism handles it; check your Flyway version's settings before doing it, and test against a copy of production first.
How often should we squash?
Often enough that a new environment stays fast, typically when the number of migrations since the last baseline reaches a few hundred or the build time passes a few minutes. Squashing to a version a few releases old, rather than the very latest, gives existing environments time to move past it.
What about environments that are far behind, such as an old customer installation?
They keep using the full history, which is why the archive must stay runnable for as long as such installations are supported. Once every supported environment is past the baseline version, the archive becomes purely historical.
7Conclusion
In this answer, we treated 1,400 migrations as both an asset and a cost. The history is a valuable audit trail, so existing databases keep it and the old scripts stay in git, but new environments should not replay it: they start from a proven, squashed baseline or a migrated snapshot and run only recent migrations. Data fixes and environment-specific steps move into separate jobs, tests use templates while one job still exercises the real path, and conventions set early keep the history fast and readable over many years.
Open with
I see the migration history as two things at once. It is an audit trail of how the schema evolved, which has real value for debugging and compliance, so I would never throw it away.
Then, if they want more
But replaying all 1,400 steps for every new environment is a cost that grows forever, and 40 minutes per environment hurts CI and pushes teams toward shared databases and drift. So I separate keeping the record from replaying it. Existing databases keep their full history untouched.
For new environments, I squash the old migrations into a baseline of the schema at, say, version 1300, generated from a migrated database and verified by comparing it with one built from the full history. With Flyway that can be a baseline migration if our edition supports it, or a snapshot plus history table otherwise.
The old scripts move to an archive in git. While doing that, I would separate data fixes and environment-specific steps from schema migrations; they become reviewed jobs and infrastructure code. For tests, I would build a pre-migrated template or image per release, while one CI job still runs the real migration path.
And I would set conventions: collision-free versions, one change per script, no data fixes, and a planned squash every year or two.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Migration scripts accumulate for years: one service has 1,400 Flyway migrations and new...": https://scrivio-interview-library.netlify.app/databases/schema-migration/2fe6f0123c/