refactor(db): declare unixepoch instead of formatting a SQL fragment
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
db::{
|
db::{
|
||||||
self,
|
self,
|
||||||
|
functions::unixepoch,
|
||||||
models::{
|
models::{
|
||||||
NewProposal, NewProposalVote, NewRecoveryProposalVote, NewRecoveryWakeupRequest,
|
NewProposal, NewProposalVote, NewRecoveryProposalVote, NewRecoveryWakeupRequest,
|
||||||
OperatorIdentityId, Proposal, ProposalId, ProposalStatus, RecoveryOperatorIdentityId,
|
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.
|
/// 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 cutoff = diesel::dsl::sql::<diesel::sql_types::Integer>(&format!(
|
|
||||||
"unixepoch('now') - {}",
|
|
||||||
Self::WAKEUP_DELAY_SECS
|
|
||||||
));
|
|
||||||
|
|
||||||
select(exists(
|
select(exists(
|
||||||
schema::recovery_wakeup_request::table
|
schema::recovery_wakeup_request::table
|
||||||
.filter(schema::recovery_wakeup_request::cancelled_at.is_null())
|
.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)
|
.get_result(conn)
|
||||||
.await
|
.await
|
||||||
|
|||||||
12
server/crates/arbiter-server/src/db/functions.rs
Normal file
12
server/crates/arbiter-server/src/db/functions.rs
Normal 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;
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
|
pub mod functions;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod proposal;
|
pub mod proposal;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|||||||
Reference in New Issue
Block a user