ADR 0294: The social schema is additive, site-keyed and shaped for atomic claim
- Status: Accepted
- Date: 2026-10-29
- Parent epic: #353
- Story: #354 (task #362)
Context#
A hosted schema is a one-way door: once a site runs it, data lives there. The epic corrects a sketch that used
gen_random_policy_v4(), plaintext tokens, a client insert with no owner, and a PUBLISHED flag with no proof of
delivery. Later stories need to claim due posts atomically, retry without duplicating, and record the provider's
response id. supabase/workspace.sql, feedback.sql and protection.sql set the conventions: server-only
service_role, additive and re-runnable, one rollback file.
Decision#
The most conservative reversible design, in supabase/social.sql with supabase/social-rollback.sql:
- Additive and re-runnable. Only
create ... if not exists, no change to any existing object. The rollback drops only theopenblog_social_*tables and leavespgcrypto. A version table (openblog_social_schema, row1) lets a later migration know what it upgrades. IDs usegen_random_uuid(). - Private. Row level security on, no policies,
revoke allfrompublic,anon,authenticated;service_rolegetsselect, insert, updateand neverdelete: cancel and disconnect are updates, attempts are append and finish, and foreign keys areon delete restrictso history is never cascaded away. - Site keyed, not owner keyed. Every table has
site_idwith the same check as the scope rule (ADR 0293). Ownership is a composite foreign key: a post's(account_id, site_id)references the account's(id, site_id), and an attempt's(post_id, site_id)references the post's, so a row cannot point across sites even by mistake. The Admin user is only the audit textconnected_by/created_by/worker. - Accounts (
openblog_social_accounts): provider, kind, the provider's id and a display name (none secret), statusconnected | needs-reauth | disconnected. A partial unique index allows one live connection per identity. - Queue (
openblog_social_posts): the canonicalworkandhttpsURL, the text (at most 3,000),scheduled_for, statusscheduled | claimed | published | failed | canceled | unknown, aversion, a lease (claimed_by,claimed_at,lease_expires_at) that check constraints require exactly whileclaimed, anidempotency_keyunique per site and account,attempt_count,provider_post_idandpublished_at, which check constraints require to appear together and exactly whenpublished: nothing ispublishedwithout a provider id. Partial indexes serve "due and unclaimed" and "expired lease".unknownis a first-class state for an outcome that may have reached the provider, so it is reconciled, never blindly retried. - Attempts (
openblog_social_attempts):(post_id, attempt_no)unique, worker, start and finish, outcomepublished | failed | unknown, HTTP status, provider request and post ids, a short error code and at most 500 characters of redacted detail. Never a token, header or response body. - Open vocabularies, closed lifecycles.
provider,kindand error codes are checked for shape only (the feedback convention), so a new provider needs no migration. The lifecycle states are enumerated because claiming depends on them; widening one is a one-line additive constraint swap. - Verified for real. The statements were run against PostgreSQL twice (idempotent), a cross-site post was
refused by the foreign key, and the rollback left no
openblog_social_*table.
Considered options#
auth.usersownership and RLS policies. Rejected in ADR 0293.- Provider-specific tables. Rejected: a table per provider multiplies migrations; the shape-checked columns do not.
- A claim function now. Deferred: the claim story owns its semantics. This schema only guarantees it is possible (version, lease checks, indexes, idempotency).
on delete cascade. Rejected: deleting an account must not erase what was sent.
Consequences#
- Rolling back deletes the schedule and delivery history; the rollback file says how to export it first.
- Adding a provider or a lifecycle state is additive.