From f2f05bdba3bbcedbbc643d8d3f1122fe0b4a3ab2 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 May 2021 03:25:59 +0200 Subject: Check if repo was updated based on `updated_at` timestamp Find out if the latest copy is more recent than the cached value in our database. --- src/database.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src/database.rs') diff --git a/src/database.rs b/src/database.rs index 085494e..375ca3b 100644 --- a/src/database.rs +++ b/src/database.rs @@ -72,7 +72,11 @@ impl Db { let mut tx = self.connection.begin().await?; // NOTE: Returns `RowNotFound` if not found. - let row = sqlx::query("SELECT id, name FROM repositories where id = ?") + let row = sqlx::query(r#" + SELECT id, name, updated_at + FROM repositories + WHERE id = ? + "#) .bind(id) .fetch_one(&mut tx) .await?; @@ -83,7 +87,7 @@ impl Db { Repo { id: row.get(0), name: Some(row.get(1)), - updated_at: None, + updated_at: Some(row.get(2)), } ) } @@ -108,9 +112,31 @@ impl Db { Ok(()) } - pub fn repo_is_updated() -> Result { - // select id from repositories where updated_at > datetime("2020-07-13T17:57:56Z"); - Ok(false) + pub async fn repo_is_updated( + &mut self, + repo: &Repo, + ) -> Result { + let mut tx = self.connection.begin().await?; + + let is_updated = match sqlx::query(r#" + SELECT 1 + FROM repositories + WHERE id = ? + AND datetime(updated_at) < datetime(?) + "#) + .bind(repo.id) + .bind(&repo.updated_at) + .fetch_one(&mut tx) + .await + { + Ok(r) => Ok(true), + Err(sqlx::Error::RowNotFound) => Ok(false), + Err(e) => Err(e.into()), + }; + + tx.commit().await?; + + is_updated } pub fn repo_update(repo: &Repo) -> Result<(), Error> { -- cgit v1.2.3