canopy-web Data Model

For a contributor reading or migrating canopy_web: the tables that exist at runtime, where each comes from (a migration file or boot-time DDL), and the retention posture. Routes live on canopy-web API Reference; source: migrations/.

Tables

The schema in effect is the migrations applied in order plus one piece of boot-time DDL, the fleet idempotency table, listed because it exists in the database although no migration file names it.

Table Purpose

web_session (migration 20270201000000)

The live worker web sessions on canopy-identity (ADR-009 Amendment 2 — never MemoryStore; ADR-049 Amendment 18). One row per live session. id UUID PRIMARY KEY (v7, bound by the application; a handle, never a bearer); token_hash BYTEA NOT NULL UNIQUE (CHECK octet_length = 32): the SHA-256 of the 256-bit random token the __Host-canopy_session cookie carries, so a dump yields no usable cookie; csrf_token TEXT NOT NULL, minted with the row and never rewritten; created_at (default now()); idle_expires_at TIMESTAMPTZ NOT NULL, the sliding idle deadline (session_ttl_seconds, 0 = 8 hours, floor 300 s, read at boot; a request slides it when less than the deadline less a minute is left, never backwards); version BIGINT NOT NULL DEFAULT 1 (CHECK >= 1), one step per committed maintenance; claim_id UUID NULL and claim_expires_at TIMESTAMPTZ NULL, both NULL or both set (web_session_claim_complete): the maintenance claim canopy-auth’s WebSessionStore contract states, leased on the database clock and committed before the upstream refresh token is presented; record JSONB NULL, the WebSessionRecord (canopy-identity’s federated session — its credential, sid, sub, times, roles, programs and username — and the upstream grant that keeps it fresh), plaintext as the grant it replaced was (field encryption is #2521), and read as no session when NULL or undecodable; studio_draft JSONB NULL, the Studio onboarding draft. Every write is one named statement over its own columns (services/canopy-web/src/web_session/sql.rs); only a completed sign-in or step-up inserts a row, and a deleted row is final. A load filters idle_expires_at > now().

web_native_session (migration 20270201000000)

One row per device a worker approved: canopy-cli’s native session (ADR-049 Amendment 21). id (v7); subject TEXT NOT NULL (CHECK <> ''), the approving worker’s canopy subject; created_at; version, claim_id / claim_expires_at and record as on web_session; due_at TIMESTAMPTZ NOT NULL, which mirrors the record’s maintain_at on every commit, so the sweep lists due rows from a column, and still lists a row whose record no longer decodes, to end it. No request serves a native session.

web_login_flow (migration 20270201000000)

One row per sign-in, step-up or device approval in flight, written by /login/start or /auth/step-up and taken once by /auth/callback. id (v7); flow_hash BYTEA NOT NULL UNIQUE, the SHA-256 of the __Host-canopy_login_flow cookie’s token; state_hash BYTEA NOT NULL, the SHA-256 of the 43-character OAuth state (the state itself is never stored: the callback’s returned state rebuilds the relying party’s pending authorization); kind (CHECK in sign_in, step_up, device_approval); nonce, pkce_verifier; max_age_secs INTEGER NULL, a step-up’s max_age; prompt_login BOOLEAN NOT NULL; fresh_retry BOOLEAN NOT NULL, set on the one restart a stale upstream sign-in gets; return_to TEXT NULL, already validated; replaces_session_id UUID NULL, the session a sign-in or step-up replaces, or the approving worker’s session for a device approval (device.rs), which the callback serves to complete it; user_code TEXT NULL, NOT NULL exactly for a device approval (web_login_flow_user_code_by_kind); expires_at (15 minutes out). The callback’s take is one DELETE … WHERE flow_hash AND state_hash AND expires_at > now() RETURNING: single use, and a wrong state consumes nothing. Sign-out deletes the browser’s pending row.

web_session_end (migration 20270201000000)

Why each web or native session ended: a diagnostic record holding no live credential. session_id UUID PRIMARY KEY; kind (CHECK in web, native); token_hash BYTEA NULL, set exactly for a web session (web_session_end_token_by_kind); subject; created_at; ended_at (default now()); reason (CHECK in canopy-auth’s WebSessionEndReason nine: signed_out, expired, identity_refused, upstream_ended, upstream_changed, refresh_lost, superseded, idle, operator); superseded_by UUID NULL (the session a sign-in or step-up replaced it with). Written by the same data-modifying CTE that deletes the session row, so no session ends unlogged; the operator’s end uses that statement too, never TRUNCATE. A sign-out whose session was already superseded follows superseded_by from the end row with its cookie’s token_hash, up to four links, to end the session that replaced it.

composition_documents (ADR-022)

Override storage for the composition runtime (ADR-021). One row per (jurisdiction_id, layer, scope_key, surface); patch_ops is the override body — an RFC 6902 JSON Patch op list for case_detail / sign_in, or a user_delta_v1 envelope for the three dashboard surfaces (ADR-024). The three DB-backed layers (user delta, role override, jurisdiction_live) share this table; the loader fetches all layers in one indexed query and replays them over the jurisdiction TOML baseline in jurisdiction_live → role → user order. created_by (TEXT NOT NULL, since 20270113000000) is the writing worker’s OIDC subject, stored verbatim; a user-layer row’s scope_key is the same subject; created_at / updated_at default to clock_timestamp(); id has no default since 20260805000000 (UUIDv7 bound at insert). Two ENUM types back the constrained columns: composition_layer (user / role / jurisdiction_live) and composition_surface (worker_dashboard / supervisor_dashboard / analyst_dashboard / case_detail / sign_in).

composition_documents_archive (ADR-022)

Explicit-archive history for promoted or superseded overrides. Created LIKE composition_documents INCLUDING DEFAULTS INCLUDING IDENTITY (columns and defaults only) plus archived_at (default clock_timestamp()) and archived_by. archived_by and the inherited created_by are both TEXT NOT NULL OIDC subjects since 20270113000000 (LIKE copied the column type once, so the archive’s created_by is retyped by its own ALTER). It deliberately inherits neither the UNIQUE (jurisdiction_id, layer, scope_key, surface) constraint nor the lookup index, so the same tuple can be archived more than once over a jurisdiction’s lifetime (ADR-022 schema note); its inherited id default was dropped by 20260805000000.

event_outbox / event_inbox

Generated ADR-018 / ADR-039 outbox and inbox (eight files in crates/canopy-mq/outbox-migrations/, cargo xtask outbox-migrations --write); columns, indexes, parking and hold semantics are documented once on the data-models index.

idempotency_keys (runtime DDL)

The fleet idempotency cache, created before the migrator runs by canopy_db::ensure_idempotency_schema (canopy_api::bootstrap::apply_migrations_on; canopy-web boots through canopy_api::bootstrap_with in main.rs, and canopy-manage migrate apply makes the same call). Shape owned by crates/canopy-db/src/idempotency_schema.rs.

_sqlx_migrations (sqlx’s history table) is present as in every service.

Relationships

No table here carries a foreign key. composition_documents_archive mirrors composition_documents by LIKE derivation, not by FK. web_login_flow.replaces_session_id and web_session_end.superseded_by name web_session ids by value only, and web_session_end.session_id names a web_session or web_native_session id: the session they name may already have ended.

Diagram

Cross-service FKs (ADR-001 boundary)

canopy-web holds no Postgres-level foreign key to any other service. The columns that name identities elsewhere are application-level only: composition_documents.jurisdiction_id (a UUID, jurisdiction config) and created_by / archived_by (TEXT OIDC subjects — workers are not canopy-persons rows); web_session.record's sub, web_native_session.subject and web_session_end.subject are the worker’s canopy subject (ADR-049), canopy-identity’s id for the worker, not an upstream’s sub. The session store holds token hashes and a worker’s own federated session, not person or household ids. canopy-web reaches every other service over HTTP.

Retention

  • web_session — transient; the 8-hour sliding idle deadline above; and the engine ends it, logged expired, once it can no longer be kept fresh, at the latest at the federated session’s own end (session_ends_at) (ADR-009 records the canopy-web figure as 8 hours). A row past its deadline neither loads nor accepts writes, and the next pass of the session sweeper (services/canopy-web/src/web_session/sweep.rs, #2519; every 5 minutes on every replica, Deployment Guide › Worker-session sweep) ends it, logged idle. No regulatory floor.

  • web_native_session — until the session ends, logged in web_session_end like a web session’s. No regulatory floor.

  • web_login_flow — at most its 15-minute lifetime (FLOW_LIFETIME); the sweeper deletes a row past it. No regulatory floor.

  • web_session_end — 30 days from ended_at (END_LOG_RETENTION), then deleted by the sweeper. The readers that decide something need a row for far less: a replacement’s sign-out check within a flow’s lifetime, a sign-out’s superseded_by walk while a concurrent sign-in completes, and the engine’s end_reason read within a claim’s lease; the rest is diagnostics. No regulatory floor.

  • event_outbox / event_inbox — the janitors documented on the data-models index. No regulatory floor.

  • composition_documents / composition_documents_archive — every override write emits a JWS-signed AuditEvent (ADR-014) under the uniform 1-year override-layer retention of ADR-022 › Audit retention. Archiving is the explicit Studio promote-merge, never a destructive migration.

canopy-web holds no FTI or PHI; no Pub 1075 or HIPAA floor applies at this layer.

Indexes

  • web_session_idle_expires_at_idx — the idle sweep; token_hash is UNIQUE. The operator’s per-worker end reads record →> 'sub' without an index: an operator action over a table of live sessions

  • web_native_session_subject_idx, web_native_session_due_at_idx — a worker’s devices, and the sweep’s due listing

  • web_login_flow_expires_at_idx — expired-flow cleanup; flow_hash is UNIQUE

  • web_session_end_token_hash_idx, web_session_end_ended_at_idx — the sign-out’s superseded_by walk, and end-log retention

  • composition_documents_lookup_idx — (jurisdiction_id, surface, layer, scope_key); the loader’s single-query fetch of every override layer for a surface

  • composition_documents_archive_lookup_idx — (jurisdiction_id, surface, archived_at DESC); most-recent-first history per jurisdiction and surface

  • UNIQUE (jurisdiction_id, layer, scope_key, surface) on composition_documents — one live override row per layer tuple (not inherited by the archive)

  • idx_idempotency_keys_created_at — runtime DDL beside its table (crates/canopy-db/src/idempotency_schema.rs)

  • event_outbox_* / event_inbox_* — the generated partial indexes, documented once on the data-models index

Migration files

The eight generated outbox/inbox files (20260508000000, 20260516000000, 20260518004851, 20260713000000, 20260720000000, 20260803000000, 20260815000000, 20260904000000) are documented once on the data-models index.

  • 20260401000000_create_sessions_table.sql — sessions + sessions_expiry_idx, which no code ever used; dropped by 20270114000000

  • 20260522002115_create_composition_documents.sql — ADR-022 (#489): the composition_layer + composition_surface ENUMs, composition_documents, composition_documents_archive, the two lookup indexes

  • 20260805000000_v7_pk_defaults.sql — #1173: drops the gen_random_uuid() id defaults on composition_documents and composition_documents_archive (the archive had inherited the parent’s default through LIKE … INCLUDING DEFAULTS); ids are bound as UUIDv7 at every insert

  • 20270113000000_composition_actor_subjects.sql — #1008: retypes composition_documents.created_by and the archive’s created_by / archived_by from UUID to TEXT NOT NULL in one migration (archive_document's INSERT … SELECT copies created_by across the two tables, and there is no text-to-uuid assignment cast); a stored UUID becomes its text. A guard then refuses the migration, naming cargo xtask dev restart, if any of the three holds the nil UUID, which is no one’s subject

  • 20270114000000_web_session_store.sql — #2518: drops the unused sessions table and creates web_session, web_login_flow and web_session_end with their indexes (ADR-009 Amendment 2). The tower_sessions schema the old store created at boot is left for the operator to drop (Deployment Guide), since schema DDL does not belong in a service migration

  • 20270115000000_web_session_refresh_claim.sql — #2224: the Keycloak-era refresh claim columns; replaced with their table by 20270201000000

  • 20270201000000_identity_web_session.sql — drops the Keycloak-era web_session, web_login_flow and web_session_end outright and creates web_session, web_native_session, web_login_flow and web_session_end on canopy-identity, with their indexes. Every live session, pending sign-in and Studio draft is dropped; pre-1.0, workers sign in again

All migrations are forward-only per ADR-016.

Edit this page · default