Skip to content

Commit b2bfb90

Browse files
buzz-db: test the partition-audit read-only check
SessionIdentity::is_read_only holds the check buzz-admin runs before partition-audit, with a table test of all four setting combinations. The docs now call the flags session settings: they do not show what the role is allowed to write. The session_identity Postgres test uses the fixture database instead of creating its own, so it needs no CREATEDB. The feed module doc names both mention-indexing paths: in the event transaction, and after commit for huddle mentions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Luke Tornquist <tornquist@squareup.com>
1 parent 5541c40 commit b2bfb90

4 files changed

Lines changed: 51 additions & 14 deletions

File tree

‎crates/buzz-admin/src/main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ async fn cmd_partition_audit(months_ahead: u32) -> Result<i32> {
426426
};
427427
let db = Db::new(&config).await?;
428428
let identity = db.session_identity().await?;
429-
if !identity.default_transaction_read_only || !identity.transaction_read_only {
429+
if !identity.is_read_only() {
430430
anyhow::bail!("partition-audit connection is not read-only");
431431
}
432432

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,11 @@ struct ReaderWarmReport {
608608
failed: u32,
609609
}
610610

611-
/// Which database a session reached and whether it can write, from
612-
/// [`Db::session_identity`].
611+
/// Which database a session reached and its read-only session settings,
612+
/// from [`Db::session_identity`].
613+
///
614+
/// The read-only flags are session settings. They do not show what the role
615+
/// is allowed to write.
613616
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
614617
pub struct SessionIdentity {
615618
/// `current_database()`.
@@ -624,6 +627,14 @@ pub struct SessionIdentity {
624627
pub transaction_read_only: bool,
625628
}
626629

630+
impl SessionIdentity {
631+
/// Whether both read-only settings are on: new transactions start
632+
/// read-only, and so does the current one.
633+
pub fn is_read_only(&self) -> bool {
634+
self.default_transaction_read_only && self.transaction_read_only
635+
}
636+
}
637+
627638
/// Snapshot of Postgres connection pool utilisation.
628639
#[derive(Debug, Clone, Copy)]
629640
pub struct DbPoolStats {
@@ -1914,11 +1925,11 @@ impl Db {
19141925
crate::search_outbox::enable(&self.pool).await
19151926
}
19161927

1917-
/// Report the database, role, schema, and read-only settings of a
1918-
/// writer-pool session.
1928+
/// Report the database, role, schema, and read-only session settings of
1929+
/// a writer-pool session.
19191930
///
1920-
/// Operator tools use this to prove which database they reached and that
1921-
/// the session cannot write.
1931+
/// Operator tools use this to show which database they reached and to
1932+
/// refuse to run unless [`SessionIdentity::is_read_only`] holds.
19221933
pub async fn session_identity(&self) -> Result<SessionIdentity> {
19231934
let (database, user, schema, default_transaction_read_only, transaction_read_only) =
19241935
sqlx::query_as(

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,9 +3965,9 @@ mod pool_release_postgres_tests;
39653965
#[tokio::test]
39663966
#[ignore = "requires Postgres"]
39673967
async fn session_identity_reports_database_and_read_only_state() {
3968-
let scratch = ScratchDb::create(&admin_url().await, "session_identity").await;
3968+
let database_url = crate::test_support::database_url();
39693969
let config = |read_only: bool| DbConfig {
3970-
database_url: scratch.url().to_owned(),
3970+
database_url: database_url.clone(),
39713971
max_connections: 1,
39723972
min_connections: 0,
39733973
default_transaction_read_only: read_only,
@@ -3979,17 +3979,17 @@ async fn session_identity_reports_database_and_read_only_state() {
39793979
.session_identity()
39803980
.await
39813981
.expect("read-only session identity");
3982-
let database = scratch
3983-
.url()
3982+
let database = database_url
39843983
.rsplit('/')
39853984
.next()
39863985
.and_then(|tail| tail.split('?').next())
3987-
.expect("scratch url names its database");
3986+
.expect("test url names its database");
39883987
assert_eq!(identity.database, database);
39893988
assert_eq!(identity.schema, "public");
39903989
assert!(!identity.user.is_empty());
39913990
assert!(identity.default_transaction_read_only);
39923991
assert!(identity.transaction_read_only);
3992+
assert!(identity.is_read_only());
39933993
read_only.pool.close().await;
39943994

39953995
let writer = Db::new(&config(false)).await.expect("connect writer Db");
@@ -3999,5 +3999,29 @@ async fn session_identity_reports_database_and_read_only_state() {
39993999
.expect("writer session identity");
40004000
assert!(!identity.default_transaction_read_only);
40014001
assert!(!identity.transaction_read_only);
4002+
assert!(!identity.is_read_only());
40024003
writer.pool.close().await;
40034004
}
4005+
4006+
#[test]
4007+
fn session_identity_is_read_only_only_when_both_settings_are_on() {
4008+
for (default_transaction_read_only, transaction_read_only, expected) in [
4009+
(true, true, true),
4010+
(true, false, false),
4011+
(false, true, false),
4012+
(false, false, false),
4013+
] {
4014+
let identity = SessionIdentity {
4015+
database: "buzz".to_owned(),
4016+
user: "buzz".to_owned(),
4017+
schema: "public".to_owned(),
4018+
default_transaction_read_only,
4019+
transaction_read_only,
4020+
};
4021+
assert_eq!(
4022+
identity.is_read_only(),
4023+
expected,
4024+
"default={default_transaction_read_only} current={transaction_read_only}"
4025+
);
4026+
}
4027+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
//! full-table scan with an indexed lookup, keeping feed queries
1515
//! sub-millisecond at scale (>100k events).
1616
//!
17-
//! **Phase 2 implemented**: the `event_mentions` table is populated by
18-
//! [`crate::Db::insert_mentions`] on every event insert. `query_mentions` and
17+
//! **Phase 2 implemented**: event writes populate the `event_mentions` table
18+
//! inside their own transaction (`insert_mentions_in_transaction`). Huddle
19+
//! mention events (kind 48101) index after commit through
20+
//! [`crate::Db::insert_mentions`]. `query_mentions` and
1921
//! `query_needs_action` now use `INNER JOIN event_mentions` instead of
2022
//! scanning tags JSON.
2123
//!

0 commit comments

Comments
 (0)