From 6387fd598ed65f5320ddc0f0871824cc3ee5b8b8 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Tue, 20 Jan 2026 01:05:32 -0600 Subject: [PATCH] Fix download repair to handle scanner-damaged tracks Problem: Repair was failing for 42 tracks because it only searched by qobuz_track_id, which was lost when scanner replaced downloads. Root cause: Scanner's INSERT OR REPLACE doesn't include qobuz_track_id in its column list, so it gets set to NULL during replacement. Previous repair logic couldn't find tracks without qobuz_track_id. Solution: Multi-strategy repair approach: 1. Search by qobuz_track_id first (intact downloads) 2. If not found, search by file_path (scanner-damaged downloads) 3. When found by path, restore BOTH qobuz_track_id AND source New database functions: - repair_qobuz_download_by_path(): Restores both ID and source - track_exists_by_path(): Check existence by file path Updated backfill logic: - Try match by ID (for intact downloads with lost source) - Try match by path (for scanner-damaged downloads) - Insert as new only if not found by either method This should successfully repair all 42 damaged downloads by matching them via file_path from download_cache. Files changed: - src-tauri/src/library/database.rs - src-tauri/src/library/commands.rs --- src-tauri/src/library/commands.rs | 53 ++++++++++++++++++++++++------- src-tauri/src/library/database.rs | 30 +++++++++++++---- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src-tauri/src/library/commands.rs b/src-tauri/src/library/commands.rs index cc86c273f..fc3ce7cf4 100644 --- a/src-tauri/src/library/commands.rs +++ b/src-tauri/src/library/commands.rs @@ -1461,31 +1461,60 @@ pub async fn library_backfill_downloads( let library_db = state.db.lock().await; for (track_id, title, artist, album, duration_secs, file_path, bit_depth, sample_rate) in downloads { - // Check if already in library - let exists = library_db + // Strategy: Try to match by qobuz_track_id first, then by file_path + // This handles both intact downloads and downloads damaged by scanner + + let exists_by_id = library_db .track_exists_by_qobuz_id(track_id) .unwrap_or(false); - if exists { - // Track exists - try to repair its source marker if it's missing - match library_db.repair_qobuz_download_source(track_id) { + let exists_by_path = library_db + .track_exists_by_path(&file_path) + .unwrap_or(false); + + if exists_by_id { + // Track exists with correct qobuz_track_id (not damaged) + // Check if it just needs source repair + match library_db.is_qobuz_download_by_path(&file_path) { Ok(true) => { - log::info!("Repaired source marker for track {}: {}", track_id, title); - report.repaired_tracks += 1; - }, - Ok(false) => { - // Already has correct source, nothing to do + // Already marked as qobuz_download, nothing to do report.skipped_tracks += 1; }, + Ok(false) => { + // Has qobuz_track_id but lost source marker - unusual case + log::info!("Repairing source for track with intact ID {}: {}", track_id, title); + match library_db.repair_qobuz_download_by_path(track_id, &file_path) { + Ok(true) => report.repaired_tracks += 1, + Ok(false) => report.skipped_tracks += 1, + Err(e) => { + log::warn!("Failed to repair track {}: {}", track_id, e); + report.failed_tracks.push(title); + } + } + }, Err(e) => { - log::warn!("Failed to repair track {}: {}", track_id, e); + log::warn!("Failed to check download status for {}: {}", track_id, e); report.failed_tracks.push(title); } } continue; } - // Insert into library with source = 'qobuz_download' + if exists_by_path { + // Track exists by path but lost qobuz_track_id (damaged by scanner) + log::info!("Repairing damaged download (lost ID) {}: {}", track_id, title); + match library_db.repair_qobuz_download_by_path(track_id, &file_path) { + Ok(true) => report.repaired_tracks += 1, + Ok(false) => report.skipped_tracks += 1, + Err(e) => { + log::warn!("Failed to repair track by path {}: {}", track_id, e); + report.failed_tracks.push(title); + } + } + continue; + } + + // Track doesn't exist - insert as new match library_db.insert_qobuz_download_direct( track_id, &title, diff --git a/src-tauri/src/library/database.rs b/src-tauri/src/library/database.rs index 609f00e38..2e4eed1e0 100644 --- a/src-tauri/src/library/database.rs +++ b/src-tauri/src/library/database.rs @@ -2005,18 +2005,36 @@ impl LibraryDatabase { Ok(count > 0) } - /// Repair the source field for a track that has qobuz_track_id but lost its source marker + /// Repair a track by file_path - restores both qobuz_track_id and source + /// This handles tracks that were damaged by scanner's INSERT OR REPLACE /// Returns true if the track was found and updated - pub fn repair_qobuz_download_source(&self, qobuz_track_id: u64) -> Result { + pub fn repair_qobuz_download_by_path( + &self, + qobuz_track_id: u64, + file_path: &str, + ) -> Result { let updated = self.conn.execute( - "UPDATE local_tracks SET source = 'qobuz_download' - WHERE qobuz_track_id = ?1 AND (source IS NULL OR source != 'qobuz_download')", - params![qobuz_track_id as i64], + "UPDATE local_tracks + SET source = 'qobuz_download', qobuz_track_id = ?1 + WHERE file_path = ?2 AND (source IS NULL OR source != 'qobuz_download')", + params![qobuz_track_id as i64, file_path], ) - .map_err(|e| LibraryError::Database(format!("Failed to repair download source: {}", e)))?; + .map_err(|e| LibraryError::Database(format!("Failed to repair download by path: {}", e)))?; Ok(updated > 0) } + /// Check if a track exists by file path (for repair matching) + pub fn track_exists_by_path(&self, file_path: &str) -> Result { + let count: i64 = self.conn + .query_row( + "SELECT COUNT(*) FROM local_tracks WHERE file_path = ?1", + params![file_path], + |row| row.get(0), + ) + .map_err(|e| LibraryError::Database(e.to_string()))?; + Ok(count > 0) + } + /// Insert a Qobuz download into the library pub fn insert_qobuz_download_direct( &self,