agentleFS
yaohuangguanyaohuangguan/agent-playbook/skills/change-data-safely/SKILL.md

change-data-safely

Skill1 starsChanged 2 days agoMITView on GitHub

Change a database schema or stored data without breaking running code or losing data. Use this for any migration, new or changed column, backfill, bulk update, delete, index, or change to a stored format, including in small projects. Data mistakes are often impossible to undo.

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Read the current state: Read the schema and existing migrations.

    line 17
  2. Distinctive:

    Test it: Run the migration on a copy of the schema with realistic old rows.

    line 41
  3. Distinctive:

    Run all migrations from empty too.

    line 42
  4. Distinctive:

    Plan recovery: Write down how to undo each step.

    line 44

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Change data safely

Code can be rolled back, but lost data often cannot. During a deploy, old and new code run against the same data at the same time, so each step must work for both.

When to use

Any change to stored data or its shape.

Steps

  1. Read the current state. Read the schema and existing migrations. Find every reader and writer of the affected data.
  2. Ask what already exists. Think about rows with nulls, duplicates, old formats and very large tables. Test against these, not only fresh data.
  3. Split risky changes into stages.

    StageActionSafe because
    ExpandAdd the new column or table, nullableOld code ignores it
    Write bothNew code writes old and newEither version can read
    BackfillCopy old rows in small batchesCan stop and resume
    Read newSwitch reads to the new fieldOld data is still there
    ContractRemove the old field, in a later releaseNothing uses it now

    Renaming a column in place breaks whichever code version is not expecting it.

  4. Make each step safe to rerun. A migration or backfill can stop halfway. Running it twice must not corrupt data.
  5. Consider locks and size. Adding an index or rewriting a large table can block production. Use the database's online option, and process rows in batches.
  6. Test it. Run the migration on a copy of the schema with realistic old rows. Run all migrations from empty too. Confirm that row counts and values are right.
  7. Plan recovery. Write down how to undo each step. If a step cannot be undone, say so. Confirm that a recent backup exists and can be restored.
  8. Get approval before production. Show the exact statements, the affected row count and the recovery plan. Do not run them against production without explicit approval for that specific action.
  9. Check the result. Compare counts and sample rows. Then follow verify-and-report.

Stop and ask when

  • The change deletes or overwrites data, or removes a column or table.
  • You are unsure which environment a connection points to.
  • There is no tested backup.

Common mistakes

Tempting moveWhy it failsDo this instead
Rename a column in one stepBreaks old or new codeExpand, migrate, contract
Add NOT NULL with no defaultFails on existing rowsAdd nullable, backfill, then constrain
UPDATE or DELETE without a previewCan hit every rowRun a SELECT with the same filter first
Update one huge batchLocks the tableUse small batches
Test only on an empty databaseMisses real dataInclude old-format rows
Edit a migration that already ranEnvironments drift apartAdd a new migration

Report

Change: <schema or data change>
Affected: <tables and row counts>
Compatibility: <how old and new code both work during deploy>
Tested on: <data used, and the result>
Recovery: <how to undo, or why it cannot be undone>
Production: <not run / run with approval, and the result>

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.