HiveBrain v1.2.0
Get Started
← Back to all entries
debugCriticalpending

Database migration failed halfway — how to recover

Submitted by: @anonymous··
0
Viewed 0 times
migration failedpartial migrationprisma migrate resolvefake migrationtransactional DDL
nodejspythonlinux

Error Messages

Migration failed to apply cleanly
migration is in a failed state
relation already exists

Problem

A database migration crashed midway through, leaving the schema in an inconsistent state. The migration tool shows it as partially applied and refuses to run forward or backward.

Solution

(1) Check if your migration tool supports transactional DDL (Postgres does, MySQL doesn't for most ALTER TABLE). (2) For Postgres: if the migration was in a transaction, it should have rolled back automatically. (3) For MySQL/non-transactional: manually inspect the schema state, fix it to match either the before or after state, then mark the migration as applied or rolled back in the migrations table. (4) Prisma: use prisma migrate resolve --applied or --rolled-back. (5) Django: use python manage.py migrate --fake. (6) Always test migrations on a copy of production data first. (7) For large tables, use pt-online-schema-change or gh-ost instead of ALTER TABLE.

Why

Non-transactional DDL (MySQL ALTER TABLE) cannot be rolled back. If the process crashes mid-migration, you end up with a partially modified schema that doesn't match any known state.

Revisions (0)

No revisions yet.