From e5a4589f9776b54e14989b625b2dc678f725fa62 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 13:46:39 +0100 Subject: [PATCH] fix(database): the audit prune's statements run in query spans prune_audit_chain ran its statements straight on the connection, bypassing run!, so a trace of a prune showed none of them. They now go through run! like every other repository query. --- crates/database/src/repositories/audit/mod.rs | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/crates/database/src/repositories/audit/mod.rs b/crates/database/src/repositories/audit/mod.rs index d1898b734..daddf3ce7 100644 --- a/crates/database/src/repositories/audit/mod.rs +++ b/crates/database/src/repositories/audit/mod.rs @@ -496,32 +496,42 @@ impl DatabaseTransaction<'_> { /// checkpoint's position. pub async fn prune_audit_chain(&mut self, through: u64) -> Result { let through = Unsigned::bind(through)?; - let connection = &mut *self.inner; - sqlx::query!("SELECT set_config('sourceorigin.audit_prune', 'on', true)") - .fetch_one(&mut *connection) - .await?; - Limit::STATEMENT - .at(PRUNE_STATEMENT_TIMEOUT) - .set(Scope::Transaction) - .fetch_one(&mut *connection) - .await?; - let deleted = sqlx::query!( - "DELETE FROM domain_events WHERE chain_position <= $1", - through - ) - .execute(&mut *connection) - .await? + let mut audit = self.audit(); + run!( + audit, + fetch_one, + sqlx::query!("SELECT set_config('sourceorigin.audit_prune', 'on', true)") + )?; + run!( + audit, + fetch_one, + Limit::STATEMENT + .at(PRUNE_STATEMENT_TIMEOUT) + .set(Scope::Transaction) + )?; + let deleted = run!( + audit, + execute, + sqlx::query!( + "DELETE FROM domain_events WHERE chain_position <= $1", + through + ) + )? .rows_affected(); - sqlx::query!( - "INSERT INTO audit_anchor (position) VALUES ($1) + run!( + audit, + execute, + sqlx::query!( + "INSERT INTO audit_anchor (position) VALUES ($1) ON CONFLICT (singleton) DO UPDATE SET position = EXCLUDED.position", - through - ) - .execute(&mut *connection) - .await?; - sqlx::query!("SELECT set_config('sourceorigin.audit_prune', 'off', true)") - .fetch_one(&mut *connection) - .await?; + through + ) + )?; + run!( + audit, + fetch_one, + sqlx::query!("SELECT set_config('sourceorigin.audit_prune', 'off', true)") + )?; Ok(deleted) } }