Quick revision
The short answer
I would layer several checks, each catching a different kind of danger. Lint the migration SQL for risky statements, for example with squawk for PostgreSQL, apply the migrations to a database restored from a production-like snapshot and measure their duration and the locks they take, run the previous release's tests against the new schema to prove backward compatibility, validate checksums so edits to applied migrations are caught, and require a database owner's review for migrations that touch large tables.
What to remember
In this answer, we designed five complementary CI checks for dangerous migrations. A linter such as squawk catches known risky statements in seconds, a run against a production-like snapshot measures duration and locks at real scale, the previous release's tests against the new schema prove backward compatibility, checksum validation catches edits to applied migrations, and a database owner reviews migrations that touch large tables with the measurements attached.
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 layer several checks, each catching a different kind of danger. Lint the migration SQL for risky statements, for example with squawk for PostgreSQL, apply the migrations to a database restored from a production-like snapshot and measure their duration and the locks they take, run the previous release's tests against the new schema to prove backward compatibility, validate checksums so edits to applied migrations are caught, and require a database owner's review for migrations that touch large tables.
Why this question is asked
Migrations are the riskiest part of most deployments because they change shared state that cannot simply be rolled back like code. The dangerous ones usually look harmless in review: an index without CONCURRENTLY, a column type change, a NOT NULL added in one step, or a column dropped one release too early. Interviewers ask this design question to see whether we can turn knowledge of these traps into automated checks. In this answer, we will design a pipeline of five checks, see what each catches and why it needs the others, and look at how to keep it fast enough that teams do not route around it.
1What We Are Trying to Catch
Dangerous migrations fall into a few groups. Some take locks that block traffic, such as a plain CREATE INDEX on a busy table or an ALTER TABLE that rewrites the table. Some are fine in principle but too slow at production size, such as an UPDATE of 200 million rows inside a migration. Some break the application version that is still running during a rolling deploy, such as dropping or renaming a column. Some break the history itself, such as an edit to a migration that has already been applied. And some are simply risky on tables that are large or critical, where a human should look before anything runs.
No single check catches all of these. A text linter understands SQL patterns but not data sizes; a timing run on real data understands sizes but not compatibility. So the design is a set of complementary checks.
- Lint the SQLpattern rules, seconds
- Validate checksumsapplied migrations unchanged
- Apply to a production-like snapshotmeasure duration and locks
- Run previous release tests on new schemabackward compatibility
- Does it touch a large table?YesRequire database owner reviewNoMerge after normal review
5 steps in order. Play them, or step through them one at a time.
Fast checks first, so most problems fail within a minute.
2Check 1: Lint the Migration SQL
The cheapest check reads the SQL and flags known dangerous patterns. For PostgreSQL, squawk is an open-source linter built for this: it parses migration files and reports rules such as creating an index without CONCURRENTLY, adding a column with a volatile default, changing a column type, adding a foreign key or NOT NULL constraint without NOT VALID, and DDL without a lock timeout.
The linter runs in seconds and catches the majority of dangerous statements before any database is involved. Rule names and output change between versions, so check your squawk version. Each rule can be suppressed for a specific statement with a comment and a reason, for example when the table is known to be tiny, which keeps the linter strict without blocking legitimate cases.
3Check 2: Apply to a Production-Like Snapshot and Measure
A statement that is safe on 1,000 rows can be dangerous on 500 million. So the second check applies the new migrations to a database restored from a production-like snapshot, with production's schema and realistic data volume, masked where needed. The job records how long each migration takes and which locks it takes and for how long.
The job compares the results with a budget, for example no ACCESS EXCLUSIVE lock held longer than 5 seconds on any table and no migration longer than 2 minutes, and fails the build when a migration exceeds it. This catches the cases a linter cannot judge, such as an ADD COLUMN that turns out to rewrite the table, or a data update that is simply too large.
4Check 3: Run the Previous Release Against the New Schema
During a rolling deploy, the previous application version runs against the new schema, and after a rollback it may run against it for hours. The third check proves this works: it applies all migrations, including the new ones, to a fresh database, then runs the previous release's integration tests against that schema, with Hibernate in validate mode and migrations disabled for the old code. If the new migration dropped or renamed a column the old code still uses, the old code's tests fail and the build stops. This catches premature contract steps, which the linter only sees as a generic warning about DROP COLUMN.
5Check 4: Validate Checksums of Applied Migrations
Editing an already applied migration does not break the new environment where it runs fresh; it breaks the existing ones, where startup fails with a checksum mismatch, or worse, where validation is off and the environments silently diverge. The fourth check runs flyway validate (or Liquibase's validate) against a database migrated from the main branch, so any change to an existing migration file fails the pull request. A simple extra rule, that files under db/migration may be added but not modified, makes the intent obvious to the author.
6Check 5: Require a Database Owner's Review for Large Tables
Some changes deserve human judgement whatever the automated checks say, because their impact depends on context: a busy period, replication setup, or a downstream consumer. We keep a list of large or critical tables, generated from table statistics, for example every table over 10 million rows or 50 GB, and the pipeline requires approval from a database owner when a migration mentions any of them. CODEOWNERS rules handle the approval mechanics, and the measurements from check 2 are attached to the pull request, so the review starts from numbers rather than guesses.
7Follow-up questions
Won't the snapshot check be too slow for every pull request?
It can be, so it runs only when a pull request adds or changes migrations, and it reuses a snapshot refreshed nightly rather than restoring production each time. Storage-level clones or database templates make the restore take seconds. For very large databases, a representative subset of the biggest tables is often enough to expose the expensive statements.
How do you avoid alert fatigue from the linter?
Start with the rules that have caused real incidents, run it in warning mode for a few weeks to tune false positives, and require a written reason for each suppression. A short list of rules that are always right is worth more than a long list that people learn to ignore.
What if a migration is dangerous but necessary?
Then the checks have done their job by making it visible. The author splits it into safe steps if possible, such as CHECK NOT VALID and VALIDATE instead of SET NOT NULL, or schedules it with a runbook, a quiet time and the database owner present. The goal is never to forbid risky changes, only to stop them from being accidental.
8Conclusion
In this answer, we designed five complementary CI checks for dangerous migrations. A linter such as squawk catches known risky statements in seconds, a run against a production-like snapshot measures duration and locks at real scale, the previous release's tests against the new schema prove backward compatibility, checksum validation catches edits to applied migrations, and a database owner reviews migrations that touch large tables with the measurements attached.
Open with
I would layer checks, because each kind of danger needs a different detector. First, a SQL linter on every new migration, for PostgreSQL something like squawk, which flags index creation without CONCURRENTLY, column type changes, volatile defaults, constraints added without NOT VALID and missing lock timeouts, with suppressions allowed only with a written reason.
Then, if they want more
That runs in seconds. Second, for pull requests that add migrations, I apply them to a database restored from a production-like snapshot, with real volumes and masked data, and measure how long each migration runs and which locks it holds, with a budget, say no ACCESS EXCLUSIVE lock longer than five seconds.
That catches what a linter cannot judge, like table rewrites or huge updates. Third, a backward compatibility job: apply the new migrations, then run the previous release's integration tests against that schema with Hibernate validating, which catches columns dropped or renamed too early.
Fourth, checksum validation against a database migrated from main, so any edit to an applied migration fails the build, plus a rule that migration files are append-only. Fifth, migrations that touch tables on a large-table list require a database owner's approval through CODEOWNERS, with the snapshot measurements attached. Fast checks run first, so most problems are reported within a minute.
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 "Design CI checks that catch dangerous migrations before they reach production.": https://scrivio-interview-library.netlify.app/databases/schema-migration/bf1b9add4c/