Skip to content

Commit fcb55ab

Browse files
refactor(db): move maintenance store ownership
Signed-off-by: tornquist <[email protected]>
1 parent 7dbb399 commit fcb55ab

4 files changed

Lines changed: 62 additions & 27 deletions

File tree

‎crates/buzz-db/src/event.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,27 @@ impl Db {
16871687
Ok(result)
16881688
}
16891689

1690+
/// Backfill `d_tag` for existing NIP-33 events (kind 30000–39999) that have `d_tag IS NULL`.
1691+
///
1692+
/// Idempotent — safe to call on every startup. No-ops when all rows are already populated.
1693+
/// Runs a single UPDATE touching only NIP-33 rows with NULL d_tag.
1694+
#[datastore_span(name = "backfill_d_tags", system = "postgresql")]
1695+
pub async fn backfill_d_tags(&self) -> Result<u64> {
1696+
let result = sqlx::query(
1697+
"UPDATE events \
1698+
SET d_tag = COALESCE( \
1699+
(SELECT elem->>1 FROM jsonb_array_elements(tags) AS elem \
1700+
WHERE elem->>0 = 'd' LIMIT 1), \
1701+
'' \
1702+
) \
1703+
WHERE kind BETWEEN 30000 AND 39999 AND d_tag IS NULL \
1704+
AND community_write_allowed(community_id)",
1705+
)
1706+
.execute(&self.pool)
1707+
.await?;
1708+
Ok(result.rows_affected())
1709+
}
1710+
16901711
/// Soft-delete NIP-29 discovery events for a channel created by a specific relay pubkey.
16911712
#[datastore_span(name = "soft_delete_discovery_events", system = "postgresql")]
16921713
pub async fn soft_delete_discovery_events(

‎crates/buzz-db/src/lib.rs‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,33 +1138,6 @@ impl Db {
11381138
}
11391139
}
11401140
}
1141-
1142-
/// Ensures monthly partitions exist for the next N months.
1143-
#[datastore_span(name = "ensure_future_partitions", system = "postgresql")]
1144-
pub async fn ensure_future_partitions(&self, months_ahead: u32) -> Result<()> {
1145-
partition::ensure_future_partitions(&self.pool, months_ahead).await
1146-
}
1147-
1148-
/// Backfill `d_tag` for existing NIP-33 events (kind 30000–39999) that have `d_tag IS NULL`.
1149-
///
1150-
/// Idempotent — safe to call on every startup. No-ops when all rows are already populated.
1151-
/// Runs a single UPDATE touching only NIP-33 rows with NULL d_tag.
1152-
#[datastore_span(name = "backfill_d_tags", system = "postgresql")]
1153-
pub async fn backfill_d_tags(&self) -> Result<u64> {
1154-
let result = sqlx::query(
1155-
"UPDATE events \
1156-
SET d_tag = COALESCE( \
1157-
(SELECT elem->>1 FROM jsonb_array_elements(tags) AS elem \
1158-
WHERE elem->>0 = 'd' LIMIT 1), \
1159-
'' \
1160-
) \
1161-
WHERE kind BETWEEN 30000 AND 39999 AND d_tag IS NULL \
1162-
AND community_write_allowed(community_id)",
1163-
)
1164-
.execute(&self.pool)
1165-
.await?;
1166-
Ok(result.rows_affected())
1167-
}
11681141
}
11691142

11701143
#[cfg(test)]

‎crates/buzz-db/src/partition.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
//!
33
//! Call `ensure_future_partitions` on startup and monthly via cron.
44
5+
use buzz_datastore_tracing::datastore_span;
56
use chrono::{Datelike, TimeZone, Utc};
67
use sqlx::{PgPool, Row};
78
use tracing::info;
89

910
use crate::error::{DbError, Result};
11+
use crate::Db;
1012

1113
/// Tables that may be partition-managed. Allowlist prevents DDL injection.
1214
const PARTITIONED_TABLES: &[&str] = &["events", "delivery_log"];
@@ -55,6 +57,14 @@ pub async fn ensure_future_partitions(pool: &PgPool, months_ahead: u32) -> Resul
5557
Ok(())
5658
}
5759

60+
impl Db {
61+
/// Ensures monthly partitions exist for the next N months.
62+
#[datastore_span(name = "ensure_future_partitions", system = "postgresql")]
63+
pub async fn ensure_future_partitions(&self, months_ahead: u32) -> Result<()> {
64+
ensure_future_partitions(&self.pool, months_ahead).await
65+
}
66+
}
67+
5868
/// Validate that a partition suffix is digits and underscores only.
5969
fn validate_partition_suffix(suffix: &str) -> bool {
6070
!suffix.is_empty() && suffix.chars().all(|c| c.is_ascii_digit() || c == '_')

‎crates/buzz-db/tests/store_ownership.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,3 +1152,34 @@ fn admin_moderation_store_has_single_ownership() {
11521152
assert_eq!(count(store, &declaration), 1);
11531153
}
11541154
}
1155+
1156+
#[test]
1157+
fn maintenance_store_facades_have_single_ownership() {
1158+
let root = include_str!("../src/lib.rs");
1159+
let partition = include_str!("../src/partition.rs");
1160+
let event = include_str!("../src/event.rs");
1161+
1162+
assert_eq!(
1163+
count(root, "pub async fn ensure_future_partitions("),
1164+
0,
1165+
"partition facade remains in lib.rs"
1166+
);
1167+
assert_eq!(
1168+
count(partition, "pub async fn ensure_future_partitions("),
1169+
2,
1170+
"partition module must own one Db facade and one pool operation"
1171+
);
1172+
assert_eq!(count(partition, "name = \"ensure_future_partitions\""), 1);
1173+
1174+
assert_eq!(
1175+
count(root, "pub async fn backfill_d_tags("),
1176+
0,
1177+
"event backfill remains in lib.rs"
1178+
);
1179+
assert_eq!(
1180+
count(event, "pub async fn backfill_d_tags("),
1181+
1,
1182+
"event module must own the backfill facade"
1183+
);
1184+
assert_eq!(count(event, "name = \"backfill_d_tags\""), 1);
1185+
}

0 commit comments

Comments
 (0)