Skip to content

Commit 5cde354

Browse files
committed
BUZZ-301: Fix intake schema review findings
Signed-off-by: Ravneet Arora <[email protected]>
1 parent c2087b9 commit 5cde354

4 files changed

Lines changed: 66 additions & 39 deletions

File tree

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,13 +2624,13 @@ mod postgres_tests {
26242624
///
26252625
/// This bootstraps one probe database from `schema.sql` **through the real
26262626
/// `bin/pgschema apply` binary** — the exact path CI (`ci.yml`) and both
2627-
/// test-relay launchers take — and migrates another through 1–38, then
2628-
/// asserts the three admin tables have identical column definitions (name,
2629-
/// type, nullability, default) and identical index shapes, including each
2627+
/// test-relay launchers take — and migrates another through the latest
2628+
/// migration, then asserts the admin and entity-intake tables have identical
2629+
/// column definitions (name, type, nullability, default) and index shapes, including each
26302630
/// key's catalog sort/null options (`pg_index.indoption`). Columns are keyed
26312631
/// by name, not ordinal, because migrations append via `ALTER TABLE` while
26322632
/// `schema.sql` declares them inline — positions legitimately differ, shapes
2633-
/// must not.
2633+
/// must not. Entity-intake constraints are also compared in the live catalog.
26342634
///
26352635
/// Driving the real binary is load-bearing: `pgschema` 1.7.4 discards
26362636
/// per-key `NULLS FIRST`/`NULLS LAST` when it re-emits an index, so a naive
@@ -2688,6 +2688,21 @@ mod postgres_tests {
26882688
.expect("read index shapes")
26892689
}
26902690

2691+
async fn constraints(pool: &PgPool, table: &str) -> Vec<(String, String, bool)> {
2692+
sqlx::query_as(
2693+
"SELECT con.conname, pg_get_constraintdef(con.oid), con.convalidated \
2694+
FROM pg_constraint con \
2695+
JOIN pg_class t ON t.oid = con.conrelid \
2696+
JOIN pg_namespace n ON n.oid = t.relnamespace \
2697+
WHERE n.nspname = 'public' AND t.relname = $1 \
2698+
ORDER BY con.conname",
2699+
)
2700+
.bind(table)
2701+
.fetch_all(pool)
2702+
.await
2703+
.expect("read constraint definitions")
2704+
}
2705+
26912706
let base_url = std::env::var("BUZZ_TEST_DATABASE_URL")
26922707
.or_else(|_| std::env::var("DATABASE_URL"))
26932708
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
@@ -2755,18 +2770,25 @@ mod postgres_tests {
27552770
let desired = PgPool::connect(&format!("{base_prefix}/{desired_db}"))
27562771
.await
27572772
.expect("connect desired-state probe database");
2773+
sqlx::raw_sql(include_str!(
2774+
"../../../../scripts/reconcile-schema-after-pgschema.sql"
2775+
))
2776+
.execute(&desired)
2777+
.await
2778+
.expect("reconcile desired-state probe database");
27582779
let migrated = PgPool::connect(&format!("{base_prefix}/{migrated_db}"))
27592780
.await
27602781
.expect("connect migrated probe database");
27612782
MIGRATOR
2762-
.run_to(55, &migrated)
2783+
.run(&migrated)
27632784
.await
2764-
.expect("apply migrations 1-55");
2785+
.expect("apply migrations for schema parity");
27652786

27662787
for table in [
27672788
"relay_admin_actions",
27682789
"relay_admin_outbox",
27692790
"relay_operator_audit",
2791+
"entity_deletion_requests",
27702792
] {
27712793
assert_eq!(
27722794
columns(&desired, table).await,
@@ -2784,6 +2806,12 @@ mod postgres_tests {
27842806
);
27852807
}
27862808

2809+
assert_eq!(
2810+
constraints(&desired, "entity_deletion_requests").await,
2811+
constraints(&migrated, "entity_deletion_requests").await,
2812+
"constraint parity mismatch for entity_deletion_requests"
2813+
);
2814+
27872815
desired.close().await;
27882816
migrated.close().await;
27892817
for probe_db in [desired_db, migrated_db] {

‎crates/buzz-db/src/store/entity_deletion_postgres_tests.rs‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ async fn store() -> EntityDeletionStore {
88
}
99
}
1010

11+
async fn assert_plain_schema(pool: &PgPool) {
12+
let fences: i64 = sqlx::query_scalar(
13+
"SELECT count(*) FROM pg_trigger
14+
WHERE tgrelid = 'entity_deletion_requests'::regclass",
15+
)
16+
.fetch_one(pool)
17+
.await
18+
.expect("fences");
19+
assert_eq!(fences, 0);
20+
let foreign_keys: i64 = sqlx::query_scalar(
21+
"SELECT count(*) FROM pg_constraint
22+
WHERE conrelid = 'entity_deletion_requests'::regclass AND contype = 'f'",
23+
)
24+
.fetch_one(pool)
25+
.await
26+
.expect("foreign keys");
27+
assert_eq!(foreign_keys, 0);
28+
}
29+
1130
#[tokio::test]
1231
#[ignore = "requires Postgres"]
1332
async fn concurrent_submissions_converge_and_conflicts_preserve_request() {
@@ -86,25 +105,9 @@ async fn invalid_submissions_do_not_create_rows() {
86105

87106
#[tokio::test]
88107
#[ignore = "requires Postgres"]
89-
async fn desired_schema_registers_intake_without_community_fences() {
108+
async fn desired_schema_has_plain_intake_without_database_guards() {
90109
let store = store().await;
91-
let registered: bool = sqlx::query_scalar(
92-
"SELECT EXISTS (SELECT 1 FROM _operator_global_tables
93-
WHERE table_name = 'entity_deletion_requests')",
94-
)
95-
.fetch_one(&store.pool)
96-
.await
97-
.expect("registry");
98-
assert!(registered);
99-
let fences: i64 = sqlx::query_scalar(
100-
"SELECT count(*) FROM pg_trigger
101-
WHERE tgrelid = 'entity_deletion_requests'::regclass
102-
AND tgname LIKE 'community_write_fence_%'",
103-
)
104-
.fetch_one(&store.pool)
105-
.await
106-
.expect("fences");
107-
assert_eq!(fences, 0);
110+
assert_plain_schema(&store.pool).await;
108111
crate::Db::from_pool(store.pool.clone())
109112
.validate_deletion_catalog()
110113
.await
@@ -145,6 +148,15 @@ async fn migration_schema_entity_intake_preserves_existing_data() {
145148
crate::migration::run_migrations(&store.pool)
146149
.await
147150
.expect("upgrade");
151+
assert_plain_schema(&store.pool).await;
152+
let registered: bool = sqlx::query_scalar(
153+
"SELECT EXISTS (SELECT 1 FROM _operator_global_tables
154+
WHERE table_name = 'entity_deletion_requests')",
155+
)
156+
.fetch_one(&store.pool)
157+
.await
158+
.expect("registry");
159+
assert!(registered);
148160
let request = store
149161
.submit("demo:alice", "operator-a", None)
150162
.await

‎migrations/0068_entity_deletion_requests.sql‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
SET LOCAL lock_timeout = '5s';
2+
13
-- Synthetic request intake only; no deletion worker or user cleanup.
24

35
-- Operator-global intake for synthetic user-deletion requests.

‎scripts/reconcile-schema-after-pgschema.sql‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66
-- apply caller must run this idempotent script so fresh bootstraps converge on
77
-- the same live database contract as migration-managed databases.
88

9-
-- pgschema does not apply the operator-global registry seed.
10-
INSERT INTO _operator_global_tables (table_name, reason)
11-
VALUES ('entity_deletion_requests', 'Operator-global synthetic user-deletion intake')
12-
ON CONFLICT (table_name) DO NOTHING;
13-
14-
DO $$
15-
BEGIN
16-
IF NOT EXISTS (
17-
SELECT 1 FROM _operator_global_tables
18-
WHERE table_name = 'entity_deletion_requests'
19-
) THEN
20-
RAISE EXCEPTION 'entity_deletion_requests must be operator-global after pgschema apply';
21-
END IF;
22-
END $$;
23-
249
DO $$
2510
BEGIN
2611
IF NOT EXISTS (

0 commit comments

Comments
 (0)