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
9 changes: 7 additions & 2 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ Relay-owned write APIs provide the supported mutation path for tenant data:
checks active, undeleted state. Deletion lifecycle transitions take the
matching exclusive lock. A serving transaction never spans communities.
The universal per-table write-fence triggers and their functions have been
retired after writer coverage and fleet gates; the community tombstone guard
remains. New tenant-owned tables rely on this admission alone.
retired after writer coverage and fleet gates. New tenant-owned tables rely
on this admission alone.
- **Replica floor:** supported channel-event insertions check `created_at`
against `clock_timestamp()` on their admitted transaction using the shared
960-second floor. The ordered heartbeat handshake and activity scan retain
Expand All @@ -511,6 +511,11 @@ Relay-owned write APIs provide the supported mutation path for tenant data:
Drain old connections/in-flight transactions, stop out-of-band writers, and
hold the replica breaker closed through the drop until replay catches up.
Already-running old pods do not reverify the trigger on each heartbeat.
- **Single-table guards:** channel tenant binding, community lifecycle,
request/approval evidence and manifest chunks are owned by application writers.
Serving/deletion catalog validation tolerates the five legacy guards being
absent. Their contract migration is gated on the writer and catalog-expand
release (#8320) being deployed fleet-wide.
- **Role separation:** relay/runtime code owns admission and lock protocols;
operator maintenance/backfill workflows must either use those protocols or run
under explicit reviewed procedures that keep routing fences closed.
Expand Down
112 changes: 76 additions & 36 deletions crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,17 +844,6 @@ mod postgres_tests {
.collect()
}

fn has_channels_community_id_immutability_guard(sql: &str) -> bool {
let normalized = normalize_sql(sql);
normalized.contains("create trigger")
&& normalized.contains("before update")
&& normalized.contains(" on channels")
&& normalized.contains("community_id")
&& normalized.contains("old.community_id")
&& normalized.contains("new.community_id")
&& normalized.contains("raise exception")
}

fn forbidden_channels_community_id_mutations(sql: &str) -> Vec<String> {
split_sql_statements(sql)
.into_iter()
Expand Down Expand Up @@ -890,7 +879,7 @@ mod postgres_tests {
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
migrations.sort_by_key(|migration| migration.version);

assert_eq!(migrations.len(), 95);
assert_eq!(migrations.len(), 96);
assert_eq!(migrations[89].version, 90);
assert!(migrations[89]
.sql
Expand Down Expand Up @@ -929,6 +918,12 @@ mod postgres_tests {
for (index, migration) in migrations.iter().enumerate() {
assert_eq!(migration.version, (index + 1) as i64);
}
// 0096 retires the five single-table guard triggers and functions.
assert_eq!(migrations[95].version, 96);
assert!(migrations[95]
.sql
.as_str()
.contains("SET LOCAL lock_timeout = '5s'"));
assert!(migrations[79]
.sql
.as_str()
Expand Down Expand Up @@ -2213,10 +2208,6 @@ mod postgres_tests {
"channels.community_id must not be re-tenanted after insert; forbidden migration statements:\n{}",
forbidden_mutations.join("\n---\n")
);
assert!(
has_channels_community_id_immutability_guard(sql),
"migrations define channels.community_id but no BEFORE UPDATE trigger/function guard that rejects OLD.community_id <> NEW.community_id was found"
);
}

#[test]
Expand Down Expand Up @@ -2485,16 +2476,9 @@ mod postgres_tests {
.expect("embedded migration 0029")
.sql
.as_ref();
let migration_0051: &str = MIGRATOR
.iter()
.find(|migration| migration.version == 51)
.expect("embedded migration 0051")
.sql
.as_ref();
let schema_sql = crate::test_support::desired_state_schema_sql();

let migration = surface(migration_0029);
let owner_admission_migration = surface(migration_0051);
let schema = surface(&schema_sql);

assert_eq!(
Expand Down Expand Up @@ -2562,6 +2546,11 @@ mod postgres_tests {
"enforce_community_write_fence",
"attach_community_write_fence",
"community_deletion_lock_key",
// 0096 retires the four deletion guard functions.
"enforce_community_tombstone",
"prevent_community_deletion_request_retargeting",
"prevent_community_deletion_approval_removal",
"protect_community_deletion_manifest_keys",
]
.contains(&function.as_str())
{
Expand All @@ -2579,17 +2568,6 @@ mod postgres_tests {
);
}
}
assert_eq!(
schema
.functions
.get("prevent_community_deletion_request_retargeting")
.expect("schema.sql deletion retargeting guard"),
owner_admission_migration
.functions
.get("prevent_community_deletion_request_retargeting")
.expect("0051 deletion retargeting guard"),
"schema.sql must carry the latest immutable owner-provenance guard"
);
let request_table = schema
.tables
.get("community_deletion_requests")
Expand All @@ -2607,6 +2585,16 @@ mod postgres_tests {
);
}
for (trigger, definition) in &migration.triggers {
if matches!(
trigger.as_str(),
"communities_deletion_tombstone"
| "community_deletion_request_retargeting_guard"
| "community_deletion_approval_removal_guard"
| "community_deletion_manifest_keys_guard"
) {
assert!(!schema.triggers.contains_key(trigger));
continue;
}
let in_schema = schema
.triggers
.get(trigger)
Expand Down Expand Up @@ -3214,6 +3202,57 @@ mod postgres_tests {
schema/ does not declare (or the reverse)"
);
}
// Compare columns, keys and remaining triggers on the five guarded tables;
// foreign keys must survive 0096 unchanged.
for table in [
"channels",
"communities",
"community_deletion_requests",
"community_deletion_approvals",
"community_deletion_manifest_keys",
] {
assert_eq!(
columns(&desired, table).await,
columns(&migrated, table).await,
"single-table guard column parity: {table}"
);
// pgschema rewrites CHECK expressions (e.g. BETWEEN) into an
// equivalent tree with different grouping. Compare key/FK shapes
// here; existing CHECK assertions above remain unchanged.
let desired_keys: Vec<_> = constraints(&desired, table)
.await
.into_iter()
.filter(|(_, definition, _)| !definition.starts_with("CHECK"))
.collect();
let migrated_keys: Vec<_> = constraints(&migrated, table)
.await
.into_iter()
.filter(|(_, definition, _)| !definition.starts_with("CHECK"))
.collect();
assert_eq!(
desired_keys, migrated_keys,
"single-table guard key parity: {table}"
);
let query = "SELECT pg_get_triggerdef(oid) FROM pg_trigger \
WHERE tgrelid = $1::regclass AND NOT tgisinternal ORDER BY tgname";
let desired_triggers: Vec<String> = sqlx::query_scalar(query)
.bind(table)
.fetch_all(&desired)
.await
.expect("desired table triggers");
let migrated_triggers: Vec<String> = sqlx::query_scalar(query)
.bind(table)
.fetch_all(&migrated)
.await
.expect("migrated table triggers");
assert_eq!(
desired_triggers, migrated_triggers,
"remaining trigger parity: {table}"
);
}
// Shared with the isolated desired-template and migration regressions.
crate::deletion::postgres_tests::assert_single_table_guards_retired(&desired).await;
crate::deletion::postgres_tests::assert_single_table_guards_retired(&migrated).await;
}

/// Re-applying `schema/schema.sql` to a database bootstrapped the way every
Expand Down Expand Up @@ -3974,8 +4013,9 @@ mod postgres_tests {
)
.fetch_one(&pool)
.await
.expect("read retained guard");
assert!(tombstone);
.expect("read retired guard");
// 0096 retired the tombstone trigger.
assert!(!tombstone);
store
.validate_serving_catalog()
.await
Expand Down
Loading
Loading