Sub topic 13
Version conflicts and out-of-order migrations in a team
This chapter assumes you can write versioned migrations, know how flyway_schema_history records them, and use Git branches and pull requests.
Why teams get conflicts
Flyway orders versioned migrations by version number, not by file date or merge time. Two developers can branch from the same commit, and each can add the next free version:
- Alice adds
V3__add_orders.sqlon her branch. - Bob adds
V3__add_invoices.sqlon his branch.
Git sees two different files, so the merge succeeds without a conflict. Flyway then refuses to start:
FlywayException: Found more than one migration with version 3Out-of-order problems are similar. Suppose Alice merges V3 and it runs in production. Bob then merges V2_5__add_index.sql, which he wrote earlier. Version 2.5 is lower than the latest applied version (3). By default Flyway treats it as an ignored migration, and validation fails because a resolved migration was never applied.
Fixing a duplicate version
Renumber the migration that has not been applied anywhere shared. Rename the file before merging:
git mv src/main/resources/db/migration/V3__add_invoices.sql \
src/main/resources/db/migration/V4__add_invoices.sqlNever renumber a migration that has already run in a shared environment. Its checksum and version are recorded in the history table, and changing the file name causes a validation error.
Reducing collisions
Choose a numbering scheme that makes collisions unlikely:
| Scheme | Example | Trade-off |
|---|---|---|
| Sequential integers | `V4__add_invoices.sql` | Easy to read, but branches collide often |
| Timestamps | `V202612010943__add_invoices.sql` | Rare collisions, but ordering follows the time the file was written, not the merge order |
With timestamps, a long-lived branch can still produce a version lower than the latest applied one. Whichever scheme you use, add a CI step that runs all migrations from scratch on a pull request after rebasing onto main. A duplicate or missing version then fails the build and not the deployment.
Enabling out-of-order
If a lower version must be applied after a higher one, set outOfOrder:
spring:
flyway:
out-of-order: trueFlyway then applies the missing migration the next time it runs. The history table records it with its own version, and it is listed after the later versions it ran behind.
SELECT installed_rank, version, description, success
FROM flyway_schema_history
ORDER BY installed_rank;installed_rank shows the real execution order. A version of 2.5 with a rank higher than version 3 is an out-of-order migration.
A practical team rule set
- Rebase onto
mainand renumber before merging. - Never edit or rename a migration that has been merged.
- Let CI build a clean database from every migration on each pull request.
- Keep
out-of-orderoff by default, and enable it only for a known case.
Exercise
Alice's V5 is merged and deployed. Bob's branch, still unmerged, contains V5__add_tags.sql. What should Bob do, and why not enable out-of-order?
Show answer
Bob should rebase onto main and rename his file to a higher unused version, such as V6__add_tags.sql, before merging. His migration has not run anywhere shared, so renaming is safe. Out-of-order only helps with a version lower than the latest applied one, and it would not fix a duplicate V5. It would also let environments run migrations in different orders.