19 — Database swap runbook
Commandment VII at its hardest: replace the 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 → database wholesale (e.g. a rebuilt catalog) while keeping every live account and everything users created. Rare — and where most traps are.
Rules that save user data
- “Users” is many tables. Enumerate every table with a Foreign key (FK) — A column that points to a record in another table — e.g. a review points to its user. It prevents “dangling” references to things that don’t exist, and sets the order of operations. more in the glossary → to the user (accounts, carts,
reviews, likes, badges, sessions, chat sessions + messages + limits, feedback…). A table without
user_idis mapped through its parent. Gate each on the table/column existing. - Map by a stable key (Slug — A readable, short part of a page address that describes its content in words instead of a mysterious number. Better for humans and SEO; a stable slug doesn’t break links when things change. more in the glossary → → new id), never by the old id. A missing slug is skipped and logged.
- The source of truth for accounts is live prod, read after
stopinside the Maintenance window — A short, planned period when users see a “work in progress” page while the database or configuration changes behind it. It guarantees the app never runs, even briefly, on half-changed data. more in the glossary → — not an older 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 → that loses the last hour’s registrations. - FK-safe order: delete children before parents, insert parents before children.
- Back up live prod right before the Database swap — Replacing the whole production database with a new version, carrying over user accounts and data. The hardest operation in The Craft — it has its own runbook and needs explicit consent. more in the glossary → (
.backup replaced-prod-<ts>.db) — that 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 →. - Verify the merged database: account count = prod, 0 dangling references,
integrity_check: ok, FK violations not above the pre-existing level. - Sessions live in their own store (e.g.
sessions.db), never in the swapped database and never in process memory — otherwise the swap or any restart logs everyone out (→ 14).
Sequence
- Live phase: Push / pull — Push sends your commits to the remote repository; pull fetches changes from there into your copy. A push is an “outward” action — in The Craft it needs explicit consent, like a deploy. more in the glossary → the code; copy git-ignored assets before the window (rsync / tar over SSH — An encrypted connection to a server for sending it commands and transferring files. An SSH key is access to production — guard it like your most important password. more in the glossary →, only
the delta) — they don’t ride with
git pull; upload the new snapshot to the server. - Window (seconds, atomic,
set -e): flag ON → pull → stop → back up live prod → migrate users (source = prod) → swap the file (remove-wal/-shm, move, re-enable 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 →) → start → smoke → flag OFF. - Smoke via localhost — The address a computer (or server) uses to connect to itself. On a server it lets you check the new version “from inside” before users see it. more in the glossary → (bypasses the maintenance page): key pages 200, redirects, row counts, process logs clean. A trivial, safe bug found in the window is fixed in the window — don’t release a known 500.
- After: tag the 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 →, clean temp files, write the lessons into the Runbook — A written step-by-step guide for production operations, with numbered lessons from incidents. The memory of outages can’t live in someone’s head — the next deploy starts by reading the runbook. more in the glossary → (→ 05).
Anti-patterns
- 🚫 A swap “except accounts” that only keeps the
userstable — reviews, badges and chat are lost. - 🚫 Mapping user data by id — reviews land on the wrong product.
- 🚫 Migrating accounts from an old snapshot.
- 🚫 Moving gigabytes of assets inside the window.
- 🚫
scpof a live.db— use.backup; WAL holds the fresh pages.