Java.DBMigrationTools.What’s the purpose of flyway.repair()?

Short definition (what interviewers expect first)

flyway.repair() is a maintenance command that fixes Flyway’s metadata table (flyway_schema_history) so migrations can proceed safely again.
It does not change the database schema — it only repairs Flyway’s internal state.

📌 Key phrase:

“Repair fixes metadata, not the schema.”

What exactly does flyway.repair() do?

1️⃣ Removes failed migrations

If a migration failed halfway:

  • success = false remains in flyway_schema_history
  • Flyway will block all future migrations

repair():

  • Deletes failed rows
  • Allows you to fix the SQL and re-run

✅ This is the most common real-world use case

2️⃣ Recalculates checksums

If someone:

  • reformatted SQL
  • changed line endings
  • updated comments
  • or fixed whitespace

➡️ The checksum no longer matches

repair():

  • Recomputes checksums
  • Updates flyway_schema_history

⚠️ Only safe if the change is logically equivalent

3️⃣ Marks missing migrations as deleted (optional behavior)

If a migration file:

  • existed before
  • was applied
  • but later removed from the repo

Flyway sees it as missing

repair():

  • Marks it as deleted = true
  • Avoids startup failure

📌 This is common after refactoring or cleanup.

What flyway.repair() does NOT do (important!)

❌ It does NOT:

  • Roll back migrations
  • Fix broken schemas
  • Re-run migrations
  • Apply new migrations

This sentence is gold in interviews:

“Repair only adjusts Flyway’s bookkeeping; it never touches actual database objects.”


When should you use it?

✅ Correct scenarios

  • A migration failed and you fixed the SQL
  • Non-functional SQL changes broke checksums
  • You removed obsolete migrations intentionally
  • CI/CD pipeline is blocked due to metadata mismatch

❌ Dangerous scenarios (red flags)

  • To “hide” a broken production migration
  • To bypass a real schema inconsistency
  • Without understanding why Flyway is failing

Interview trap:

“Repair is not a substitute for proper migrations.”


Typical production workflow

# 1. Migration fails
flyway migrate  ❌

# 2. Fix the migration script
# 3. Repair metadata
flyway repair

# 4. Retry
flyway migrate  ✅

Relation to repeatable migrations (bonus)

  • Repeatable migrations (R__) rely on checksums
  • repair() updates stored checksums
  • Allows repeatables to re-run correctly

But:

  • If you changed logic, not formatting → you must understand the impact

Interview-ready answer (perfect)

Short version (2–3 sentences):

flyway.repair() fixes inconsistencies in the flyway_schema_history table by removing failed migration entries, recalculating checksums, and marking missing migrations as deleted. It does not change the database schema, only Flyway’s metadata. It’s typically used after fixing a failed migration or resolving checksum mismatches.”

This entry was posted in Без рубрики. Bookmark the permalink.