From c0546fa17fffef7fe5753df0acf7d74d484c9e33 Mon Sep 17 00:00:00 2001 From: CleverWild Date: Thu, 27 Aug 2026 13:50:02 +0200 Subject: [PATCH] refactor(db): use diesel exists() instead of counting rows --- .../src/actors/proposal_manager.rs | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/server/crates/arbiter-server/src/actors/proposal_manager.rs b/server/crates/arbiter-server/src/actors/proposal_manager.rs index 1be894d..8f59928 100644 --- a/server/crates/arbiter-server/src/actors/proposal_manager.rs +++ b/server/crates/arbiter-server/src/actors/proposal_manager.rs @@ -16,7 +16,10 @@ use crate::{ }, }; use chrono::Utc; -use diesel::{ExpressionMethods as _, QueryDsl}; +use diesel::{ + ExpressionMethods as _, QueryDsl, + dsl::{exists, select}, +}; use diesel_async::{AsyncConnection as _, RunQueryDsl}; use kameo::{Actor, actor::ActorRef, messages}; use strum::IntoDiscriminant as _; @@ -216,13 +219,14 @@ impl ProposalManager { })?; // Check for duplicate vote before status check so AlreadyVoted takes priority - let existing: i64 = schema::proposal_vote::table - .filter(schema::proposal_vote::proposal_id.eq(proposal_id)) - .filter(schema::proposal_vote::operator_id.eq(operator_id)) - .count() - .get_result(&mut conn) - .await?; - if existing > 0 { + let already_voted: bool = select(exists( + schema::proposal_vote::table + .filter(schema::proposal_vote::proposal_id.eq(proposal_id)) + .filter(schema::proposal_vote::operator_id.eq(operator_id)), + )) + .get_result(&mut conn) + .await?; + if already_voted { return Err(Error::AlreadyVoted); } @@ -429,13 +433,16 @@ impl ProposalManager { return Err(Error::RecoveryNotActive); } - let existing: i64 = schema::recovery_proposal_vote::table - .filter(schema::recovery_proposal_vote::proposal_id.eq(proposal_id)) - .filter(schema::recovery_proposal_vote::recovery_operator_id.eq(recovery_operator_id)) - .count() - .get_result(&mut conn) - .await?; - if existing > 0 { + let already_voted: bool = select(exists( + schema::recovery_proposal_vote::table + .filter(schema::recovery_proposal_vote::proposal_id.eq(proposal_id)) + .filter( + schema::recovery_proposal_vote::recovery_operator_id.eq(recovery_operator_id), + ), + )) + .get_result(&mut conn) + .await?; + if already_voted { return Err(Error::AlreadyVoted); } @@ -547,30 +554,29 @@ impl ProposalManager { /// Returns true when an uncancelled wakeup request has passed the 14-day dispute window. async fn is_recovery_active_conn(conn: &mut db::DatabaseConnection) -> Result { - let count: i64 = schema::recovery_wakeup_request::table - .filter(schema::recovery_wakeup_request::cancelled_at.is_null()) - .filter( - schema::recovery_wakeup_request::requested_at.le(diesel::dsl::sql::< - diesel::sql_types::Integer, - >(&format!( - "unixepoch('now') - {}", - Self::WAKEUP_DELAY_SECS - ))), - ) - .count() - .get_result(conn) - .await?; - Ok(count > 0) + let cutoff = diesel::dsl::sql::(&format!( + "unixepoch('now') - {}", + Self::WAKEUP_DELAY_SECS + )); + + select(exists( + schema::recovery_wakeup_request::table + .filter(schema::recovery_wakeup_request::cancelled_at.is_null()) + .filter(schema::recovery_wakeup_request::requested_at.le(cutoff)), + )) + .get_result(conn) + .await + .map_err(Error::from) } /// Returns true when there is any uncancelled wakeup request (pending or active). async fn has_uncancelled_wakeup(conn: &mut db::DatabaseConnection) -> Result { - let count: i64 = schema::recovery_wakeup_request::table - .filter(schema::recovery_wakeup_request::cancelled_at.is_null()) - .count() - .get_result(conn) - .await?; - Ok(count > 0) + select(exists(schema::recovery_wakeup_request::table.filter( + schema::recovery_wakeup_request::cancelled_at.is_null(), + ))) + .get_result(conn) + .await + .map_err(Error::from) } async fn execute_proposal(&self, proposal: &Proposal) -> Result<(), Error> {