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.
pull/253/head
Broque Thomas 2 months ago
parent f66eecebf5
commit 2564e6bf4f

@ -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()

Loading…
Cancel
Save