mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## 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>
35 lines
1.7 KiB
SQL
35 lines
1.7 KiB
SQL
-- Undo 0097 (community artifact expand), step 1 of 3. The community-scoped
|
|
-- artifact feature that needed 0097 was reverted (#9689), and no release after
|
|
-- the revert reads or writes the columns 0097 added. 0097 stays in place
|
|
-- because databases that auto-migrated have recorded its checksum.
|
|
--
|
|
-- Required stop: every relay pod and job must run #9689 or a later release
|
|
-- before 0100 runs. Relays built from #9650 itself read artifact_heads.scope
|
|
-- and write with ON CONFLICT on artifact_heads_scoped_identity.
|
|
--
|
|
-- This step fails closed on a database whose artifact head key is not the
|
|
-- channel key. Only a database that ran the removed operator contract script,
|
|
-- or was bootstrapped from #9650's declared schema, has the scoped key. Such a
|
|
-- database needs the separate recovery step in #9689 first.
|
|
--
|
|
-- Then it starts restoring NOT NULL on artifact_heads.channel_id without a
|
|
-- table scan under ACCESS EXCLUSIVE: 0099 validates this check, and 0100 sets
|
|
-- NOT NULL from it.
|
|
SET LOCAL lock_timeout = '5s';
|
|
SET LOCAL statement_timeout = '30s';
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF (SELECT array_agg(a.attname::text ORDER BY k.ordinality)
|
|
FROM pg_constraint c
|
|
CROSS JOIN LATERAL unnest(c.conkey) WITH ORDINALITY AS k(attnum, ordinality)
|
|
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.attnum
|
|
WHERE c.conrelid = 'artifact_heads'::regclass AND c.contype = 'p')
|
|
IS DISTINCT FROM ARRAY['community_id', 'artifact_id'] THEN
|
|
RAISE EXCEPTION 'artifact_heads primary key is not (community_id, artifact_id); recover this database as described in #9689 before migrating';
|
|
END IF;
|
|
END $$;
|
|
|
|
ALTER TABLE artifact_heads
|
|
ADD CONSTRAINT artifact_heads_channel_id_not_null CHECK (channel_id IS NOT NULL) NOT VALID;
|