Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Level 3 · Codex ~2 min read

In one sentence: Swapping the database live, step by step — so not a single account, cart or review is lost.

This chapter in plain terms

The hardest form of Commandment VII: you 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 → the whole 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 (e.g. a rebuilt catalogue), and every account and everything users created has to survive. It’s done rarely — which is exactly why most of the traps live here.

First: “users” isn’t one table. The account is only the start — there are carts, reviews, likes, badges, sessions, chat history. Every table that points at a user has to be listed and carried over. Second: user data is pinned to a stable identifier (a 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 →), not to a number that already means something else in the new database. Third: the source of accounts is live production at the moment of the swap, not yesterday’s copy, which loses the last hour’s sign-ups.

The swap window itself takes seconds and is a script: turn on 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 → → stop the app → back up the live database (that’s the way back) → carry over user data → swap the database file → start → check the key pages → turn maintenance off. Afterwards the account count matches production and there isn’t a single dangling reference. Login sessions live in a separate store — otherwise the swap logs everyone out.

Example: a swap “keeping accounts” carries over only the users table. Everyone can log in — and nobody has their reviews, badges or conversations. Or mapping by number instead of slug: the review “great bike” lands under a washing machine.

Read more: foreign key · SQLite — backing up a live database (.backup).

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

  1. “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_id is mapped through its parent. Gate each on the table/column existing.
  2. 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.
  3. The source of truth for accounts is live prod, read after stop inside 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.
  4. FK-safe order: delete children before parents, insert parents before children.
  5. 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 →.
  6. Verify the merged database: account count = prod, 0 dangling references, integrity_check: ok, FK violations not above the pre-existing level.
  7. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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 users table — 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.
  • 🚫 scp of a live .db — use .backup; WAL holds the fresh pages.
Chapter deep-dive 19Why these rules, examples and stories from projects — what the core deliberately leaves out.

The canonical doctrine is written in English.