From 05756d95ccf692dd50439a787dcea8622024901d Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Wed, 25 Mar 2026 21:50:09 -0400 Subject: [PATCH] I **think** I've at least 99% fixed the top songs mismatch --- src/tagger.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/tagger.rs b/src/tagger.rs index 2246a0f..881fd56 100644 --- a/src/tagger.rs +++ b/src/tagger.rs @@ -1,4 +1,4 @@ -use sea_orm::{ActiveValue::NotSet, ActiveValue::Set, DatabaseConnection}; +use sea_orm::{ActiveModelTrait, ActiveValue::NotSet, ActiveValue::Set, DatabaseConnection}; use shanty_data::{MetadataFetcher as MetadataProvider, RecordingDetails, ReleaseRef}; use shanty_db::entities::track; use shanty_db::queries; @@ -78,6 +78,22 @@ pub async fn tag_track( return Ok(true); } + // If the resolved MBID differs from the track's original MBID, update the wanted_item + // so the cleanup step doesn't delete files whose MBID changed during tagging + if let Some(ref old_mbid) = track.musicbrainz_id + && old_mbid != &details.mbid + && let Ok(Some(wanted)) = queries::wanted::find_by_mbid(conn, old_mbid).await + { + let mut active: shanty_db::entities::wanted_item::ActiveModel = wanted.into(); + active.musicbrainz_id = Set(Some(details.mbid.clone())); + let _ = active.update(conn).await; + tracing::info!( + old_mbid = %old_mbid, + new_mbid = %details.mbid, + "updated wanted_item MBID after tagger fallback" + ); + } + // Use existing artist_id if already set (e.g., from download pipeline). // Only upsert from MB when the track has no artist association yet. let artist_id = if track.artist_id.is_some() { @@ -121,14 +137,22 @@ pub async fn tag_track( let genre = details.genres.first().cloned(); - // Update track metadata + // Update track metadata — preserve artist name when artist_id was already set + let artist_name_for_track = if track.artist_id.is_some() { + track + .artist + .clone() + .or_else(|| Some(details.artist.clone())) + } else { + Some(details.artist.clone()) + }; let active = track::ActiveModel { id: Set(track.id), file_path: Set(track.file_path.clone()), title: Set(Some(details.title.clone())), - artist: Set(Some(details.artist.clone())), + artist: Set(artist_name_for_track.clone()), album: Set(album_name), - album_artist: Set(Some(details.artist.clone())), + album_artist: Set(artist_name_for_track), musicbrainz_id: Set(Some(details.mbid.clone())), artist_id: Set(artist_id), album_id: Set(album_id),