Menu

How to handle DDL rollbacks when a migration fails midway?

Reddit r/Backend·u/Mystery2058·6 months ago
#QPXf9pRa
#migration#database#fails#table#handle
Reading 0:00
15s threshold

I am using TypeORM with a MySQL database. I've noticed that if I have a single migration file containing multiple structural changes (like several CREATE TABLE or ALTER TABLE statements) and the migration fails halfway through, the database gets stuck in a partially updated state.

Even though TypeORM wraps the migration in a transaction and logs a ROLLBACK when the error occurs, the structural changes that ran before the error remain in the database. I understand this happens because MySQL issues an "implicit commit" for DDL statements, effectively ignoring the transaction. Because the migration fails, it isn't recorded in the migrations table, which leaves my codebase and database schema out of sync.

What is the best way to handle this?