Data model
One SQLite file, WAL mode, eight tables. Every table is STRICT, so a column
typed TEXT cannot quietly accept an integer.
The consistent theme below is that invariants live in the schema. A CHECK
constraint holds against a migration, a console action, a test fixture and a
hand-edit in sqlite3 alike, which is not true of a validation in a route
handler.
users
| Column | Notes |
|---|---|
subject | PK. The opaque identifier every app keys its own rows on. Never derived from anything. |
username | As typed, for display. |
username_folded | UNIQUE. What login looks up. Folding is what stops Alice and alice being two accounts. |
password_hash | scrypt, per-password salt. |
email | Nullable — an account need not have one. |
email_verified | 0/1. |
disabled_at | Set rather than deleted, so a disabled account keeps its audit trail. |
CHECK (email_verified = 0 OR email IS NOT NULL)
An unverified address is fine; a verified NULL is a bug that would let a password-reset flow trust an address that was never collected.
The UNIQUE on username_folded already provides login's index. The separate
partial index on email serves the console's "who has an unverified address"
and registration's "is this address already spoken for".
apps
| Column | Notes |
|---|---|
slug | PK, lower case, enforced. |
name | Display name. |
public_registration | 0/1. Only prm has it on. |
baseline_role | What a public registrant becomes. |
oidc_redirect_uris, oidc_post_logout_redirect_uris | JSON arrays. [] means the app is not an OpenID Connect client. |
oidc_jwks | The app's public keys as a JWK Set, for its client assertions. Never a secret. |
CHECK (slug <> '' AND slug = lower(slug))
CHECK (public_registration = 0 OR baseline_role IS NOT NULL)
The first stops Atrium and atrium becoming two apps that look like one —
slugs appear in grant rows, in URLs, and in six apps' configuration. The second
makes it impossible to open signup without saying what a new registrant
receives, which would otherwise be an open door onto an undefined authority.
grants
PRIMARY KEY (subject, app_slug, role)
The security boundary, described in full on Grants. Both foreign keys cascade on delete.
The composite primary key's leading column is subject, so the hot lookup —
"every role this person holds", asked by /session-status and the OIDC login
step — is already served. The extra index on
(app_slug, role) serves the other direction, which is the console's: everyone
who can reach a given app.
refresh_tokens
One row per issued token. This is where rotation and reuse detection live.
| Column | Notes |
|---|---|
token_hash | PK. sha256 of the token. The plaintext is handed out once and never stored. |
family_id | The unit of revocation. The access token's sid claim names it. |
used_at | NULL until spent. This is the single-use guarantee. |
revoked_at / revoked_reason | One of rotated, logout, reuse_detected, admin, expired. |
CHECK ((revoked_at IS NULL) = (revoked_reason IS NULL))
A reason without a revocation is a writer that set one and forgot the other, and the pair is what the console renders.
The expires_at index exists because a 30-day token nobody ever presents again
is otherwise immortal — expired rows are swept on a schedule rather than left to
accumulate.
See Sessions and tokens for why the claim on used_at must stay a
single statement.
oidc_payloads and oidc_authorizations
Added by the 20261013000000-oidc migration for Sign in with Ward.
Times here are epoch seconds, because oidc-provider works in them.
oidc_payloadsis the provider's storage: codes, interactions, grants and replay markers, keyed(model, id), with the payload as checked JSON. It survives a restart mid-flow, which the provider's in-memory default would not.oidc_authorizationsbridges a provider grant to Ward's own session: the client, the subject, the refresh family (family_id, which becomesward_sid) and the role (ward_role). Both foreign keys cascade.
app_keys is goneApp keys authenticated apps to the retired /introspect. Migration
20261013120000-drop-app-keys dropped the table on 2026-10-13. The
app_key.* rows in audit_log stay as history.
verification_tokens
| Column | Notes |
|---|---|
purpose | email_verify or password_reset. |
email | The address the token was sent to — recorded, not looked up later. |
token_hash | UNIQUE. |
consumed_at | Single-use, same shape as refresh tokens. |
audit_log
| Column | Notes |
|---|---|
actor_kind | superuser, account, or system. |
actor_subject | Only ever set for account. |
target_kind | user, app, grant, session, token, or NULL. |
CHECK ((actor_subject IS NOT NULL) = (actor_kind = 'account'))
CHECK ((target_id IS NULL) = (target_kind IS NULL))
The first is the schema refusing to let a superuser or system row claim a
subject. Only an account has one, and a row claiming otherwise would assert that
an account exists for an identity that has none — exactly the confusion
decisions-admin exists to prevent.
The default console view is newest-first, indexed on (at DESC, id DESC). The
id DESC gives the same order as at DESC without depending on clock
monotonicity — worth copying anywhere else this pattern appears.
Backups
ward.db copy as a backupThe database runs in WAL mode, which means committed data lives in the -wal
sidecar until a checkpoint. Copying ward.db alone can produce a file that is
missing recent commits while looking perfectly valid.
Back up all three files together — ward.db, ward.db-wal, ward.db-shm — or
checkpoint first. Ward checkpoints on clean shutdown for exactly this reason;
see decisions-implementation.
Migrations
Statically imported from api/src/db/migrations/index.ts and run to completion
before the listener binds. Four exist, in order: the baseline, the one that
added app_keys, the OpenID Connect tables, and the one that dropped
app_keys again.
Static imports rather than a directory scan means the set of migrations is fixed at build time — a file that failed to copy into the container is a compile error, not a database that silently skips a step.