Skip to content

Commit 4ca7e82

Browse files
committed
fix: Distinguish between tracks with the same title but different
track_ids
1 parent be92c4e commit 4ca7e82

1 file changed

Lines changed: 37 additions & 32 deletions

File tree

internal/catalog/associate_track.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,48 @@ func matchTrackByTrackInfo(ctx context.Context, d db.TrackStore, opts AssociateT
8787
ArtistIDs: opts.ArtistIDs,
8888
})
8989
if err == nil {
90-
l.Debug().Msgf("Track '%s' found by title, release and artist match", track.Title)
91-
return track, nil
90+
if opts.TrackMbzID == uuid.Nil || track.MbzID == nil || *track.MbzID == uuid.Nil || *track.MbzID == opts.TrackMbzID {
91+
l.Debug().Msgf("Track '%s' found by title, release and artist match", track.Title)
92+
return track, nil
93+
}
94+
// Two distinct recordings sharing same title, fall through to create a new track
95+
l.Debug().Msgf("Track '%s' found but has a different MusicBrainz ID (%s != %s)",
96+
track.Title, track.MbzID.String(), opts.TrackMbzID.String())
9297
} else if !errors.Is(err, db.ErrNotFound) {
9398
return nil, fmt.Errorf("matchTrackByTrackInfo: %w", err)
94-
} else {
95-
if opts.TrackMbzID != uuid.Nil {
96-
mbzTrack, err := opts.Mbzc.GetTrack(ctx, opts.TrackMbzID)
99+
}
100+
101+
if opts.TrackMbzID != uuid.Nil {
102+
mbzTrack, err := opts.Mbzc.GetTrack(ctx, opts.TrackMbzID)
103+
if err == nil {
104+
track, err := d.GetTrack(ctx, db.GetTrackOpts{
105+
Title: mbzTrack.Title,
106+
ReleaseID: opts.AlbumID,
107+
ArtistIDs: opts.ArtistIDs,
108+
})
97109
if err == nil {
98-
track, err := d.GetTrack(ctx, db.GetTrackOpts{
99-
Title: mbzTrack.Title,
100-
ReleaseID: opts.AlbumID,
101-
ArtistIDs: opts.ArtistIDs,
102-
})
103-
if err == nil {
104-
if track.MbzID == nil || *track.MbzID == uuid.Nil || *track.MbzID == opts.TrackMbzID {
105-
l.Debug().Msgf("Track '%s' found by MusicBrainz title, release and artist match", opts.TrackName)
106-
return track, nil
107-
}
110+
if track.MbzID == nil || *track.MbzID == uuid.Nil || *track.MbzID == opts.TrackMbzID {
111+
l.Debug().Msgf("Track '%s' found by MusicBrainz title, release and artist match", opts.TrackName)
112+
return track, nil
108113
}
109114
}
110115
}
111-
l.Debug().Msgf("Track '%s' could not be found by title and artist match", opts.TrackName)
112-
t, err := d.SaveTrack(ctx, db.SaveTrackOpts{
113-
RecordingMbzID: opts.TrackMbzID,
114-
AlbumID: opts.AlbumID,
115-
Title: opts.TrackName,
116-
ArtistIDs: opts.ArtistIDs,
117-
Duration: opts.Duration,
118-
})
119-
if err != nil {
120-
return nil, fmt.Errorf("matchTrackByTrackInfo: %w", err)
121-
}
122-
if opts.TrackMbzID == uuid.Nil {
123-
l.Info().Msgf("Created track '%s' with title and artist", opts.TrackName)
124-
} else {
125-
l.Info().Msgf("Created track '%s' with MusicBrainz Recording ID", opts.TrackName)
126-
}
127-
return t, nil
128116
}
117+
l.Debug().Msgf("Track '%s' could not be found by title and artist match", opts.TrackName)
118+
t, err := d.SaveTrack(ctx, db.SaveTrackOpts{
119+
RecordingMbzID: opts.TrackMbzID,
120+
AlbumID: opts.AlbumID,
121+
Title: opts.TrackName,
122+
ArtistIDs: opts.ArtistIDs,
123+
Duration: opts.Duration,
124+
})
125+
if err != nil {
126+
return nil, fmt.Errorf("matchTrackByTrackInfo: %w", err)
127+
}
128+
if opts.TrackMbzID == uuid.Nil {
129+
l.Info().Msgf("Created track '%s' with title and artist", opts.TrackName)
130+
} else {
131+
l.Info().Msgf("Created track '%s' with MusicBrainz Recording ID", opts.TrackName)
132+
}
133+
return t, nil
129134
}

0 commit comments

Comments
 (0)