Skip to content
Latchkey

Flyway "Detected failed migration" - Repair Needed in CI

A previous flyway migrate errored partway and recorded a failed entry in flyway_schema_history. Flyway blocks the next migrate until you remove that failed marker with repair - after fixing what made it fail.

What this error means

flyway migrate refuses to continue, reporting a detected failed migration to a specific version and pointing you at flyway repair. It is deterministic until the failed marker is cleared.

flyway output
ERROR: Detected failed migration to version 3.2 (add orders index).
Please remove any half-completed changes then run repair to fix the
schema history.

Common causes

A prior migration failed mid-apply

On a database without DDL transactions (e.g. MySQL), a migration can partially apply and then error, leaving a "failed" row in the history that blocks further migrates.

Half-completed changes left behind

The failed migration may have applied some statements. Those must be reconciled before repair so the next run starts from a known state.

How to fix it

Reconcile, repair, then migrate

Undo any half-applied changes from the failed migration, clear the failed marker with repair, then re-run.

Terminal
# 1) manually undo half-applied changes if any
# 2) clear the failed history entry
flyway repair
# 3) re-run after fixing the migration SQL
flyway migrate

Fix the migration that failed

  1. Read the original error to see why the migration failed (bad SQL, constraint, timeout).
  2. Correct the migration so it can apply cleanly.
  3. Run migrations against a clean database in CI to confirm the fix.

How to prevent it

  • Prefer databases/engines with transactional DDL so failures leave no partial state.
  • Run flyway validate/clean CI databases to catch failing migrations early.
  • Resolve a failed migration promptly instead of stacking new ones on top.

Frequently asked questions

What causes ""Detected failed migration to version""?
On a database without DDL transactions (e.g. MySQL), a migration can partially apply and then error, leaving a "failed" row in the history that blocks further migrates.
How do I fix "Detected failed migration to version"?
Undo any half-applied changes from the failed migration, clear the failed marker with repair, then re-run.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card