diff options
author | Teddy Wing | 2021-05-30 03:25:59 +0200 |
---|---|---|
committer | Teddy Wing | 2021-05-30 03:25:59 +0200 |
commit | f2f05bdba3bbcedbbc643d8d3f1122fe0b4a3ab2 (patch) | |
tree | 626735e18611882cf287297cf59905dffa226b80 /src/database.rs | |
parent | 1c191a68165c70a6d0d8f4d5b9e01412266c8c25 (diff) | |
download | reflectub-f2f05bdba3bbcedbbc643d8d3f1122fe0b4a3ab2.tar.bz2 |
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.
Diffstat (limited to 'src/database.rs')
-rw-r--r-- | src/database.rs | 36 |
1 files changed, 31 insertions, 5 deletions
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<bool, Error> { - // 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<bool, Error> { + 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> { |