From 2564e6bf4f7a5e16d0db018f0c7b9622cd004976 Mon Sep 17 00:00:00 2001 From: Broque Thomas <26755000+Nezreka@users.noreply.github.com> Date: Wed, 18 Mar 2026 10:38:06 -0700 Subject: [PATCH] Fix deep scan wiping track enrichment data and not updating file paths INSERT OR REPLACE on existing tracks was deleting the entire row and reinserting only 9 columns, nuking spotify_track_id, deezer_id, isrc, bpm, musicbrainz IDs, and ~15 other enrichment columns every scan. Now uses UPDATE for existing tracks (preserves all enrichment) and INSERT only for new tracks. Also ensures file_path gets updated from the media server on each scan, fixing stale paths for users whose files were moved/reorganized. --- database/music_database.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/database/music_database.py b/database/music_database.py index 7513de75..d7627287 100644 --- a/database/music_database.py +++ b/database/music_database.py @@ -3730,16 +3730,27 @@ class MusicDatabase: if file_path is None and hasattr(track_obj, 'suffix') and track_obj.suffix: file_path = f"{track_obj.title}.{track_obj.suffix}" - # Check if this is a genuinely new track (for history logging) + # Check if track already exists — UPDATE to preserve enrichment columns, + # INSERT only for genuinely new tracks cursor.execute("SELECT 1 FROM tracks WHERE id = ? LIMIT 1", (track_id,)) is_new_track = cursor.fetchone() is None - # Use INSERT OR REPLACE to handle duplicate IDs gracefully - cursor.execute(""" - INSERT OR REPLACE INTO tracks - (id, album_id, artist_id, title, track_number, duration, file_path, bitrate, server_source, updated_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) - """, (track_id, album_id, artist_id, title, track_number, duration, file_path, bitrate, server_source)) + if is_new_track: + cursor.execute(""" + INSERT INTO tracks + (id, album_id, artist_id, title, track_number, duration, file_path, bitrate, server_source, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) + """, (track_id, album_id, artist_id, title, track_number, duration, file_path, bitrate, server_source)) + else: + # Update server-provided fields only — preserves spotify_track_id, deezer_id, + # isrc, bpm, musicbrainz IDs, and all other enrichment data + cursor.execute(""" + UPDATE tracks + SET album_id = ?, artist_id = ?, title = ?, track_number = ?, + duration = ?, file_path = ?, bitrate = ?, server_source = ?, + updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, (album_id, artist_id, title, track_number, duration, file_path, bitrate, server_source, track_id)) conn.commit()