04 — Scripts and databases
Commandments IV and V: Dry-run — A “dry” run — the script shows what it WOULD do but changes nothing. You see the effects before executing; a data change happens only after deliberate confirmation. more in the glossary → is the default; the Backup — A copy of your data saved earlier, which you return to when something goes wrong. It’s the only way to undo a change to data — code comes back from git, data only from a backup. more in the glossary → is the Rollback — Reverting a change to the previous, working state — “Ctrl+Z” for a deployment. When a new version breaks production, a rollback restores the previous one in seconds instead of fixing in a panic. more in the glossary →.
Scripts
- Dry-run by default. A data-changing script prints the plan (what, how many rows, where); a deliberate
--executewrites. - Idempotency — A property of an operation you can run many times with the same result — no duplication. Key for scripts and events: a re-run doesn’t break data. Like an “ON” switch. more in the glossary →. Running it twice yields the same state (UPSERT — An “insert or update” write: if the record exists it’s updated, if not it’s added. It means a script run twice doesn’t create duplicates. more in the glossary → /
UPSERT). - Print what you did not do — truncation, sampling, skipped rows. Silence reads as “covered everything”.
- Catalog-mutating operations stay out of automatic Pipeline — A chain of processing steps run in order, e.g. fetch → clean → enrich → check. When each step runs on its own and repeats safely, a failure halfway doesn’t mean starting from zero. more in the glossary → (dedup, Merge — Combining the changes from one branch into another. A merge brings in every commit from the source branch — other people’s too — so first you check what exactly comes in. more in the glossary →): dry-run → review →
--executeby hand. - The environment is written in
CLAUDE.md— interpreter, variables, venv. The agent doesn’t guess. - Absolute paths when a value crosses tools.
/tmpmeans one place to Git Bash, another to Python — A popular, readable programming language — The Craft’s default for scripts, data and the back end. A proven default: lots of ready tools and libraries, easy to drive with AI. more in the glossary → on Windows, a third to PowerShell — each tool then reads a different file. Pass fully-qualified paths; when a run “did nothing”, printos.path.abspath(p)/pwd -Wand compare.
Databases and migrations
- Forward-only Migration (database) — A controlled change to the database layout — adding a column, a table or moving data — step by step. Like a renovation to plan: rebuilding data in a set order so nothing “collapses”. more in the glossary →. No down-migrations; rolling back the schema = restoring the backup.
- Back up before every schema/data change on Environment (dev / prod) — Separate places where the app runs: “dev” for safe tests, “prod” for real users. Dev is a playground, prod is where real users are — the rules for each are completely different. more in the glossary →, with a timestamp and a retention window:
sqlite3 data/app.db ".backup backups/pre-deploy-$(date +%F_%H%M%S).db" - Snapshot — A saved state of data from a specific moment — e.g. a database copy or a file of computed statistics. It speeds things up and keeps them safe, but is stale from the moment it’s taken — you need to know where fresh data comes from. more in the glossary → with
.backup/VACUUM INTO, nevercp/scpof a live database — WAL — Write-Ahead Log: a database mode where changes go to a journal first, then to the data. Gives safety (recovery after a crash) and better read/write concurrency. more in the glossary → makes the copy inconsistent. - Additive before destructive.
ADD COLUMNis safe;DROP/RENAMEwaits for a later Deploy — Releasing a new version of the app “live”, where real people use it. It’s the moment a change reaches real people — which is why it happens only on an explicit “deploy”. more in the glossary →, once no running code reads the column. - Check integrity after every data operation:
integrity_check,foreign_key_check, row counts. Tell pre-existing noise (present before and after) from a regression (new violations). - Rehearse a bulk or irreversible mutation on a fresh prod copy — a dry-run shows the plan, not the
outcome.
.backupprod → run on the copy → check counts, integrity and the tricky cases → then prod. The deploy-level sibling of this rule → 18.
Anti-patterns
- 🚫 A mutating script without
--dry-run. - 🚫 A migration deployed without a backup “because it’s additive”.
- 🚫 Dropping a column running code still reads.
- 🚫
cp app.dbwhile the server runs. - 🚫 A catalog-mutating step wired into the automatic pipeline.