Agent Wranglers

DAT-25Required

Fix production data with reviewed scripts

Change or delete production data only with a reviewed script in version control. The script checks how many rows it touched before it commits.

How to do it

Write the fix as a script or a data migration, never as a statement typed into a live console. Run it first on a copy of production. In the script, state the expected row count and roll back when the actual count differs. Log who ran it and when. Take a backup or note the restore point just before a large fix. The review and the script are the evidence that Prove every production change was reviewed asks for.

Examples

JavaScript example

await db.tx(async t => {
  const result = await t.result("UPDATE orders SET status = 'cancelled' WHERE status = 'open' AND created_at < $1", [cutoff]);
  if (result.rowCount !== expectedRows) throw new Error(`Expected ${expectedRows} rows, changed ${result.rowCount}`);
});

Python example

with connection.transaction():
    result = connection.execute(
        "UPDATE orders SET status = 'cancelled' WHERE status = 'open' AND created_at < %s", (cutoff,)
    )
    if result.rowcount != expected_rows:
        raise RuntimeError(f"Expected {expected_rows} rows, changed {result.rowcount}")

How to check

Every data change in production has a matching script in the repository. Run a script with a wrong expected count on a copy, and it rolls back with nothing changed.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/migrations/data-fixes">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.