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
- A Deployment That Could Not Be Rolled Back
- A Feature Flag Change That Triggered a Production Outage
- Accidental DELETE Without a WHERE Clause in Production
- An IAM Misconfiguration That Let a Developer Become Admin
- AWS Credentials Committed to a Public GitHub Repository
- Backups That Were Never Actually Completing
- BGP Route Leak That Rerouted Traffic Through an Unexpected AS
- Blue-Green Deployment That Resulted in Split-Brain Sessions
- Cache Stampede on a Flash-Sale Product Page
- Connection Pool Exhaustion That Brought Down 12 Microservices
- DynamoDB Hot Partition That Throttled a Flash Sale
- Failover to a Lagging Replica Lost 4 Minutes of Orders
Start free on Cariara · Coding problems · System design studio