From fe0e8deca4ca43a3f5c22929180f8eadc24adca0 Mon Sep 17 00:00:00 2001 From: 4cuck <40539855+4cuck@users.noreply.github.com> Date: Sun, 22 Oct 2023 01:28:41 +0200 Subject: [PATCH] fixup crash on empty DB --- scanner/src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scanner/src/lib.rs b/scanner/src/lib.rs index e1e3505..be9bc57 100644 --- a/scanner/src/lib.rs +++ b/scanner/src/lib.rs @@ -221,12 +221,23 @@ impl Scanner { /// Retrieves the last uptime fetch that happened pub async fn query_last_fetch(db: &DatabaseConnection) -> Result> { - let time = health_check::Entity::find().column_as(health_check::Column::Time.max(), health_check::Column::Time) + #[derive(Debug, FromQueryResult, Default)] + pub(crate) struct TimeMax { + pub max: Option, + } + // can't use find().column_as(health_check::Column::Time.max(), health_check::Column::Time) + // will crash on an empty DB + let time_max = TimeMax::find_by_statement(Statement::from_sql_and_values( + DbBackend::Sqlite, + r#"SELECT MAX(time) FROM health_check"#, + [], + )) .one(db) .await? - .map(|model| model.time) + .map(|model| model.max) + .flatten() .unwrap_or_default(); - Ok(Utc.timestamp_opt(time,0).unwrap()) + Ok(Utc.timestamp_opt(time_max,0).unwrap()) } pub async fn query_latest_check(