diff --git a/src/rust/lektor_db/src/connexion.rs b/src/rust/lektor_db/src/connexion.rs index 11af90b9395eb8dbe6d66ddce33663cf24288ec3..0727f8e95e7b3e985e561a473504ade0bcfa30a0 100644 --- a/src/rust/lektor_db/src/connexion.rs +++ b/src/rust/lektor_db/src/connexion.rs @@ -212,8 +212,30 @@ impl LktDatabaseConnection { }) } - pub fn make_kara_available<'a>(&mut self, remote_id: RemoteKaraId) -> LktDatabaseResult<()> { - todo!() + /// Makes a kara avilable, set the avail flag to `true` or `false` depending on the passed + /// flag. If the operation changed nothing, don't raise an error. + pub fn make_kara_available<'a>( + &mut self, + remote_id: RemoteKaraId, + flag: bool, + ) -> LktDatabaseResult<()> { + let RemoteKaraId { + repo_id: rid, + repo_kara_id: rkid, + } = remote_id; + self.sqlite.exclusive_transaction(|c| { + let local_kara_id: i64 = with_dsl!(repo_kara => repo_kara + .select(local_kara_id) + .filter(repo_id.eq(rid).and(repo_kara_id.eq(rkid))) + .first(c)? + ); + with_dsl!(kara => diesel::update(kara) + .filter(id.eq(local_kara_id)) + .set(is_dl.eq(flag)) + .execute(c)? + ); + Ok(()) + }) } /// Create a series of models from a kara signature from Kurisu's V1 API. diff --git a/src/rust/lektor_db/src/models.rs b/src/rust/lektor_db/src/models.rs index f07ed82013bc797f2a57b076c84c6a5ce4d4ffb6..07e7bc3b24c1857d39b531da31842df180605643 100644 --- a/src/rust/lektor_db/src/models.rs +++ b/src/rust/lektor_db/src/models.rs @@ -18,7 +18,7 @@ pub struct KaraId { pub local_kara_id: i64, } -#[derive(Debug, Queryable, Selectable)] +#[derive(Debug, Queryable, Selectable, Clone, Copy)] #[diesel(table_name = repo_kara)] pub struct RemoteKaraId { pub repo_id: i64, diff --git a/src/rust/lektor_repo/src/download.rs b/src/rust/lektor_repo/src/download.rs index 1d3698e599659df9fc34aa6568a8b7c34767aa37..a84718693c76666d03f3388de193801b3ae7a0ce 100644 --- a/src/rust/lektor_repo/src/download.rs +++ b/src/rust/lektor_repo/src/download.rs @@ -225,24 +225,42 @@ impl Download { search_urls: &[&str], dest: impl AsRef<Path>, ) { - let (dest, name, db) = (dest.as_ref(), name.as_ref(), self.db.clone()); + macro_rules! make_kara_available { + ($db: expr, $id: expr, $flag: expr) => {{ + let mut db = $db.lock().expect("failed to lock the database..."); + if let Err(err) = db.make_kara_available($id, $flag) { + log::error!( + target: "REPO", + "failed to make kara `{repo_kara_id}` from {} {}: {err}", + name.as_ref(), + either!($flag => "available"; "unavailable") + ); + return; + } + }}; + } + + let (remote_id, db, dest) = ( + RemoteKaraId { + repo_id, + repo_kara_id, + }, + self.db.clone(), + dest.as_ref(), + ); + + make_kara_available!(db, remote_id, false); if let Err(err) = Download::download_kara(db.clone(), api, search_urls, repo_id, repo_kara_id, dest).await { log::error!(target: "REPO", "failed to download file `{}` for kara {repo_kara_id}: {err}", dest.to_string_lossy()); return; } - let mut db = db.lock().expect("failed to lock the database..."); - let remote_id = RemoteKaraId { - repo_id, - repo_kara_id, - }; - if let Err(err) = db.make_kara_available(remote_id) { - log::error!(target: "REPO", "failed to make kara `{repo_kara_id}` from {name} available: {err}"); - return; - } + make_kara_available!(db, remote_id, true); + log::info!( - "downloaded kara {repo_kara_id} from {name} at location {}", + "downloaded kara {repo_kara_id} from {} at location {}", + name.as_ref(), dest.to_string_lossy() ); }