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