refactor(db): declare unixepoch instead of formatting a SQL fragment

This commit is contained in:
CleverWild
2026-08-27 14:08:48 +02:00
parent e9496da78c
commit 0d29d0d532
3 changed files with 18 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ use crate::{
},
db::{
self,
functions::unixepoch,
models::{
NewProposal, NewProposalVote, NewRecoveryProposalVote, NewRecoveryWakeupRequest,
OperatorIdentityId, Proposal, ProposalId, ProposalStatus, RecoveryOperatorIdentityId,
@@ -570,15 +571,13 @@ 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<bool, Error> {
let cutoff = diesel::dsl::sql::<diesel::sql_types::Integer>(&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)),
.filter(
schema::recovery_wakeup_request::requested_at
.le(unixepoch("now") - Self::WAKEUP_DELAY_SECS),
),
))
.get_result(conn)
.await

View File

@@ -0,0 +1,12 @@
//! Typed bindings for the SQLite scalar functions used in Diesel expressions.
use diesel::sql_types::{Integer, Text};
diesel::define_sql_function! {
/// SQLite `unixepoch(modifier)` -- seconds since the Unix epoch.
///
/// Declared so timestamp comparisons are built by the query DSL instead of by
/// `format!`-ing a SQL fragment: the argument becomes a bind parameter and the
/// result type is checked against the column it is compared with.
fn unixepoch(modifier: Text) -> Integer;
}

View File

@@ -8,6 +8,7 @@ use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
use thiserror::Error;
use tracing::info;
pub mod functions;
pub mod models;
pub mod proposal;
pub mod schema;