35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
use chrono::{DateTime, NaiveDateTime, Utc};
|
|
use diesel::result::Error;
|
|
use diesel::{Connection, QueryDsl, RunQueryDsl, SqliteConnection};
|
|
use persistence::model::NewCloudObjectsRefresh;
|
|
use persistence::schema;
|
|
|
|
pub fn record_time_of_next_refresh(
|
|
conn: &mut SqliteConnection,
|
|
timestamp: DateTime<Utc>,
|
|
) -> Result<(), Error> {
|
|
use schema::cloud_objects_refreshes::dsl::*;
|
|
let refresh = NewCloudObjectsRefresh {
|
|
time_of_next_refresh: timestamp.naive_utc(),
|
|
};
|
|
conn.transaction::<(), Error, _>(|conn| {
|
|
diesel::delete(cloud_objects_refreshes).execute(conn)?;
|
|
diesel::insert_into(cloud_objects_refreshes)
|
|
.values(refresh)
|
|
.execute(conn)?;
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
pub fn read_time_of_next_force_object_refresh(
|
|
conn: &mut SqliteConnection,
|
|
) -> Result<Option<DateTime<Utc>>, Error> {
|
|
// Find the smallest refresh timestamp to pass into CloudModel.
|
|
Ok(cloud_objects_refreshes
|
|
.select(time_of_next_refresh)
|
|
.load::<NaiveDateTime>(conn)?
|
|
.into_iter()
|
|
.map(|refresh| refresh.and_utc())
|
|
.min())
|
|
}
|