diff --git a/crates/buzz-db/src/runtime/migration.rs b/crates/buzz-db/src/runtime/migration.rs index 1bc8a1ff227..8a5c0879fa9 100644 --- a/crates/buzz-db/src/runtime/migration.rs +++ b/crates/buzz-db/src/runtime/migration.rs @@ -879,7 +879,10 @@ mod postgres_tests { let mut migrations: Vec<_> = MIGRATOR.iter().collect(); migrations.sort_by_key(|migration| migration.version); - assert_eq!(migrations.len(), 100); + assert_eq!(migrations.len(), 101); + assert_eq!(migrations[100].version, 101); + assert!(strip_sql_comments(migrations[100].sql.as_str()) + .contains("DROP CONSTRAINT IF EXISTS push_gateway_delegations_installation_id_fkey")); assert_eq!(migrations[89].version, 90); assert!(migrations[89] .sql @@ -3198,6 +3201,7 @@ mod postgres_tests { "entity_deletion_approvals", "workflow_runs", "push_gateway_installations", + "push_gateway_delegations", ] { assert_eq!( constraints(&desired, table).await, diff --git a/crates/buzz-push-gateway/migrations/0008_drop_delegation_installation_fk.sql b/crates/buzz-push-gateway/migrations/0008_drop_delegation_installation_fk.sql new file mode 100644 index 00000000000..c2b585776f6 --- /dev/null +++ b/crates/buzz-push-gateway/migrations/0008_drop_delegation_installation_fk.sql @@ -0,0 +1,11 @@ +-- Retire the last foreign key in the gateway schema (docs/database.md Rule 2). +-- The gateway checks the reference in the writing transaction instead: +-- upsert_delegation locks the installation FOR UPDATE and rejects a missing or +-- revoked one, the reaper locks expired installations before it deletes them +-- and their delegations (#9627), and delivery rejects a delegation whose +-- installation is gone. Every gateway and rollback target must run #9627 or +-- later before this migration runs; an older reaper can leave an orphaned +-- delegation once the foreign key is gone. +SET LOCAL lock_timeout = '5s'; +ALTER TABLE push_gateway_delegations + DROP CONSTRAINT push_gateway_delegations_installation_id_fkey; diff --git a/crates/buzz-push-gateway/src/postgres.rs b/crates/buzz-push-gateway/src/postgres.rs index 18de63fd893..7b068bacac3 100644 --- a/crates/buzz-push-gateway/src/postgres.rs +++ b/crates/buzz-push-gateway/src/postgres.rs @@ -732,7 +732,7 @@ mod postgres_tests { ); CREATE TABLE push_gateway_delegations ( id UUID PRIMARY KEY, - installation_id UUID NOT NULL REFERENCES push_gateway_installations(id), + installation_id UUID NOT NULL, expires_at TIMESTAMPTZ NOT NULL, revoked_at TIMESTAMPTZ );", @@ -1068,6 +1068,25 @@ mod postgres_tests { drop_schema(&schema).await; } + // The schema has no foreign keys, so these tests prove the reference + // checks run in the gateway's own transactions. + #[tokio::test] + #[ignore = "requires PostgreSQL"] + async fn migrated_schema_has_no_foreign_keys() { + let (pool, schema) = full_schema(1).await; + let foreign_keys: i64 = sqlx::query_scalar( + "SELECT count(*) FROM pg_constraint c + JOIN pg_namespace n ON n.oid = c.connamespace + WHERE c.contype = 'f' AND n.nspname = current_schema()", + ) + .fetch_one(&pool) + .await + .expect("count foreign keys"); + assert_eq!(foreign_keys, 0); + pool.close().await; + drop_schema(&schema).await; + } + #[tokio::test] #[ignore = "requires PostgreSQL"] async fn delegation_for_missing_installation_is_rejected() { diff --git a/migrations/0101_drop_push_gateway_delegation_fk.sql b/migrations/0101_drop_push_gateway_delegation_fk.sql new file mode 100644 index 00000000000..4c2dc699212 --- /dev/null +++ b/migrations/0101_drop_push_gateway_delegation_fk.sql @@ -0,0 +1,7 @@ +-- Mirror of push gateway migration 0008 (docs/database.md Rule 4). The relay +-- never reads or writes the push_gateway_* tables: the gateway runs against its +-- own dedicated database. Dropping this copy of the foreign key changes no +-- relay behavior. +SET LOCAL lock_timeout = '5s'; +ALTER TABLE push_gateway_delegations + DROP CONSTRAINT IF EXISTS push_gateway_delegations_installation_id_fkey; diff --git a/schema/tables/public/push_gateway_delegations.sql b/schema/tables/public/push_gateway_delegations.sql index 5022e651032..6b28223bea7 100644 --- a/schema/tables/public/push_gateway_delegations.sql +++ b/schema/tables/public/push_gateway_delegations.sql @@ -1,6 +1,6 @@ CREATE TABLE push_gateway_delegations ( id UUID PRIMARY KEY, - installation_id UUID NOT NULL REFERENCES push_gateway_installations(id), + installation_id UUID NOT NULL, relay_pubkey BYTEA NOT NULL CHECK (length(relay_pubkey) = 32), endpoint_epoch BIGINT NOT NULL CHECK (endpoint_epoch > 0), generation BIGINT NOT NULL CHECK (generation > 0),