Files
buzz/schema/tables/public/communities.sql
Luke Tornquist 9ea57cba2a fix(db): undo migration 0097 (#9710)
## What

Migrations `0098`–`0100` undo migration `0097` (community artifact
expand). The declared schema goes back to its shape before #9650.

#9650 added `0097`. The revert, #9689, removed the feature but kept
`0097` byte for byte, because databases that auto-migrated recorded its
checksum. No code on `main` reads or writes what `0097` added. This PR
removes it with forward migrations, as `docs/database.md` requires
("Never edit a merged migration. Fix forward.").

`artifact_heads.channel_id` gets `NOT NULL` back in the three steps from
*Adding `NOT NULL` to a populated table*:
- `0098` checks that the artifact head key is `(community_id,
artifact_id)`, then adds `CHECK (channel_id IS NOT NULL) NOT VALID`.
- `0099` validates the check under `SHARE UPDATE EXCLUSIVE`, so channel
artifact reads and writes continue.
- `0100` sets `NOT NULL` from the validated check (no scan), drops the
check, drops `artifact_heads_live_type`,
`artifact_heads_scoped_identity` and
`artifact_revisions_scoped_identity`, and drops `artifact_heads.scope`,
`artifact_revisions.scope`, `artifact_revisions.community_event`,
`artifact_revisions.redacted` and `communities.artifact_type_policies`.

The primary keys don't change. `0097` never changed them.

## Review checklist (docs/database.md)

- **Phase:** contract. It follows the expand in #9650 (`0097`) and the
code revert in #9689.
- **Required stop:** every relay pod, job and rollback target must run
#9689 (`sha-a406191`) or later before `0100` runs. The chart's upgrade
notes (`deploy/charts/buzz/README.md`) say this, along with how to roll
back with auto-migration off. No tagged relay release contains #9650.
Only rolling images from `eebf62b6` up to, but not including, `a4061916`
are affected. Relays built from #9650 itself read `artifact_heads.scope`
and write with `ON CONFLICT` on `artifact_heads_scoped_identity`.
Releases before #9650 never read these objects.
- **Fails closed:** `0098` refuses a database whose artifact head key is
the scoped key from #9650's removed operator contract script or declared
schema. That database needs the recovery in #9689's Compatibility
section. `0099` fails if any head has a null `channel_id`. Both stop
before any cleanup.
- No trigger, function, procedure, rule or foreign key is added. No
migration changes existing rows.
- Every schema location in Rule 4 is updated: `migrations/` and
`schema/tables/public/` (`artifact_heads.sql`, `artifact_revisions.sql`,
`communities.sql`). No reconcile-script or gateway change applies.
- **Currently deployed relay:** a relay from #9689 or later doesn't use
these objects, and neither does a relay from before #9650. The
populated-table test runs every migration over channel artifact data.
- **Locks:** each migration sets `lock_timeout = '5s'` and
`statement_timeout = '30s'`. No index is built.

## Tests

- `admin_schema_parity_between_desired_state_and_migrations` compares
`artifact_heads`, `artifact_revisions` and `communities` across the
desired-state and migrated paths. It fails without `0100`.
- `populated_artifact_tables_auto_migrate_through_every_migration`
checks that a channel head and revision written before `0097` survive
with the same channel and event.
- `artifact_unexpand_validates_while_channel_heads_are_written`: `0099`
completes while a channel head write is still open. It fails if `0099`
sets `NOT NULL` directly.
- `artifact_unexpand_refuses_scoped_head_key`: `0098` stops on the
scoped key and leaves `0097`'s columns in place.

## What got simpler

The schema loses 5 unused columns and 3 unused indexes.
`artifact_heads.channel_id` is required again, as the channel code
assumes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:18:37 -05:00

34 lines
2.0 KiB
SQL

-- ── Communities ───────────────────────────────────────────────────────────────
-- Conformance: row zero (host binding). The host map. `resolve_host(host)`
-- reads exactly one row here to mint the request's TenantContext. This table
-- is OPERATOR-GLOBAL: it is the registry of tenants, not itself tenant-scoped,
-- so it carries no `community_id` of its own (its `id` IS the community key).
-- Listed in the lint allowlist as operator-global.
--
-- Host normalization (Lane 0 contract): `host` is stored already-normalized —
-- ASCII-lowercased, trailing dot stripped, default port omitted. The UNIQUE is
-- on `lower(host)` belt-and-suspenders so `Relay.Example` and `relay.example`
-- can never become two tenants even if a writer forgets to normalize.
-- `resolve_host()` (buzz-core) applies the identical normalization before
-- lookup, so resolution and storage agree by construction.
CREATE TABLE communities (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
host VARCHAR(255) NOT NULL,
signing_key BYTEA,
requires_nip_fi BOOLEAN NOT NULL DEFAULT false,
policy_generation BIGINT NOT NULL DEFAULT 0,
-- Per-community workspace icon (NIP-11 `icon`), set via kind:9033.
-- Added by migration 0003; kept here so desired-state applies match.
icon TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
archived_at TIMESTAMPTZ,
deletion_state TEXT NOT NULL DEFAULT 'active' CHECK (deletion_state IN ('active', 'quiescing', 'fenced', 'tombstone')),
deletion_fence_generation BIGINT NOT NULL DEFAULT 0 CHECK (deletion_fence_generation >= 0),
deleted_at TIMESTAMPTZ,
artifact_policy_modified_at TIMESTAMPTZ NOT NULL DEFAULT 'epoch',
CONSTRAINT chk_communities_id_not_nil CHECK (id <> '00000000-0000-0000-0000-000000000000'::uuid)
);
CREATE UNIQUE INDEX idx_communities_host ON communities (lower(host));