Practice: Flyway migrations
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most Flyway trouble comes from four habits: changing the schema without a new version, naming a file so that Flyway skips it, editing or half-running a script, and renaming in one step while two app versions run. Each one has a short, reliable fix.
Writing the next migration
Before writing a migration, run mvn flyway:info. It shows the current version, every applied script and anything pending or out of order.
Example 1 · mvn flyway:info before writing the next migration
Runs in: migration-lab: a practice copy of shoplite-api on the shoplite_lab database (V1 applied, Section 12 rows loaded)
# migration-lab, after the evolving-schema lessonmvn flyway:info[INFO] Schema version: 8 [INFO] [INFO] +-----------+---------+---------------------------+------+---------------------+--------------+----------+ | Category | Version | Description | Type | Installed On | State | Undoable | +-----------+---------+---------------------------+------+---------------------+--------------+----------+ | Versioned | 1 | create schema | SQL | 2026-10-16 12:59:38 | Success | No | | Versioned | 2 | add gst rate to products | SQL | 2026-10-16 13:04:06 | Success | No | | Versioned | 3 | add index on product name | SQL | 2026-10-16 13:04:06 | Success | No | | Versioned | 4 | add customers mobile | SQL | 2026-10-16 13:06:25 | Success | No | | Versioned | 5 | drop customers phone | SQL | 2026-10-16 13:08:34 | Success | No | | Versioned | 6 | whatsapp opt in | SQL | 2026-10-16 13:16:56 | Success | No | | Versioned | 8 | add order delivery note | SQL | 2026-10-16 13:24:28 | Success | No | | Versioned | 7 | add product brand | SQL | 2026-10-16 13:25:39 | Out of Order | No | +-----------+---------+---------------------------+------+---------------------+--------------+----------+
What this code does
- The next free version is 9: the highest applied version is 8, even though 7 ran later (marked "Out of Order").
- Descriptions come from the file names (
V4__add_customers_mobile.sql→ "add customers mobile"), so a good name documents the history. - The plugin reads
DB_URL,DB_USERandDB_PASSWORDlike the app, so the same command works against any database you point it at.
Every schema change is a new file in db/migration: the next free version, two underscores, a short description in snake_case, .sql.
Then check three things in the script itself. Existing rows: a new NOT NULL column needs a DEFAULT or must start nullable. Size: one small change per file, so a failure is easy to undo. Entities: if code maps the change, the migration and the entity go in the same commit.
Mistake 1: a file that Flyway silently skips
Example 2 shows two bad file names: the first is skipped quietly, the second is caught by a startup error.
Example 2 · Two bad file names: skipped, then caught
Runs in: migration-lab: a practice copy of shoplite-api on the shoplite_lab database (V1 applied, Section 12 rows loaded)
# two new files with small naming mistakes# src/main/resources/db/migration/V9_add_brand_index.sql (one underscore)# src/main/resources/db/migration/v10__add_status_index.sql (small v)mvn clean packagejava -jar target/shoplite-api-1.0.0.jar# the same jar with the naming check switched onjava -jar target/shoplite-api-1.0.0.jar --spring.flyway.validate-migration-naming=truerun 1 (log prefix trimmed) INFO o.f.c.i.resource.ResourceNameValidator: 2 SQL migrations were detected but not run because they did not follow the filename convention. INFO o.f.c.i.resource.ResourceNameValidator: Set 'validateMigrationNaming' to true to fail fast and see a list of the invalid file names. INFO o.f.core.internal.command.DbValidate: Successfully validated 8 migrations (execution time 00:00.092s) INFO o.f.core.internal.command.DbMigrate: Current version of schema `shoplite_lab`: 8 INFO o.f.core.internal.command.DbMigrate: Schema `shoplite_lab` is up to date. No migration necessary. INFO com.shoplite.ShopliteApiApplication: Started ShopliteApiApplication in 12.694 seconds (process running for 13.595) run 2 Caused by: org.flywaydb.core.api.FlywayException: Invalid SQL filenames found: Unrecognised migration name format: v10__add_status_index.sql Invalid versioned migration name format: V9_add_brand_index.sql (could not recognise version number 9_add_brand_index)
What this code does
- Run 1 started normally and said "up to date": both indexes are missing and only an INFO line hints at it, which is easy to miss in a long log.
- Run 2 fails fast and names each file with the reason. ShopLite can set
validate-migration-naming: trueinapplication.ymlpermanently. - Fix:
V9__add_brand_index.sqlandV10__add_status_index.sql, thenmvn clean packageso the old names leavetarget/classes.
Flyway runs only files that match its pattern exactly: capital V (or R), the version, two underscores, the description, .sql. V9_add_brand_index.sql (one underscore) and v10__add_status_index.sql (small v) are not migrations to Flyway. By default Flyway only logs that some files "did not follow the filename convention" and starts the app with the change missing.
The fix is the name, plus a safety net: spring.flyway.validate-migration-naming=true turns the quiet log line into a startup error that lists every bad file name.
Mistake 2: editing or half-running a migration
Example 3 lists the two errors that stop the next start.
Example 3 · The two errors that stop the next start
Runs in: migration-lab: a practice copy of shoplite-api on the shoplite_lab database (V1 applied, Section 12 rows loaded)
# Error A: V1 edited after it ran (VARCHAR(100) -> VARCHAR(120))# Error B: V6 failed on its second statement (a typo in the table name)A: Caused by: org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation Migration checksum mismatch for migration version 1 -> Applied to database : 509004249 -> Resolved locally : 1222396894 Either revert the changes to the migration, or run repair to update the schema history. B: Caused by: org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation Detected failed migration to version 6 (whatsapp opt in). Please remove any half-completed changes then run repair to fix the schema history.
What this code does
- Both stop the app before any migration runs, because Flyway validates first. Read the line after "Validate failed".
- A: revert the file (
git checkout -- <file>) and writeV2__widen_customer_name.sql.repairwould only hide the difference between databases. - B: Flyway itself tells the order: remove the half-completed change (
ALTER TABLE customers DROP COLUMN whatsapp_opt_in), runmvn flyway:repair, then start with the fixed script.
An applied file must never change; even a comment changes its checksum and stops every database that ran it with "Migration checksum mismatch". Revert the edit and put the change in a new version. Keep flyway repair for edits that are truly harmless.
A script that fails halfway on MySQL leaves its first statements applied, because DDL commits at once, and a success = 0 row in the history. The next start stops with "Detected failed migration to version N". Undo what ran, mvn flyway:repair, fix the script, start again.
Planning a rename with expand and contract
Example 4 plans the rename of addresses.line1 as three migrations, one per release.
Example 4 · A rename plan: addresses.line1 to address_line1
-- Release 1 (expand): V9__add_addresses_address_line1.sqlALTER TABLE addresses ADD COLUMN address_line1 VARCHAR(150) NULL;UPDATE addresses SET address_line1 = line1;
-- Release 1 code: PostalAddress maps BOTH columns; every write sets both.
-- Release 2 (contract, after every server runs release 1): V10__drop_addresses_line1.sqlUPDATE addresses SET address_line1 = line1 WHERE address_line1 IS NULL;ALTER TABLE addresses MODIFY address_line1 VARCHAR(150) NOT NULL;ALTER TABLE addresses DROP COLUMN line1;
-- Release 2 code: PostalAddress maps only address_line1.mvn flyway:info on each environment during the change: laptop Schema version: 10 (V9 Success, V10 Success) test server Schema version: 10 production Schema version: 9 (V10 Pending until release 2 is deployed)
What this code does
- The new column starts nullable because old servers still insert rows without it; release 2 fills the gaps and only then makes it
NOT NULL. - Each release ships its migration and its entity change together, so
ddl-auto=validatepasses at every step. - Production may stay at V9 for days. That is normal: Flyway runs V10 on the next deploy, wherever each database happens to be.
A rename that must not break running servers takes three releases, not one statement. Expand: add the new column and copy the data. Migrate the code: read the new column, write both. Contract: once no old version runs, copy rows written to the old column only, drop it, and remove the old field.
mvn flyway:info shows where each database is on that path, which helps when production is two releases behind your laptop.
Finish up
Mark complete & continue
Next: ShopLite: Flyway owns the schema