Skip to content

0014. D1 data model conventions ​

  • Status: Accepted
  • Date: 2026-11-30

Context ​

The Worker needs one database for entries, envelopes, settings, the matching tables and the reconcile history. Money rules live in packages/core; the database must hold what they read and refuse what they can't use.

Decision ​

  • Schema and migrations: Drizzle schema in apps/worker/src/db/schema.ts; SQL migrations generated by drizzle-kit into apps/worker/migrations/ and applied by Wrangler (and, in tests, by the Vitest setup file). A guards migration adds triggers, which drizzle-kit can't express.
  • Money, flags and dates: integer cents within the safe range, with a CHECK (SQLite would otherwise keep 12.5 in an INTEGER column); booleans CHECKed to 0 or 1; dates as 'YYYY-MM-DD' text, moments as ISO UTC text, both CHECKed.
  • Nothing is deleted: rows carry archived_at. Triggers refuse DELETE on every history table, and the audit and pull logs refuse UPDATE too. SQLite's INSERT OR REPLACE deletes the old row without firing DELETE triggers (D1 doesn't enable recursive triggers), so a second set of triggers refuses any insert that collides with an existing id or unique key; ids and the key + date of settings and weekly amounts never change. Two tables may be replaced on purpose: settings and job_health. The Worker never uses REPLACE or upserts (a test greps its source). Foreign keys are NO ACTION, never CASCADE or RESTRICT. entry_parts and roll_state are working data and can be deleted.
  • Computed, never stored: locks (an entry stores only the confirmed flag reconcile sets), balances other than the bank's last reported ones, grid layout, envelope placeholders.
  • Fixed choices (entry status and source, movement kinds, roles…) are CHECKed against lists in enums.ts that the core types constrain at compile time. A new choice is a new migration.
  • Settings are effective-dated rows (key, effective_from); a change is a new row. Small household facts are Settings rows, not columns or tables: the admin's phone number (admin_phone) and other spellings of note tags (tag_aliases, JSON) are rows. The page data only ever gets the ready-made text-message link, never the number.
  • Optional facts about a person are nullable columns with no CHECK where the app owns the list: people.avatar holds one of the built-in avatar keys (never an image), and a key the app doesn't know reads as empty (initials).
  • Seed: the Addams household from packages/mocks, as a function for tests and as SQL for the local database. No route in the Worker seeds anything.
  • D1 limits that shaped this: 100 bound values per statement (the seed batches), and GLOB / LIKE patterns of 50 bytes at most (the timestamp CHECK is built from short pieces). Tests cover both.

Consequences ​

  • The database refuses bad rows even if a route forgets to check; the Worker still validates first for good errors.
  • Changing a CHECK (or a fixed list) means a table rebuild migration in SQLite.
  • Hard deletes need a deliberate migration, not a stray query.
  • A migration that rebuilds a table drops its triggers: it must recreate them.
  • Triggers can't stop someone who drops them; they stop a stray query, not an attacker with schema access.

Sources ​