Agent Wranglers

Migrations Required

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.

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}`);
});
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.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/migrations/data-fixes.