Sub topic 13
Version Conflicts and Out-of-Order Migrations in a Team
This chapter assumes you can write versioned migrations, you know how Flyway records them in flyway_schema_history, and you use Git branches and pull requests. It covers what goes wrong when several developers add migrations at the same time, and how to handle it.
Why teams hit conflicts
Flyway orders versioned migrations by their version number. Each applied migration is recorded in flyway_schema_history, and Flyway compares that record with the files on the classpath at every startup.
Two kinds of problems appear when more than one person adds migrations:
- Duplicate versions. Two branches each add
V5__...sql. Flyway refuses to start because it finds two migrations with the same version. - Late versions. A branch adds
V5, butV6is already applied in the target database. Version 5 is lower than the highest applied version, so Flyway treats it as out of order.
Neither problem is a bug in Flyway. Both come from the fact that the version number is shared state, and Git does not know it is shared.
Duplicate versions
Suppose main has V1 to V4. Alice and Bob each branch from it.
-- Alice's branch: src/main/resources/db/migration/V5__add_phone_to_app_user.sql
ALTER TABLE app_user ADD COLUMN phone VARCHAR(30);-- Bob's branch: src/main/resources/db/migration/V5__add_last_login.sql
ALTER TABLE app_user ADD COLUMN last_login TIMESTAMP;The file names differ, so Git reports no merge conflict. After both merge, the application fails at startup with a message like Found more than one migration with version 5.
The fix is to rename the later migration before it is applied anywhere shared. If Bob merges second, he renames his file to V6__add_last_login.sql, and he reruns his tests against a database that contains Alice's change.
Out-of-order migrations
The second problem is subtler. Alice merges V5 and it is deployed to the staging database. Bob's V4_1 or V5-numbered work was written earlier but merged later, so it has a lower version than what staging already has.
By default Flyway does not apply such a migration. With the default validate-on-migrate: true, it reports the migration as ignored and the validation step fails. It does not apply the migration quietly.
You can see this with flyway.info(). The state of the skipped migration is IGNORED.
| State | Meaning |
|---|---|
| `SUCCESS` | Applied and recorded. |
| `PENDING` | Found on the classpath, not applied yet. |
| `IGNORED` | Lower version than the latest applied one, and never applied. |
| `MISSING_SUCCESS` | Recorded as applied, but the file is no longer found. |
| `FAILED` | Started and failed. |
Flyway has an option for this case.
spring:
flyway:
out-of-order: trueWith out-of-order: true, Flyway applies an ignored migration with a lower version after higher ones. It records it in the history with its own version and a later installed_rank. The order in which migrations were applied then differs from the order of their versions.
When out-of-order is a bad idea
Out-of-order mode makes the failure go away, and it can also hide a real design problem. Consider these risks:
- Different order in different environments. A fresh database applies
V5beforeV6. Staging appliedV6first, thenV5. If the two migrations touch the same table, the result can differ. - Hidden dependencies.
V5may assume a column thatV6dropped. On a fresh database this fails. On staging it may pass, or the reverse. - Weaker guarantees. The version number no longer tells you what state the schema is in.
Use out-of-order as a deliberate choice for independent changes, such as separate tables, and not as a default. Many teams enable it only in development and keep it off in production.
Prevent conflicts with a team convention
The best fix is to avoid the clash. Common conventions:
- Timestamp versions. Name files
V20261201093000__add_phone.sql. Collisions become unlikely, and versions sort by creation time. Mixing with old small numbers works because the timestamp is larger than any of them. - A central counter. Reserve the next number in the issue tracker or a shared file before writing the migration.
- Rebase before merge. The second pull request rebases onto
main, renames its migration to the next free number, and then merges. - A CI check. A job that fails when two files share a version, or when a new migration is lower than the highest one on
main, catches the problem in the pull request.
Timestamps trade readability for safety. A timestamp also does not fix the late-version case, since a branch that lives for a week can still carry a version older than the latest on main. Pair timestamps with the CI check or with a rebase rule.
Here is a simple CI check written as a test. It scans the migration folder and fails when versions repeat.
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
class MigrationVersionTest {
@Test
void versionsAreUnique() throws IOException {
Path dir = Path.of("src/main/resources/db/migration");
Set<String> seen = new HashSet<>();
try (Stream<Path> files = Files.list(dir)) {
files.map(p -> p.getFileName().toString())
.filter(n -> n.startsWith("V") && n.contains("__"))
.map(n -> n.substring(1, n.indexOf("__")))
.forEach(v -> assertTrue(seen.add(v), "Duplicate version: " + v));
}
}
}The test covers only SQL files in that folder. Java migrations in db.migration need a separate check, or a check that uses Flyway itself.
A safe merge workflow
Running on a fresh database matters. It proves the whole chain applies in version order, which is what a new environment will do.
Handling a conflict that already reached an environment
Sometimes a duplicate or late migration is already applied in a shared database. Work from the least invasive option.
- Leave the applied migration alone. Rename or renumber only the unapplied one.
- Enable
out-of-orderfor one deployment if the late migration is independent, then turn it off again. - Add a new migration that fixes the schema difference. Do not edit the applied file, because its checksum is recorded.
- Use
flyway repaironly to realign the history table after you have fixed the files, for example to remove a failed entry or update a checksum. It does not undo changes the SQL made to the schema.
A failed migration on a database without transactional DDL, such as MySQL, can leave a half-applied change. On PostgreSQL each migration runs in a transaction by default, so a failure rolls back cleanly. Know which behavior your database gives you before you plan recovery.
Summary of settings
| Property | Default | Use |
|---|---|---|
| `spring.flyway.out-of-order` | `false` | Allow applying a lower version after higher ones. |
| `spring.flyway.validate-on-migrate` | `true` | Fail when applied history and files disagree. |
| `spring.flyway.ignore-migration-patterns` | `*:future` | Choose which validation problems to tolerate. |
Exercise
Two pull requests both add V7__...sql and both merge to main without a Git conflict. What happens at startup, and what is the fix?
Show answer
Flyway fails with an error that it found more than one migration with version 7. Rename the migration that was merged later to the next free version (for example V8), after confirming it has not been applied to any shared database, then rerun the tests on a fresh database.
Exercise
Staging has applied V6. A branch merges with V5. Flyway reports it as IGNORED and validation fails. Name two ways to proceed and one risk of the first.
Show answer
Renumber the migration to V7 (safe if it is independent of the later change), or set spring.flyway.out-of-order=true for the deployment. The risk of out-of-order is that environments apply migrations in different orders, so a migration that depends on or conflicts with V6 can behave differently on staging than on a fresh database.