From ee48fa727afd1927f20b5d0491344f4afb03bd2e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 18 Jul 2025 18:13:31 +0200 Subject: fix(crates/yt/db/insert/maintenance): Re-init --- crates/yt/src/storage/db/insert/maintenance.rs | 28 ++++++++++++++++++++++++++ crates/yt/src/storage/db/insert/mod.rs | 1 + 2 files changed, 29 insertions(+) create mode 100644 crates/yt/src/storage/db/insert/maintenance.rs diff --git a/crates/yt/src/storage/db/insert/maintenance.rs b/crates/yt/src/storage/db/insert/maintenance.rs new file mode 100644 index 0000000..6442c48 --- /dev/null +++ b/crates/yt/src/storage/db/insert/maintenance.rs @@ -0,0 +1,28 @@ +use crate::{ + app::App, + storage::db::{ + insert::Operations, + video::{Video, VideoStatus, VideoStatusMarker}, + }, +}; + +use anyhow::Result; + +/// Remove the downloaded paths from videos in the db, that no longer exist on the file system. +pub(crate) async fn clear_stale_downloaded_paths(app: &App) -> Result<()> { + let mut cached_videos = Video::in_states(app, &[VideoStatusMarker::Cached]).await?; + + let mut ops = Operations::new("DbMaintain: init"); + for vid in &mut cached_videos { + if let VideoStatus::Cached { cache_path, .. } = &vid.status { + if !cache_path.exists() { + vid.remove_download_path(&mut ops); + } + } else { + unreachable!("We only asked for cached videos.") + } + } + ops.commit(app).await?; + + Ok(()) +} diff --git a/crates/yt/src/storage/db/insert/mod.rs b/crates/yt/src/storage/db/insert/mod.rs index f1d464f..e83287a 100644 --- a/crates/yt/src/storage/db/insert/mod.rs +++ b/crates/yt/src/storage/db/insert/mod.rs @@ -6,6 +6,7 @@ use anyhow::Result; use log::trace; use sqlx::SqliteConnection; +pub(crate) mod maintenance; pub(crate) mod playlist; pub(crate) mod subscription; pub(crate) mod video; -- cgit 1.4.1