Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Rules of chapter 11
Level 3 · Codex Chapter deep-dive

In the core the rules are short and hard. Here is what stands behind them: the why, examples and stories from real projects. Rule quotes come straight from the core.

11 — Data model and normalization

A database schema is the contract of the whole system: data-fetching scripts, the website, background processing and 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 → all rest on it at once. A badly shaped model takes revenge on every layer — copies of data drift apart, links on an unstable key lose records, and computed values stop agreeing. So you normalise first, and depart from normalisation deliberately, with a named source of truth. Below is what stands behind the rules.

A vocabulary instead of free text

Controlled vocabularies live in lookup tables ↗

A free-text “country” column contains, after a year, “USA”, “U.S.A.”, “United States” and “usa ” with a trailing space — and a “products from the USA” filter finds a quarter of them. The fix: vocabulary values live in separate tables (in the reference project: countries, regions, product types, tag types), and records only refer to them. Where the list is fixed and short, a CHECK constraint is enough (e.g. a product type from a closed list) — the database rejects junk before it even gets in.

Many-to-many goes through a junction table ↗

A product has many tags, a tag has many products. The tempting shortcut is two columns, “main tag” and “secondary tag” — until the day a product needs a third, and the query “all products tagged X” has to search both columns. In the reference project those two columns were removed by a migration, and a table joining product and tag became the only place attributes are stored.

A slug survives a merge, an id doesn’t

The stable identifier is the 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 the numeric id. ↗

Numeric identifiers drift. When two duplicate products are merged, one takes over the other’s data and the other disappears — along with its id. A copy of the database made a week ago already has different ids from 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 →. A slug (a readable, stable identifier, e.g. coffee-ethiopia-yirgacheffe) survives such operations.

The reference project’s hardest lesson comes from 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 production database: user data (reviews, favourites) has to be carried over to the new database. Mapping by the old id would attach a review to the wrong product — silently, with no error at all. So user data is mapped by slug to the new id, and a record whose slug doesn’t exist in the new database is skipped and logged rather than squeezed in somewhere (19).

History instead of overwriting

Active row instead of overwrite where history matters: ↗

A pattern from prices worth carrying anywhere history matters: instead of an in-place UPDATE of the price, the old row gets an expiry date (expired_at) and a new row is inserted. A unique Index (database) — A lookup in the database that makes searching instant instead of scanning everything in order. The first move for slow queries — like an index at the back of a book instead of reading 400 pages. more in the glossary → limited to active rows (WHERE expired_at IS NULL) ensures exactly one active price per product–shop pair. The result is auditing for free: you know what changed and when, with no extra triggers — and you can draw a price-over-time chart.

One truth for every fact

Computed values are computed, not stored ↗

A product’s display name is built from brand, variant and edition. Stored as a separate column, it drifts from its parts at the first change to any of them — and nobody notices, because both values “look fine”. In the reference project that name is computed in one helper every time it’s displayed.

A Cache — A temporarily remembered result, so the same thing isn’t computed again on every request. Speeds the app up, but can be a trap: a stale cache shows old data. more in the glossary → or 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 → names its source of truth ↗

Denormalising for faster reads is fine — as long as everyone knows which copy is the real one. Two examples from the reference project: data from an external source is kept only in a separate cache table (columns holding the same data were removed from the products table, so there aren’t two truths), and the numbers summary on the home page is a snapshot file that falls back to the live database when it’s stale. One fact in two places without synchronisation guarantees they’ll eventually differ.

Migrations that survive different environments

Migrations are forward-only and additive ↗

The rules from 04 — forward only, add first, remove only in 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 → — get one more element here: a script checks whether a column or table exists before acting on it. The local database, the Preprod (staging) — A faithful copy of production with no real users, where you rehearse a deploy. A migration that fails on preprod is an ordinary Tuesday; in production it’s an incident. more in the glossary → copy and production are often in different schema states; a script that checks works in each of them instead of falling over halfway. After every data operation — foreign_key_check, so no orphaned references are left behind.

The schema is documented: ↗

A relationship diagram (e.g. in Mermaid, rendered straight from markdown) and a list of allowed values for every vocabulary field, in docs/. A schema nobody described gets reconstructed from the code by the next session, which then guesses — and guessing the allowed values of a type column ends in a new, fourth spelling of the same thing (01).

Privacy built into the model

Declared privacy is enforced by the model: ↗

When the policy promises “we don’t read your conversations”, that promise has to follow from the system’s structure, not from goodwill (09). In practice: user-facing data and sensitive processing (e.g. drawing insights from conversations) are two separate paths. The processing is a separate, 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 → script that receives data already anonymised — PII (personal data) — Information that identifies a person: name, email, address, phone number. They need special protection (GDPR) — they mustn’t end up in logs or on test servers. more in the glossary → is removed before anything processes it. Plus a test proving that identity can’t be reconstructed from the output. Then the statement in the policy describes the architecture rather than expressing a hope.