Guide

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.

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

  1. List candidates read-only

    Write the rows that look wrong to a file marked read-only. Nothing is written to the database.

    Check: The candidate file has a checksum and no write was made.

  2. 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.

    Check: The batch cannot be built from a changed candidate file.

  3. 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.

    Check: The plan has its own checksum and records which code made it.

  4. Validate before you apply

    Run the apply script without its apply flag first. It re-reads every row and reports drift without writing.

    Check: The validate-only run matches the plan with no drift.

  5. 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.

    Check: The receipt lists the ids created and retired, and the counts match the plan.

  6. 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.

    Check: A rollback from the journal returns the documents to the backup.

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.