refactor(db): use diesel exists() instead of counting rows

This commit is contained in:
CleverWild
2026-08-27 13:50:02 +02:00
parent 71081b6ee7
commit c0546fa17f

View File

@@ -16,7 +16,10 @@ use crate::{
}, },
}; };
use chrono::Utc; use chrono::Utc;
use diesel::{ExpressionMethods as _, QueryDsl}; use diesel::{
ExpressionMethods as _, QueryDsl,
dsl::{exists, select},
};
use diesel_async::{AsyncConnection as _, RunQueryDsl}; use diesel_async::{AsyncConnection as _, RunQueryDsl};
use kameo::{Actor, actor::ActorRef, messages}; use kameo::{Actor, actor::ActorRef, messages};
use strum::IntoDiscriminant as _; use strum::IntoDiscriminant as _;
@@ -216,13 +219,14 @@ impl ProposalManager {
})?; })?;
// Check for duplicate vote before status check so AlreadyVoted takes priority // Check for duplicate vote before status check so AlreadyVoted takes priority
let existing: i64 = schema::proposal_vote::table let already_voted: bool = select(exists(
schema::proposal_vote::table
.filter(schema::proposal_vote::proposal_id.eq(proposal_id)) .filter(schema::proposal_vote::proposal_id.eq(proposal_id))
.filter(schema::proposal_vote::operator_id.eq(operator_id)) .filter(schema::proposal_vote::operator_id.eq(operator_id)),
.count() ))
.get_result(&mut conn) .get_result(&mut conn)
.await?; .await?;
if existing > 0 { if already_voted {
return Err(Error::AlreadyVoted); return Err(Error::AlreadyVoted);
} }
@@ -429,13 +433,16 @@ impl ProposalManager {
return Err(Error::RecoveryNotActive); return Err(Error::RecoveryNotActive);
} }
let existing: i64 = schema::recovery_proposal_vote::table 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::proposal_id.eq(proposal_id))
.filter(schema::recovery_proposal_vote::recovery_operator_id.eq(recovery_operator_id)) .filter(
.count() schema::recovery_proposal_vote::recovery_operator_id.eq(recovery_operator_id),
),
))
.get_result(&mut conn) .get_result(&mut conn)
.await?; .await?;
if existing > 0 { if already_voted {
return Err(Error::AlreadyVoted); return Err(Error::AlreadyVoted);
} }
@@ -547,30 +554,29 @@ impl ProposalManager {
/// Returns true when an uncancelled wakeup request has passed the 14-day dispute window. /// 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<bool, Error> { async fn is_recovery_active_conn(conn: &mut db::DatabaseConnection) -> Result<bool, Error> {
let count: i64 = schema::recovery_wakeup_request::table let cutoff = diesel::dsl::sql::<diesel::sql_types::Integer>(&format!(
.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') - {}", "unixepoch('now') - {}",
Self::WAKEUP_DELAY_SECS Self::WAKEUP_DELAY_SECS
))), ));
)
.count() 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) .get_result(conn)
.await?; .await
Ok(count > 0) .map_err(Error::from)
} }
/// Returns true when there is any uncancelled wakeup request (pending or active). /// Returns true when there is any uncancelled wakeup request (pending or active).
async fn has_uncancelled_wakeup(conn: &mut db::DatabaseConnection) -> Result<bool, Error> { async fn has_uncancelled_wakeup(conn: &mut db::DatabaseConnection) -> Result<bool, Error> {
let count: i64 = schema::recovery_wakeup_request::table select(exists(schema::recovery_wakeup_request::table.filter(
.filter(schema::recovery_wakeup_request::cancelled_at.is_null()) schema::recovery_wakeup_request::cancelled_at.is_null(),
.count() )))
.get_result(conn) .get_result(conn)
.await?; .await
Ok(count > 0) .map_err(Error::from)
} }
async fn execute_proposal(&self, proposal: &Proposal) -> Result<(), Error> { async fn execute_proposal(&self, proposal: &Proposal) -> Result<(), Error> {