Files
Convivo/docs/backend/schema.md
T
Lago d4e4637e7c feat(sessions): material downloads unlock when the session ends
Private materials bucket + RLS read gate (admins/own speakers anytime, all
attendees after the scheduled slot end, event-local Amsterdam time); always
visible uploaded-state, locked download panel, authenticated client downloads,
admin signed URLs. 12 new assertions in material_time_lock_tests.sql.
2026-10-21 18:57:15 +02:00

376 lines
19 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Database Schema
All tables live in the `public` schema. Source of truth:
[`supabase/migrations/20260824000000_redesign.sql`](../../supabase/migrations/20260824000000_redesign.sql)
(the Redesign migration, which **replaces** the old `profiles`-based schema).
There are **13 tables**: people (`contacts`), the event (`events`), participation
(`registrations`, `sponsors`), the schedule (`slots`, `sessions`,
`session_speakers`), and the event's activity (`preferences`, `connections`,
`checkins`, `feedback`, `leaderboard`, `points_log`).
The old `profiles` table is **dropped**. People are now `contacts` (keyed by
email), and every activity table is scoped to an **event** + a **contact email**
instead of a single `profile_id`.
```mermaid
erDiagram
contacts ||--o{ registrations : "participates"
contacts ||--o{ session_speakers : "speaks"
contacts ||--o{ preferences : "picks"
contacts ||--o{ connections : "from/to"
contacts ||--o{ checkins : "checked in"
contacts ||--o{ feedback : "rates"
contacts ||--o{ leaderboard : "mirrored"
contacts ||--o{ points_log : "audited"
events ||--o{ registrations : "has"
events ||--o{ sponsors : "has"
events ||--o{ slots : "has"
events ||--o{ sessions : "has"
events ||--o{ preferences : "scopes"
events ||--o{ connections : "scopes"
events ||--o{ checkins : "scopes"
events ||--o{ feedback : "scopes"
events ||--o{ leaderboard : "scopes"
events ||--o{ points_log : "scopes"
slots ||--o{ sessions : "contains"
sessions ||--o{ session_speakers : "has speakers"
sessions ||--o{ preferences : "picked"
sessions ||--o{ checkins : "checked into"
sessions ||--o{ feedback : "rated"
```
## `contacts` — people (source of truth)
One row per person. **`email` is the primary key** (a natural key that matches
`auth.users.email`). Contacts carry **no QR token** — the token lives on the
registration (it is generated per event and doubles as the Auth password).
| Column | Type | Notes |
|---|---|---|
| `email` | `text` **PK** | The ticket-purchase email (login identifier) |
| `full_name` | `text` | |
| `linkedin_url` | `text` | nullable |
| `show_email` | `boolean` | default `false` — visibility toggle |
| `show_linkedin` | `boolean` | default `false` — visibility toggle |
| `is_admin` | `boolean` | default `false` — **staff = admin (merged)**; grants check-in + APPE Admin access |
| `password` | `text` | nullable — the **login password** (event-independent) for admin/staff. Setting it (e.g. in the Supabase Table Editor) **auto-syncs** the bcrypt hash into `auth.users.encrypted_password` via the `trg_contact_password_sync` trigger, so it works with `signInWithPassword` immediately |
| `external_id` | `text` | nullable — reserved for an external system's id (currently unused) |
| `created_at` | `timestamptz` | |
Partial unique index: `contacts_external_id_uniq (external_id) WHERE external_id IS NOT NULL`.
## `events` — a conference
At most **one active event** at a time (partial unique index + trigger). The app
queries everything for the active event and handles "no active event" gracefully.
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | `gen_random_uuid()` |
| `name` | `text` | e.g. "APPE 2026" |
| `start_date` / `end_date` | `date` | |
| `location` | `text` | nullable |
| `company` | `text` | nullable — the owning organisation / company (e.g. the **Pretix organizer name**). Auto-filled by the Pretix sync; bindable on badges (see [sidecar.md](./sidecar.md)) |
| `is_active` | `boolean` | default `false` |
| `avg_feedback` | `numeric` | maintained aggregate |
| `num_sessions` | `integer` | maintained aggregate |
| `num_speakers` | `integer` | maintained aggregate |
| `num_registrations` | `integer` | maintained aggregate |
| `num_checked_in_registrations` | `integer` | maintained aggregate |
| `sessionize_endpoint` | `text` | nullable — the Sessionize API URL for this event (see [sidecar.md](./sidecar.md)) |
| `use_realtime_clock` | `boolean` | default `false` — when `true`, the attendee app drives slot status (Live/Next/Done) from the real system clock; when `false` it uses the simulated mid-event clock (see [../frontend/app-structure.md](../frontend/app-structure.md)) |
| `talk_networking_bonus` | `integer` | default `2` — bonus points awarded to **each** party when two people who were checked into the same session (talk) accept a connection. `0` disables the feature (the +5 connection award still applies). Awarded with the `talk_networking` points category (see [rpcs.md](./rpcs.md#accept_connectionp_connection_id-uuid)) |
| `badge_copies` | `integer` | default `1`, `check (between 1 and 4)` — how many badge labels a **welcome (front-desk) check-in** prints. Enforced again inside `welcome_checkin`, so a hand-edited row cannot flood the queue. Manual reprint ignores it and always prints one. Set from **Settings → Badge printing** (see [printing.md](./printing.md)) |
| `last_synced_at` | `timestamptz` | nullable — when the Sessionize sync last ran |
| `created_at` | `timestamptz` | |
**Constraint:** `unique index events_one_active on (is_active) where is_active`
— at most one active event. The aggregate columns are kept in sync by triggers
(counts) and the `submit_feedback` RPC (`avg_feedback`).
## `registrations` — a contact's participation in an event
Points are scoped **per event** here (one row per contact per event).
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | `on delete cascade` |
| `type` | `registration_type` (enum) | `attendee` \| `speaker` \| `staff` \| `sponsor` (default `attendee`) |
| `checked_in` | `boolean` | default `false` — **the front-desk flag.** Set by the welcome check-in (or an admin's Registrations toggle); it is what makes someone eligible for session check-ins. `checkin_attendee` refuses anyone with this flag `false` |
| `points` | `integer` | `>= 0`, default 0 |
| `qr_token` | `text` | nullable — the lanyard token; **doubles as the Auth password** (generated per event) |
| `external_id` | `text` | nullable — reserved for an external system's id (currently unused) |
| `created_at` | `timestamptz` | |
**Constraint:** `unique (event_id, contact_email)` — one registration per contact
per event. Partial unique indexes: `registrations_external_id_uniq (external_id) WHERE external_id IS NOT NULL`
and `registrations_qr_token_uniq (qr_token) WHERE qr_token IS NOT NULL` (fast lanyard lookups).
**`type` is a Postgres ENUM** (`public.registration_type`), not free text — the
four ticket types are enforced at the database level.
**Speaker auto-promote:** the `session_speakers_speaker_promote` trigger (after
insert on `session_speakers`) promotes a contact's active-event registration from
`attendee` → `speaker` when they're added as a speaker. It is one-way (removing a
speaker never demotes) and never overrides an explicit `staff`/`sponsor` ticket.
## `sponsors` — a sponsor for an event
Unpaid sponsors are hidden from non-admins (RLS).
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `type` | `text` | `headline` \| `platinum` \| `gold` \| `silver` \| `community` \| `swag` \| `catering` \| `diversity` |
| `name` | `text` | |
| `email` | `text` | nullable |
| `phone` | `text` | nullable |
| `paid` | `boolean` | default `false` — unpaid hidden from non-admins |
| `amount_eur` | `numeric` | nullable |
| `contact_email` | `text` FK → `contacts` | nullable lookup, `on delete set null` |
| `created_at` | `timestamptz` | |
## `slots` — schedule blocks
| Column | Type | Notes |
|---|---|---|
| `id` | `integer` PK | `nextval('slots_id_seq')` default — the UI/sidecar can create a slot **without** supplying an id (the seed inserts explicit ids and re-syncs the sequence) |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `type` | `slot_type` (enum) | **`session` \| `service_session`** (default `session`) — a true optionset, not freetext. `service_session` = breaks / lunch / closing / plenums (Sessionize's `isServiceSession` **or** `isPlenumSession`) |
| `name` | `text` | e.g. "Slot 09:00–09:45" / "Service Session" |
| `slot_date` | `date` | |
| `start_time` / `end_time` | `time` | |
> **`type` is a Postgres ENUM** (`public.slot_type`, added by
> [`20260832000000_slots_enum_excel_import.sql`](../../supabase/migrations/20260832000000_slots_enum_excel_import.sql)).
> The old freetext values (`keynote` / `lunch_break` / `coffee_break` /
> `close_up`) were all folded into `service_session` — they are all non-talk
> agenda items. The admin Schedule view shows the two values as **Session** /
> **Service Session**.
## `sessions` — one per (slot, room)
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | `gen_random_uuid()` |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `slot_id` | `integer` FK → `slots` | `on delete cascade` |
| `title` | `text` | |
| `description` | `text` | nullable |
| `room` | `text` | "Main Stage" / "Room A" / "Room B" |
| `capacity` | `integer` | nullable (not enforced) |
| `level` | `text` | nullable — free-text level (100/200/300 or Beginner/…), imported |
| `area` | `text` | nullable — free-text topic/area, imported |
| `language` | `text` | nullable — delivery language, imported from the Sessionize "Language" session category (custom fields must be enabled on the endpoint) or the Excel `Language` column; see `20261021090000_session_language.sql`. Service sessions are always forced to `English` (`20261021103000_service_language_english.sql`) |
| `average_feedback` | `numeric` | maintained by `submit_feedback` |
| `ai_generated_feedback` | `text` | AI-feedback state (stub) |
| `material_path` | `text` | nullable — storage object path for session material |
| `external_id` | `text` | nullable — reserved for an external system's id (currently unused) |
| `created_at` | `timestamptz` | |
> **Speakers are no longer a `text` column** — they live in the `session_speakers`
> join table below (1:N). The `get_agenda()` RPC aggregates them into a
> comma-separated `speakers` string for display.
Indexes: `sessions_event_id_idx (event_id)`, `sessions_slot_id_idx (slot_id)`.
Partial unique index: `sessions_external_id_uniq (external_id) WHERE external_id IS NOT NULL`.
## `session_speakers` — M:N between sessions and contacts
A session must have **≥ 1 speaker** (enforced by a trigger).
| Column | Type | Notes |
|---|---|---|
| `session_id` | `uuid` FK → `sessions` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | `on delete cascade` |
**PK:** `(session_id, contact_email)`.
## `preferences` — one pick per slot
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | `on delete cascade` |
| `session_id` | `uuid` FK → `sessions` | `on delete cascade` |
| `slot_id` | `integer` | denormalized for easy "one per slot" |
| `created_at` | `timestamptz` | |
**Constraint:** `unique (event_id, contact_email, slot_id)` — one pick per slot.
Enforced in the DB *and* by the `set_preference` RPC (which deletes the old pick
for the slot before inserting).
## `connections` — the networking relationship
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `from_email` | `text` FK → `contacts` | the requester |
| `to_email` | `text` FK → `contacts` | the recipient |
| `status` | `text` | `pending` \| `accepted` \| `rejected` (default `pending`) |
| `created_at` | `timestamptz` | |
| `resolved_at` | `timestamptz` | set when accepted/rejected |
| `auto` | `boolean` | staff auto-pairing (never hand-made, never scored) — `20261020120000_staff_auto_connections.sql` |
**Staff auto-connect:** the trigger `registrations_autoconnect_staff` inserts a
staff × person pair as `accepted`, `auto = true` whenever a registration is
created or changes type (plus a backfill in the migration). Staff here means
exactly the points-exempt set (`_points_eligible`): type `staff`, Admin contact,
or an `@appe.at` address. No points are ever awarded for these rows, and
existing pairs — including manually rejected ones — are never touched. The admin
console hides `auto` rows from its activity views.
**Constraint:** unique per **unordered pair per event** via
`unique index connections_pair_uniq on (event_id, least(from_email, to_email),
greatest(from_email, to_email))`. So Ana→Bruno and Bruno→Ana are the *same*
connection.
## `checkins` — staff check-in of a contact
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | the attendee being checked in |
| `session_id` | `uuid` FK → `sessions` | |
| `staff_email` | `text` FK → `contacts` | the staff member who scanned |
| `created_at` | `timestamptz` | |
**Constraint:** `unique (event_id, contact_email, session_id)` — one check-in per
contact per session (prevents double points).
## `feedback` — post-session rating
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | |
| `session_id` | `uuid` FK → `sessions` | |
| `rating` | `integer` | `1–5` |
| `comment` | `text` | nullable (bonus points if present) |
| `created_at` | `timestamptz` | |
**Constraint:** `unique (event_id, contact_email, session_id)` — one feedback per
contact per session.
Points (awarded by `submit_feedback`, first submission only): **+5** rating
only, **+10** rating + non-empty comment.
## `event_feedback` — event-level rating
Same shape as `feedback` but `session_id` is replaced by
`unique (event_id, contact_email)` — one answer per participant per event,
upserted by `submit_event_feedback` (read by `get_my_event_feedback`).
Points: **+5** on the **first** submission only (edits earn nothing), gated by
`_points_eligible` — see `20261020130000_event_feedback_points.sql`.
## `leaderboard` — Realtime mirror
A public, Realtime-friendly mirror of points **per event**. **Not** the source of
truth — it is kept in sync transactionally by the RPCs (see
[rpcs.md](./rpcs.md#_sync_leaderboard)).
| Column | Type | Notes |
|---|---|---|
| `event_id` | `uuid` PK FK → `events` | |
| `contact_email` | `text` PK FK → `contacts` | |
| `full_name` | `text` | denormalized for display |
| `points` | `integer` | |
| `updated_at` | `timestamptz` | |
**PK:** `(event_id, contact_email)`. The app subscribes to `postgres_changes` on
this table to update the leaderboard live. See
[../frontend/screens.md#leaderboard](../frontend/screens.md#leaderboard).
> **Staff are excluded.** `_sync_leaderboard` skips (and removes) staff rows, so
> staff never appear on the board even if they hold points. `get_leaderboard()`
> also filters `type = 'staff'` as defense in depth.
## `points_log` — audit trail
Powers the per-category points breakdown on the Profile screen (per event).
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `contact_email` | `text` FK → `contacts` | |
| `delta` | `integer` | points added (can be negative for admin adjustments) |
| `category` | `text` | `checkin` \| `connection` \| `feedback` \| `admin` \| `talk_networking` (CHECK constraint, widened by the talk-networking migration) |
| `created_at` | `timestamptz` | |
Index: `points_log_event_id_idx (event_id)`.
## `sync_runs` — Sessionize sync audit trail
Event-scoped record of every sidecar sync run (the data behind the admin
Interfaces view's "recent runs").
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | `gen_random_uuid()` |
| `event_id` | `uuid` FK → `events` | `on delete cascade` |
| `source` | `text` | `sessionize` (API sync) \| `sessionize_excel` (Excel import) |
| `started_at` / `finished_at` | `timestamptz` | `finished_at` nullable |
| `created` / `updated` / `skipped` | `integer` | counts from the run |
| `error` | `text` | nullable — error text if the run failed |
| `triggered_by` | `text` | default `service_role` |
**RLS:** enabled; `SELECT` for `authenticated` restricted to admins
(`sync_runs_select_admin`). Written only by the `sync_sessionize_event` /
`sync_sessionize_excel` RPCs.
## `print_stations` — badge printer stations
One row per Raspberry Pi print station. Added by
[`20261018170000_badge_print_queue.sql`](../../supabase/migrations/20261018170000_badge_print_queue.sql)
together with the badge-print queues — the full model (tokens, states, RPCs)
is in [printing.md](./printing.md).
| Column | Type | Notes |
|---|---|---|
| `id` | `uuid` PK | `gen_random_uuid()` |
| `name` | `text` | non-blank; renamed via `rename_print_station`, shown to the worker through the heartbeat response |
| `token_hash` | `text` **unique** | the **SHA-256 hex digest** of the station's `cvp_…` bearer token — the plaintext is shown once at creation and never stored |
| `created_at` | `timestamptz` | |
| `revoked_at` | `timestamptz` | nullable — set by `revoke_print_station`; the token stops working immediately |
| `last_seen_at` | `timestamptz` | stamped by every printer RPC; > 45 s old ⇒ state `offline` |
| `last_status` | `jsonb` | the latest heartbeat blob (printer / network / worker), capped at 16 KB |
**RLS enabled with no policies** and no grants to `anon` / `authenticated` —
every access goes through the `security definer` RPCs (admin:
`get_print_overview` & friends; printer: the token-gated `printer_*` functions).
### pgmq queues (`badge_print`, `badge_print_dlq`)
The `pgmq` extension holds the badge-print job queue and its dead-letter queue.
Queue rows live in the **`pgmq` schema** (`pgmq.q_badge_print`,
`pgmq.a_badge_print` for completed/archived jobs, `pgmq.q_badge_print_dlq`),
not in `public`. The schema, tables, sequences and functions are revoked from
`public` / `anon` / `authenticated`: the API never exposes queue contents —
enqueuing, claiming, completing and dead-lettering all go through the RPCs in
[printing.md](./printing.md#4-rpcs).
## Other indexes
- `slots_event_id_idx (event_id)`, `registrations_event_id_idx (event_id)`,
`checkins_session_id_idx (session_id)`.
## Storage
A **`materials`** bucket (**private** since `20261021180000_material_time_lock.sql`)
stores session material. Speakers + admins can write (see
[rls.md](./rls.md#storage--materials-bucket)). A session's `material_path`
points at the object. Files are fetched through **authenticated** storage
requests (attendee app) or short-lived signed URLs (admin app); the read policy
releases a file to plain attendees only after the session's scheduled end —
`/storage/v1/object/public/...` URLs no longer serve materials.