mirror of
https://github.com/block/buzz.git
synced 2026-11-25 02:31:36 +00:00
push-gateway: drop the delegation → installation foreign key (#9610)
## Summary Retire the last foreign key in the push gateway schema: `push_gateway_delegations.installation_id → push_gateway_installations(id)`. This follows `docs/database.md` Rule 2 and the relay FK retirement work (#9399, #9404). - **Gateway migration 0008** drops the constraint. - **Relay migration 0101** drops the relay's legacy copy (Rule 4: gateway schema changes go in both). `schema/tables/public/push_gateway_delegations.sql` loses the inline `REFERENCES`. ## Required stop Every gateway and rollback target must run #9627 (`44cdd9fe`) or later before this merges and deploys. #9627 makes the reaper lock expired installations before it deletes their delegations. Without the FK, an older reaper can leave an orphaned delegation. This is step 2 of the Rule 5 retirement sequence; #9627 is step 1. ## Why the app already enforces the reference | Path | Check | |---|---| | `upsert_delegation` (only delegation insert) | Locks the installation `FOR UPDATE` in the same transaction; rejects a missing or revoked one. | | `create_installation` (replaces expired installations) | Locks the matching installations `FOR UPDATE`, deletes their delegations, then the installations. | | `reap_expired` | Locks expired installations `FOR UPDATE`, deletes their delegations, then the installations (#9627). | | `authorize_delivery` | Reads the installation through the delegation; a missing installation is `Rejected`. | | `revoke_delegation` | Updates the delegation only; an orphan has no effect. | | `revoke_installation`, `rotate_endpoint`, `advance_assertion_counter` | Update the installation; they never delete it. | The relay never reads or writes the `push_gateway_*` tables (the gateway requires its own database), so dropping the relay copy changes no relay behavior. ## Tests The gateway tests run against the migrated schema, which no longer has the FK, so they prove the app check. - `migrated_schema_has_no_foreign_keys` (new): the gateway schema has no foreign keys after migrations. It fails if gateway migration 0008 is removed. - `delegation_for_missing_installation_is_rejected` and `reaper_deletes_delegation_committed_while_it_waits` (from #9627) now run without the FK. - `reaper_retains_revocation_tombstones_until_authority_expiry`: its hand-built schema drops the FK too. - Relay: `push_gateway_delegations` joins the constraint parity check in `admin_schema_parity_between_desired_state_and_migrations`, and the migration count test covers 0101. ## Validation Blox at `33f7df90` (before the rename to 0101; CI covers the rebased head): - `cargo fmt --check`, clippy (`buzz-push-gateway`, `buzz-db`, all targets), gateway unit tests, `buzz-db` migration unit tests: pass. - `scripts/postgres-test-run.sh -p buzz-push-gateway --lib --tests`: 16/16 pass. - `buzz-db` Postgres tests for migrations, parity, push, floor retirement and retained guards: 56/56 pass, including `admin_schema_parity_between_desired_state_and_migrations`. - Must-fail check: with gateway migration 0008 removed, `migrated_schema_has_no_foreign_keys` fails (1 foreign key vs 0). ## Coordination - On main after #8696, #9398 and #9710 (relay 0095–0100). #9627 shipped this PR's reaper fix and tests separately, so this PR is the schema drop only. - Renumbered to relay 0101 after #9710 took 0098–0100 (rename only; file contents unchanged). #9690 and #9621 also add a relay 0098; whichever merges second renumbers. - #8328 adds the gateway's declarative schema with the inline FK. Whichever PR merges second removes it there. ## What got simpler One fewer database object. Every gateway reference check now lives in one place: the gateway's own transactions. buzz-review-completed 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Luke Tornquist <tornquist@squareup.com> Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
@@ -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() {
|
||||
|
||||
7
migrations/0101_drop_push_gateway_delegation_fk.sql
Normal file
7
migrations/0101_drop_push_gateway_delegation_fk.sql
Normal file
@@ -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;
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user