Quick revision
The short answer
I would set one rule for anything that must exist in production: schema and reference data change only through versioned migrations in Flyway or Liquibase, applied the same way in every environment. Test data lives with the tests, startup code that writes data is either made idempotent with a named owner or removed, and CI compares schemas across environments so drift is caught instead of discovered. The standard goes into the service template so new services start right by default.
What to remember
In this answer, we saw that drift comes from mechanisms that cannot record what they did: data.sql and startup inserts. The standard I would set sends production schema and reference data through one versioned migration tool only, keeps test data in test fixtures, allows startup writers only when they are owned and idempotent, adds a CI check that compares environments against a fresh build, and lives in the service template so new services follow it without effort.
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 would set one rule for anything that must exist in production: schema and reference data change only through versioned migrations in Flyway or Liquibase, applied the same way in every environment. Test data lives with the tests, startup code that writes data is either made idempotent with a named owner or removed, and CI compares schemas across environments so drift is caught instead of discovered. The standard goes into the service template so new services start right by default.
Why this question is asked
When different teams initialise databases in different ways, the damage is slow and quiet. Staging has a column production lacks, a lookup table has 40 rows in one environment and 38 in another, and an incident starts with someone asking which version of the reference data is real. Interviewers ask this leadership question to see whether we can turn a messy situation into a standard that people will actually follow, and whether we can explain the reasons behind it rather than just picking a tool. In this answer, we will look at why the three approaches drift, set out the standard piece by piece with the reasoning for each part, cover how to detect drift, and plan how to roll it out without stopping teams.
1Why the Three Approaches Drift Apart
It helps to start by understanding why environments drift, because the standard has to remove those causes. A data.sql script, the plain SQL file Spring Boot can run at startup, has no memory of what it already did, so it is either run on every start or skipped, and teams patch environments by hand to compensate. Application code that inserts reference data at startup runs whenever the service starts, so its effect depends on which version of the code ran last in each environment. Flyway, a migration tool that applies numbered SQL scripts once and records each one in a history table, is the only one of the three that knows what has already been applied.
So drift is not a discipline problem; it is a property of the mechanisms. Two of them cannot tell us what state an environment is in, and hand fixes made to rescue them are recorded nowhere. Whatever standard we set must give every environment a recorded, ordered history.
| Approach | Knows what ran? | Typical drift |
|---|---|---|
data.sql at startup | No | duplicates, skipped runs, hand fixes in staging |
| Startup code inserting rows | No | depends on which code version ran last |
| Flyway or Liquibase migrations | Yes, history table | only from changes made outside the tool |
2Rule 1: Production Schema and Reference Data Go Through Migrations Only
The core of the standard is that the schema and any reference data the application needs to work, such as country codes, order states or tax categories, change only through versioned migrations. We choose one tool for the organisation, Flyway or Liquibase, so people can move between teams without learning a second tool. Each change is a new script, never an edit to an old one, and it is reviewed in the same pull request as the code that needs it.
The reason this works is that the same ordered list of scripts is applied to every environment, and each database records which scripts it has. If staging is at V42 and production at V41, we know exactly what differs. Manual changes in production are not allowed; an urgent fix is written as a migration and goes through the pipeline, even if it is expedited.
3Rule 2: Test Data Lives With the Tests
Test data is different from reference data. A sample customer with three orders is useful in a test and harmful in production. So the standard says test data belongs in test fixtures: @Sql annotations on test classes, which run a script before a test method; builders or factory methods in test code; or init scripts in Testcontainers, the library that starts a real database in Docker for a test run. None of it is in src/main, and none of it runs when the application starts.
This removes the most common reason teams kept data.sql: they wanted local data to play with. A local profile that loads a fixture script into a throwaway container gives them that, without the risk of the same script reaching a shared environment.
4Rule 3: Startup Code That Writes Data Is Owned and Idempotent, or Removed
Some startup writers have a real purpose, for example creating a default tenant or registering the service in a table. We do not ban them blindly. Each one must have a named owning team, must be idempotent, meaning running it twice has the same effect as running it once, and must be safe when several instances start at the same time. If nobody can say why a writer exists, it is removed, and anything it inserted that the application truly needs becomes a migration.
The concurrency point is easy to miss. With five pods starting together, a check-then-insert in Java can insert five rows. An idempotent write uses the database to decide, for example INSERT ... ON CONFLICT DO NOTHING on a unique key, so only one insert wins.
5Detecting Drift in CI
A standard without a check slowly decays, so we add one. A scheduled CI job reads each environment's schema, either through the migration history table or by dumping the schema with a tool such as pg_dump --schema-only, and compares it with what a fresh database built from the migrations looks like. Any difference fails the job and opens a ticket for the owning team. For reference data, we compare row counts or checksums of the reference tables.
Flyway can also validate on startup: it compares the checksums of applied scripts with the files in the build and refuses to start if someone edited an applied migration. That catches the other common cause of drift, changing history instead of adding to it.
- Build a fresh database from migrationsTestcontainers, same engine and version
- Dump schema and reference-table checksumsstaging and production, read-only user
- Do they match the fresh build?YesRecord successNoOpen a ticket for the owning team
3 steps in order. Play them, or step through them one at a time.
6Rolling the Standard Out
We put the standard into the service template, so a new service is generated with the migration folder, the init mode set to never, a Testcontainers test base class and the drift job already wired in. Doing the right thing becomes the default rather than an extra task.
For existing services we accept a transition. Each team takes a baseline of its current production schema as the first migration (Flyway calls this baselining), moves its data.sql content into a seed migration with ON CONFLICT so it is safe against environments that already have the rows, and deletes the startup writers it cannot justify. We track progress per service on one page, and we explain the reason in terms teams care about: fewer staging surprises and no more hand fixes at 2 a.m.
7Follow-up questions
Flyway or Liquibase?
Either works; the point is to choose one. Flyway is plain SQL with simple versioned files and is easy to read. Liquibase offers database-independent changelogs and features such as preconditions and rollback blocks. I would pick whichever most teams already use, because the migration cost of the standard is lower.
What about very large reference data sets, like a postal code table with millions of rows?
Those do not fit well in a migration script. I would treat them as a data import with its own versioned job and a checksum recorded in a table, so the drift check can still compare environments, while the table definition itself stays in migrations.
How do you handle a team that pushes back?
I ask what their current approach gives them that the standard does not. Usually it is convenience for local data, which the test fixture rule solves. If they have a genuine need, such as a tenant bootstrap, the owned idempotent writer rule allows it. The standard should remove causes of drift, not ban useful things.
8Conclusion
In this answer, we saw that drift comes from mechanisms that cannot record what they did: data.sql and startup inserts. The standard I would set sends production schema and reference data through one versioned migration tool only, keeps test data in test fixtures, allows startup writers only when they are owned and idempotent, adds a CI check that compares environments against a fresh build, and lives in the service template so new services follow it without effort.
Open with
I would start from why the environments drift: data.sql and startup code have no memory of what they did, so they either rerun or get skipped, and people fix environments by hand.
Then, if they want more
Only a migration tool records what was applied. So the standard is that production schema and reference data change only through versioned migrations, with one tool for the whole organisation, Flyway or Liquibase, and every change reviewed with the code that needs it. No manual production changes; urgent fixes are migrations too.
Test data moves to the tests: @Sql, builders, or Testcontainers init scripts, never in src/main. Any startup code that writes data needs a named owner and has to be idempotent and safe with several pods starting at once, using something like ON CONFLICT on a unique key; if nobody can explain it, it goes.
Then I would add a CI job that builds a fresh database from the migrations and compares the schema and reference-table checksums of staging and production against it, plus Flyway's checksum validation so nobody edits applied scripts.
Finally, I would bake all of this into the service template so new services start right, and for existing ones run a baseline migration, convert their seed data, and track progress openly. The pitch to teams is fewer staging surprises and no hand fixes.
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 "Some teams seed reference data with data.sql, others with Flyway, others with...": https://scrivio-interview-library.netlify.app/databases/schema-migration/8c6a0d9600/