@@ -31,11 +31,13 @@ use std::time::Instant;
3131use anyhow:: Result ;
3232use buzz_core:: kind:: KIND_NIP43_MEMBERSHIP_LIST ;
3333use buzz_core:: tenant:: { relay_url_authority, TenantContext } ;
34- use buzz_db:: { Db , DbConfig } ;
34+ use buzz_db:: { partition :: PartitionAuditReport , Db , DbConfig } ;
3535use buzz_media:: { BucketSnapshot , MediaConfig , MediaStorage , S3AddressingStyle , SweepError } ;
3636use buzz_pubsub:: { EventTopic , PubSubManager } ;
3737use clap:: { Parser , Subcommand } ;
3838use nostr:: { EventBuilder , Keys , Kind , Tag } ;
39+ use serde:: Serialize ;
40+ use sqlx:: Row ;
3941use tracing:: warn;
4042
4143#[ derive( Parser ) ]
@@ -89,6 +91,12 @@ enum Command {
8991 #[ arg( long, default_value_t = 10_000_000 ) ]
9092 max_objects : u64 ,
9193 } ,
94+ /// Run the partition catalog audit using a read-only database session.
95+ PartitionAudit {
96+ /// Future months to include in the coverage check.
97+ #[ arg( long, default_value_t = 3 ) ]
98+ months_ahead : u32 ,
99+ } ,
92100 /// Inspect deployment-wide Buzz product feedback.
93101 ProductFeedback {
94102 #[ command( subcommand) ]
@@ -166,6 +174,7 @@ async fn run(cli: Cli) -> Result<i32> {
166174 Ok ( 0 )
167175 }
168176 Command :: StorageSnapshot { max_objects } => cmd_storage_snapshot ( max_objects) . await ,
177+ Command :: PartitionAudit { months_ahead } => cmd_partition_audit ( months_ahead) . await ,
169178 Command :: AddMember { pubkey, role } => cmd_add_member ( pubkey, role) . await ,
170179 Command :: RemoveMember { pubkey, role } => cmd_remove_member ( pubkey, role) . await ,
171180 Command :: ListMembers => cmd_list_members ( ) . await ,
@@ -314,6 +323,94 @@ fn storage_config_from_env() -> Result<MediaConfig> {
314323 } )
315324}
316325
326+ #[ derive( Serialize ) ]
327+ struct PartitionAuditIdentity {
328+ database : String ,
329+ user : String ,
330+ schema : String ,
331+ default_transaction_read_only : bool ,
332+ transaction_read_only : bool ,
333+ }
334+
335+ #[ derive( Serialize ) ]
336+ struct PartitionAuditOutput {
337+ schema_version : u32 ,
338+ mode : & ' static str ,
339+ source_sha : & ' static str ,
340+ build_id : & ' static str ,
341+ build_url : & ' static str ,
342+ outcome : & ' static str ,
343+ months_ahead : u32 ,
344+ identity : PartitionAuditIdentity ,
345+ report : PartitionAuditReport ,
346+ }
347+
348+ async fn cmd_partition_audit ( months_ahead : u32 ) -> Result < i32 > {
349+ let db_url = std:: env:: var ( "DATABASE_URL" )
350+ . map_err ( |_| anyhow:: anyhow!( "DATABASE_URL is required for partition-audit" ) ) ?;
351+ let config = DbConfig {
352+ database_url : db_url,
353+ max_connections : 1 ,
354+ min_connections : 1 ,
355+ statement_timeout_ms : 30_000 ,
356+ default_transaction_read_only : true ,
357+ ..DbConfig :: default ( )
358+ } ;
359+ let pool = Db :: connect_writer_pool ( & config) . await ?;
360+ let row = sqlx:: query (
361+ "SELECT current_database() AS database, current_user AS user, \
362+ current_schema() AS schema, \
363+ current_setting('default_transaction_read_only') = 'on' \
364+ AS default_transaction_read_only, \
365+ current_setting('transaction_read_only') = 'on' AS transaction_read_only",
366+ )
367+ . fetch_one ( & pool)
368+ . await ?;
369+ let identity = PartitionAuditIdentity {
370+ database : row. try_get ( "database" ) ?,
371+ user : row. try_get ( "user" ) ?,
372+ schema : row. try_get ( "schema" ) ?,
373+ default_transaction_read_only : row. try_get ( "default_transaction_read_only" ) ?,
374+ transaction_read_only : row. try_get ( "transaction_read_only" ) ?,
375+ } ;
376+ if !identity. default_transaction_read_only || !identity. transaction_read_only {
377+ anyhow:: bail!( "partition-audit connection is not read-only" ) ;
378+ }
379+
380+ let report = Db :: from_pool ( pool)
381+ . audit_partitions_report ( months_ahead)
382+ . await ;
383+ let outcome = if !report. errors . is_empty ( ) {
384+ "error"
385+ } else if !report. serving_safe ( ) {
386+ "unsafe"
387+ } else if report. tables . iter ( ) . any ( |table| table. degraded ( ) ) {
388+ "degraded"
389+ } else {
390+ "ok"
391+ } ;
392+ let code = match outcome {
393+ "error" => 5 ,
394+ "unsafe" => 2 ,
395+ _ => 0 ,
396+ } ;
397+ println ! (
398+ "{}" ,
399+ serde_json:: to_string_pretty( & PartitionAuditOutput {
400+ schema_version: 1 ,
401+ mode: "read_only" ,
402+ source_sha: option_env!( "BUZZ_SOURCE_SHA" ) . unwrap_or( "unknown" ) ,
403+ build_id: option_env!( "BUZZ_BUILD_ID" ) . unwrap_or( "local" ) ,
404+ build_url: option_env!( "BUZZ_BUILD_URL" ) . unwrap_or( "unknown" ) ,
405+ outcome,
406+ months_ahead,
407+ identity,
408+ report,
409+ } ) ?
410+ ) ;
411+ Ok ( code)
412+ }
413+
317414async fn cmd_add_member ( pubkey_arg : String , role : String ) -> Result < i32 > {
318415 if let Err ( msg) = validate_role ( & role) {
319416 eprintln ! ( "error: {msg}" ) ;
@@ -581,8 +678,7 @@ async fn connect_db() -> Result<Db> {
581678}
582679
583680fn db_config_from_env ( ) -> DbConfig {
584- let db_url = std:: env:: var ( "DATABASE_URL" )
585- . unwrap_or_else ( |_| "postgres://buzz:buzz_dev@localhost:5432/buzz" . to_string ( ) ) ;
681+ let db_url = std:: env:: var ( "DATABASE_URL" ) . unwrap_or_else ( |_| DbConfig :: default ( ) . database_url ) ;
586682 DbConfig {
587683 database_url : db_url,
588684 ..DbConfig :: default ( )
@@ -801,3 +897,18 @@ mod storage_snapshot_tests {
801897 assert_eq ! ( persist_calls. load( Ordering :: SeqCst ) , 0 ) ;
802898 }
803899}
900+
901+ #[ cfg( test) ]
902+ mod tests {
903+ use super :: * ;
904+
905+ #[ test]
906+ fn parses_partition_audit_month_horizon ( ) {
907+ let cli = Cli :: try_parse_from ( [ "buzz-admin" , "partition-audit" , "--months-ahead" , "6" ] )
908+ . expect ( "parse partition-audit command" ) ;
909+ assert ! ( matches!(
910+ cli. command,
911+ Command :: PartitionAudit { months_ahead: 6 }
912+ ) ) ;
913+ }
914+ }
0 commit comments