Video in progress

A Flyway migration failed halfway on prod. How do the consequences differ between Postgres and MySQL, and what do you do?

In Postgres, DDL is transactional, so the migration is rolled back completely, including its flyway_schema_history entry: no failed entry is left. In MySQL, DDL does an implicit commit, so half-applied changes stay and you have to clean them up by hand. In MySQL a failed entry does stay, and you need flyway repair afterward.

📖 Answer

In Postgres, Flyway runs each migration in one transaction. If V12__add_tables.sql has three CREATE TABLEs and one failing ALTER, everything is undone. The schema stays at the V11 state.

In MySQL, every DDL statement commits itself. If two tables are created and the third fails, they stay in the database. When you rerun it, you get "table already exists".

The usual recovery order:

  1. Read the error from the log and fix the cause (an error in the SQL, duplicate data, a lock timeout).
  2. Clean up the half-finished changes by hand (only for non-transactional DDL).
  3. flyway repair removes the failed entry from flyway_schema_history and updates the checksums (with non-transactional DDL such as MySQL; in Postgres no failed entry is left).
  4. Run the fixed migration again.
SELECT version, description, success, installed_on
FROM flyway_schema_history ORDER BY installed_rank DESC LIMIT 5;

An important rule: never change a migration file that has run successfully. The checksum won't match and the next deploy fails at the validate stage. To fix a mistake, write a new V13__fix....

Postgres has exceptions too: CREATE INDEX CONCURRENTLY and ALTER TYPE ... ADD VALUE (in old versions) don't run inside a transaction. If such a migration fails, "leftovers" like an invalid index can remain.