Merge pinned main primary-key repair into invitation candidate

Preserve invitation migration 0069 and admission semantics while integrating main events_pkey reconciliation and schema-parity coverage.

Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: seiler <seiler@squareup.com>
This commit is contained in:
seiler
2026-10-22 21:32:35 +00:00
5 changed files with 222 additions and 36 deletions

View File

@@ -181,6 +181,55 @@ BEGIN
END IF;
END $$;
-- pgschema moves the partition key to the front of a partitioned table's
-- primary key when it creates the table, so events gets
-- PRIMARY KEY (created_at, community_id, id) instead of the declared
-- (community_id, created_at, id). Every later plan then drops and re-adds
-- events_pkey and each declared child's key, which fails once the children are
-- attached because their keys are inherited. Rebuild the key on the parent in
-- the declared order; PostgreSQL recreates each partition's key with it.
-- delivery_log already declares its partition key first. This block is a
-- verbatim copy of scripts/repair-events-pkey.sql; keep the two identical.
DO $$
DECLARE
declared CONSTANT text := 'PRIMARY KEY (community_id, created_at, id)';
BEGIN
-- Also runs before pgschema apply, when events may not exist yet.
IF to_regclass('events') IS NULL THEN
RETURN;
END IF;
IF (
SELECT pg_get_constraintdef(oid) FROM pg_constraint
WHERE conrelid = 'events'::regclass AND contype = 'p'
) IS DISTINCT FROM declared THEN
-- The rebuild locks events and every partition ACCESS EXCLUSIVE. Fail
-- fast instead of queueing behind a relay that still uses them.
BEGIN
PERFORM set_config('lock_timeout', '10s', true);
ALTER TABLE events DROP CONSTRAINT IF EXISTS events_pkey;
ALTER TABLE events
ADD CONSTRAINT events_pkey PRIMARY KEY (community_id, created_at, id);
EXCEPTION WHEN lock_not_available THEN
RAISE EXCEPTION 'timed out locking events to rebuild events_pkey'
USING HINT = 'Stop any relay using this database, then rerun.';
END;
END IF;
IF EXISTS (
SELECT 1 FROM pg_constraint
WHERE contype = 'p'
AND conrelid IN (
SELECT 'events'::regclass
UNION ALL
SELECT inhrelid FROM pg_inherits WHERE inhparent = 'events'::regclass
)
AND pg_get_constraintdef(oid) <> declared
) THEN
RAISE EXCEPTION 'events and its partitions must have %', declared;
END IF;
END $$;
-- pgschema drops multi-column CHECK constraints. Restore the direct-action
-- shape CHECK from schema/schema.sql (and migration 0055) verbatim.
DO $$

View File

@@ -0,0 +1,56 @@
-- Rebuild events_pkey in its declared column order.
--
-- pgschema creates events with the partition key moved to the front of its
-- primary key, and every later plan then tries to drop and re-add the inherited
-- key on each partition, which fails (SQLSTATE 42P16).
-- scripts/reconcile-schema-after-pgschema.sql repairs that after every apply
-- and carries a verbatim copy of the block below. A reused database reconciled
-- before that repair existed still fails its next apply, before the reconcile
-- script can run, so scripts/start-relay-for-tests.sh, the one launcher that
-- reuses its database, runs this file before pgschema apply. It does nothing on
-- an empty database or one whose key is already in order.
--
-- Both copies are temporary. The reconcile block, and the test that keeps the
-- copies identical, can go once pgschema creates the key in declared order
-- (1.13.3 does). This file and the launcher step can go once no local database
-- reconciled before the reconcile block existed remains.
DO $$
DECLARE
declared CONSTANT text := 'PRIMARY KEY (community_id, created_at, id)';
BEGIN
-- Also runs before pgschema apply, when events may not exist yet.
IF to_regclass('events') IS NULL THEN
RETURN;
END IF;
IF (
SELECT pg_get_constraintdef(oid) FROM pg_constraint
WHERE conrelid = 'events'::regclass AND contype = 'p'
) IS DISTINCT FROM declared THEN
-- The rebuild locks events and every partition ACCESS EXCLUSIVE. Fail
-- fast instead of queueing behind a relay that still uses them.
BEGIN
PERFORM set_config('lock_timeout', '10s', true);
ALTER TABLE events DROP CONSTRAINT IF EXISTS events_pkey;
ALTER TABLE events
ADD CONSTRAINT events_pkey PRIMARY KEY (community_id, created_at, id);
EXCEPTION WHEN lock_not_available THEN
RAISE EXCEPTION 'timed out locking events to rebuild events_pkey'
USING HINT = 'Stop any relay using this database, then rerun.';
END;
END IF;
IF EXISTS (
SELECT 1 FROM pg_constraint
WHERE contype = 'p'
AND conrelid IN (
SELECT 'events'::regclass
UNION ALL
SELECT inhrelid FROM pg_inherits WHERE inhparent = 'events'::regclass
)
AND pg_get_constraintdef(oid) <> declared
) THEN
RAISE EXCEPTION 'events and its partitions must have %', declared;
END IF;
END $$;

View File

@@ -103,6 +103,10 @@ export PGSCHEMA_PLAN_DB=buzz
export PGSCHEMA_PLAN_USER=buzz
export PGSCHEMA_PLAN_PASSWORD=buzz_dev
# This database persists across runs. One reconciled before the events_pkey
# repair existed would fail apply, so repair it first (see the script).
docker exec -i -e PGPASSWORD="${PGPASSWORD}" buzz-postgres \
psql -U "${PGUSER}" -d "${PGDATABASE}" -v ON_ERROR_STOP=1 < scripts/repair-events-pkey.sql
./bin/pgschema apply --file schema/schema.sql --auto-approve
docker exec -i -e PGPASSWORD="${PGPASSWORD}" buzz-postgres \
psql -U "${PGUSER}" -d "${PGDATABASE}" -v ON_ERROR_STOP=1 < scripts/reconcile-schema-after-pgschema.sql
@@ -168,7 +172,7 @@ if [[ "${BUZZ_REQUIRE_RELAY_MEMBERSHIP:-}" == "true" ]]; then
fi
nohup env \
DATABASE_URL=postgres://buzz:buzz_dev@localhost:5432/buzz \
DATABASE_URL="postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}:${PGPORT}/${PGDATABASE}" \
REDIS_URL=redis://localhost:6379 \
RELAY_URL=ws://localhost:3000 \
BUZZ_BIND_ADDR=0.0.0.0:3000 \