---
title: "Fix production data with reviewed scripts · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/migrations/data-fixes
updated: 2026-10-20
licence: "All rights reserved"
---

# Fix production data with reviewed scripts

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Migrations](https://agentwranglers.se/playbook/data/migrations)
- Rule: 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: 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.
- 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.

Example:

```js
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
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}")
```

**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`.

- Previous: [Test migrations on a copy of production data](https://agentwranglers.se/playbook/data/migrations/test-migrations)
- [Migrations](https://agentwranglers.se/playbook/data/migrations)
