A Failed Migration That Left the Database in a Partial State

A migration script that added 3 constraints failed partway through, leaving the database with one constraint applied and the others missing.

Overview

A migration adding three foreign keys failed on the third, left the first two committed, and the application shipped on top of the half-applied schema.

What happened

The migration file held three separate ALTER TABLE statements on orders, part of a data integrity project. The migration tool ran each one individually, and the run went like this.

  • The first ALTER TABLE added its constraint and committed.
  • The second ALTER TABLE added its constraint and committed.
  • The third ALTER TABLE failed on orphaned order records.
  • The tool marked the migration as failed and exited.
  • The application was deployed before the migration was fixed.
  • The application's validation queries started returning wrong results.

Key concepts

  • Transactional DDL
  • Pre-migration data validation
  • Idempotent migration

Interview questions

  • How do you write a migration script that is safe to run against production data and handles failures cleanly?

Sign up free to read the full topic

More War Stories topics

Start free on Cariara · Coding problems · System design studio