Schema migrations with Flyway
Versioned migrations, the checksum that breaks a deploy, and why ddl-auto=update has no place in production.
spring.jpa.hibernate.ddl-auto=update will keep your schema in step with your entities, and it is the reason this lesson exists. It works on your laptop, it works in the demo, and the first time it meets production data it will decide — silently, at startup, with no review — what to do about a column you renamed.
A migration tool replaces that guess with a file somebody wrote and somebody else read.
Why a file beats an inference
ddl-auto=update compares your entities to the database and issues DDL to close the gap. Three things it cannot do, and each is fatal in production:
- It never drops or narrows anything. Rename a field and you get a new column beside the old one, with the old data still in the old one and nothing in the new. No error.
- It cannot move data. Splitting
nameintofirst_nameandlast_nameis a data problem, and Hibernate only speaks schema. - It is not reviewable. Nobody can read a diff of what it is about to do, because there is no artefact until it runs.
A migration is an ordinary SQL file in the repository. It goes through review, it is the same on every machine, and the order it runs in is written down.
The conventions are the whole API
Files live in src/main/resources/db/migration and the name carries the meaning:
V1__create_account.sql
V2__add_email_to_account.sql
V3__backfill_email.sql
R__account_summary_view.sqlVis a versioned migration: runs once, in version order, ever.Ris repeatable: runs whenever its checksum changes, after all pendingVs. For views, functions and stored procedures — things you would rather edit in place than accumulate versions of.- Two underscores separate version from description. One is a common and confusing typo.
Flyway keeps a flyway_schema_history table in your database recording every migration it applied, when, and a checksum of the file's contents.
The checksum that stops a deploy
That checksum is the mechanism behind the rule people learn the hard way. Apply a migration, then edit the file — as you might if you spotted a typo, or widened a column while "fixing up" your own change:
Migration checksum mismatch for migration version 1
-> Applied to database : 1047314683
-> Resolved locally : 1938079900
Either revert the changes to the migration, or run repair to update the schema history.The application does not start. Not a warning, not a degraded mode — the flywayInitializer bean fails and the context never comes up.
That is deliberate, and it is the feature. The database in front of you was built by the old content of that file. Your repository now says something different. Flyway cannot know whether the difference matters, so it refuses to guess — and it refuses before your application serves a single request against a schema nobody can describe.
The rule that follows: a migration that has run anywhere is immutable. Not on production — anywhere. If a colleague has run it, editing it breaks their machine. If CI has run it, editing it breaks the build. Write V4 instead.
flyway repair rewrites the history table's checksums to match the files. It is the right tool for exactly one situation: you edited a migration that had only ever run on your own laptop. Reaching for it against production is telling the tool to stop checking the thing it exists to check.
Roll forward, not back
Flyway Community has no undo, and the instinct that this is a gap is worth examining, because the gap is mostly in the idea of rollback itself.
A migration that ran on production has already had traffic against it. drop column email cannot be undone — the data is gone. A down-migration that recreates the column gives you a schema that matches, holding nothing. The schema rolled back; the data did not.
So the discipline is forward-only, in small steps, and the pattern for anything destructive is expand → migrate → contract:
- Expand. Add the new column. Deploy. Old code ignores it; new code can use it.
- Migrate. Backfill, and have the application write both for a while. Deploy.
- Contract. Once nothing reads the old column, drop it. Deploy.
Three deploys instead of one, and at no point is there a version of the application that cannot run against the database in front of it. That property — every deploy is compatible with the schema both before and after it — is what makes a rollback of the application possible, which is the rollback you actually want at three in the morning.
Starting on a database that already exists
Most teams adopt Flyway on a schema that is already years old. baseline is the answer: it writes a history row saying everything up to version N was already here, and Flyway then only applies migrations above it.
spring.flyway.baseline-on-migrate=true
spring.flyway.baseline-version=1The honest way to do it is to dump the current schema into V1__baseline.sql first, so a fresh database — a new developer, a test container — can be built from the repository alone. A project whose migrations only work against a database somebody already has is a project with no reproducible environment.
Under the hood: what flyway_schema_history actually stores, and how migrate decides what to run
The checksum mismatch earlier in this lesson looks like magic — Flyway "just knows" a file changed — but the mechanism is one ordinary table, read and compared with plain SQL on every run. flyway_schema_history has one row per applied migration, and the columns that matter are installed_rank (the order things actually ran in, not the version number), version, description, type (SQL for a plain .sql file), checksum, installed_by, installed_on, execution_time, and success. The checksum itself is a CRC32 of the migration file's content — not a cryptographic hash, because it only needs to detect accidental drift, not resist tampering. On every migrate call, Flyway does three things in order: it reads the history table to see what is already recorded as applied; it scans the configured migration locations on disk and computes each pending file's checksum; and for every migration the history table says already ran, it recomputes that file's checksum right now and compares it against the stored one. Any mismatch — on any already-applied version, not just the newest — stops the whole operation before a single pending migration runs. That is why editing V2 after V3 has already been written and applied still fails the deploy: Flyway checks the entire recorded history, not just the tip.
This project's own history table is exactly that mechanism, doing exactly that job: frontend/db/migration/ holds V1__baseline.sql (the schema as of 16 September 2026, two independent halves for the blog and the LMS), V2__drop_prisma_migrations.sql, V3__drop_interview_question_tables.sql, and V4__project_order.sql (docs/database-governance.md). scripts/db-migrate.mjs is the only thing ever allowed to run them, and only from the deploy, before the content import, the build, and the symlink flip — never at application start-up, which is the opposite of the Spring Boot spring.flyway.enabled wiring this lesson otherwise describes, because this project's servers (code10x, code10x-blog-worker) are Node processes with no Flyway runtime inside them at all. The rule stays identical either way: an applied V<n> file is never edited, because the table that decides whether a deploy is allowed to proceed is checking its checksum on every single migration in the history, not just the latest one.
Try it yourself
Work out what the deploy sees
A teammate's PR adds V5__add_index_on_orders.sql. It is reviewed, merged, and deployed — the migration runs against production, and flyway_schema_history now has a row for V5 with success = true. Two days later, a second PR notices the index was built on the wrong column and "fixes" V5__add_index_on_orders.sql in place, changing the column name inside the existing file, and opens a PR titled "fix wrong index column".
What happens the next time the migration step runs against production, and what should the second PR have done instead?
Work through the checksum comparison before reading on
The migration step recomputes the checksum of every migration the history table already has a row for, V5 included, and compares it against the checksum stored back when V5 first ran. Editing the file in place changes its CRC32, so the comparison fails and Flyway refuses to proceed — the entire migrate step errors out before any new migration (including whatever the second PR intended to add) runs at all. Since migrations run before the build and the switchover in this project's own deploy, that means the deploy itself fails at the migration step, with the old release still serving traffic (docs/database-governance.md's production migration policy is built around exactly this: a failing migration stops the deploy with the previous release intact). The second PR should instead have added V6__fix_orders_index_column.sql that drops the wrong index and creates the correct one — forward-only, the same discipline this lesson's "Roll forward, not back" section already argues for schema changes generally, and for the identical reason: once a migration has run anywhere, including a lone production database, the file recording it is not the thing to change.
Misconceptions
- "Flyway's checksum is there to stop malicious tampering with migration files." It is CRC32, not a cryptographic hash, and CRC32 is trivially forgeable — it is not a security control. Its job is to catch accidental drift: a well-meaning edit after the fact, a merge conflict resolved by hand that silently altered a line, a file copied between environments with different line endings. The checksum mismatch is a correctness guard, not a tamper-detection one.
- "If a migration fails partway through, it rolls back automatically no matter what database you're on." Whether a failed migration leaves a partial change behind depends on whether the target database supports transactional DDL. PostgreSQL does — a single
V<n>file that fails mid-statement is rolled back as one transaction, and its history row is marked as failed so a retry is possible. Databases without transactional DDL (MySQL is the common example) can leave a migration half-applied, which is exactly why small, single-concern migration files and testing every migration against a local database first matters more on some databases than others.
Sources
- Flyway documentation, "How Flyway works" and "Migrations" — the history table's schema, checksum computation, and version-vs-installed-rank ordering.
- Flyway documentation, "Checksum mismatch" troubleshooting reference.
docs/database-governance.md(this repository) — the migration and production-migration policy this project actually runs under.