Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3198,6 +3201,7 @@ mod postgres_tests {
"entity_deletion_approvals",
"workflow_runs",
"push_gateway_installations",
"push_gateway_delegations",
] {
assert_eq!(
constraints(&desired, table).await,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 20 additions & 1 deletion crates/buzz-push-gateway/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);",
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions migrations/0101_drop_push_gateway_delegation_fk.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion schema/tables/public/push_gateway_delegations.sql
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
Loading