Repairing production data with a dry run first
For anyone fixing rows in a live database by script: at the end the write applies exactly the plan that was reviewed, or nothing.
- Topics
- Skill areas
- Based on
- Updated
- ·
Before you start
-
A read-only connection to the database.
-
A backup of the rows in scope, with its checksum.
-
One place in the code that may write the tables.
Steps
List candidates read-only
Write the rows that look wrong to a file marked read-only. Nothing is written to the database.
Freeze a small batch
Pick a bounded number of groups from the candidates into a batch file that names the candidate file’s checksum. No duplicates and no unknown ids.
Plan in a read-only transaction
Read the current rows in one consistent read-only snapshot and write the plan: every change, the batch checksum and the version of the code that made it.
Validate before you apply
Run the apply script without its apply flag first. It re-reads every row and reports drift without writing.
Apply only the reviewed plan
Apply needs the plan’s checksum, the backup and its checksum, and the same code version that made the plan. It runs in one transaction, re-checks every row and aborts on any drift or unexpected row count.
Keep a way back
Where the write does not fit in one transaction, as in a search index, write the backup and a journal of each event to disk before and during the write, so a rollback can replay them.
When not to use this
-
A one-row fix a person can check by eye: the steps cost more than they save.
-
Data that changes faster than a plan can be reviewed: fix the source that writes it instead.
Where it comes from
Hintapiste.fi repairs product identities and search documents this way. A batch holds at most 12 groups, and the apply step refuses while the import queue has pending or failed events.