mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 13:38:15 -08:00
fix: do not update mbz id when one already exists (#123)
This commit is contained in:
parent
f51771bc34
commit
d61e814306
4 changed files with 99 additions and 12 deletions
|
|
@ -89,15 +89,19 @@ func createOrUpdateAlbumWithMbzReleaseID(ctx context.Context, d db.DB, opts Asso
|
|||
})
|
||||
if err == nil {
|
||||
l.Debug().Msgf("Found album %s, updating with MusicBrainz Release ID...", album.Title)
|
||||
err := d.UpdateAlbum(ctx, db.UpdateAlbumOpts{
|
||||
ID: album.ID,
|
||||
MusicBrainzID: opts.ReleaseMbzID,
|
||||
})
|
||||
if err != nil {
|
||||
l.Err(err).Msg("createOrUpdateAlbumWithMbzReleaseID: failed to update album with MusicBrainz Release ID")
|
||||
return nil, fmt.Errorf("createOrUpdateAlbumWithMbzReleaseID: %w", err)
|
||||
if album.MbzID == nil {
|
||||
err := d.UpdateAlbum(ctx, db.UpdateAlbumOpts{
|
||||
ID: album.ID,
|
||||
MusicBrainzID: opts.ReleaseMbzID,
|
||||
})
|
||||
if err != nil {
|
||||
l.Err(err).Msg("createOrUpdateAlbumWithMbzReleaseID: failed to update album with MusicBrainz Release ID")
|
||||
return nil, fmt.Errorf("createOrUpdateAlbumWithMbzReleaseID: %w", err)
|
||||
}
|
||||
l.Debug().Msgf("Updated album '%s' with MusicBrainz Release ID", album.Title)
|
||||
} else {
|
||||
l.Warn().Msgf("Attempted to update album %s with MusicBrainz ID, but an existing ID was already found", album.Title)
|
||||
}
|
||||
l.Debug().Msgf("Updated album '%s' with MusicBrainz Release ID", album.Title)
|
||||
|
||||
if opts.ReleaseGroupMbzID != uuid.Nil {
|
||||
aliases, err := opts.Mbzc.GetReleaseTitles(ctx, opts.ReleaseGroupMbzID)
|
||||
|
|
|
|||
|
|
@ -96,6 +96,19 @@ func matchArtistsByMBIDMappings(ctx context.Context, d db.DB, opts AssociateArti
|
|||
})
|
||||
if err == nil {
|
||||
l.Debug().Msgf("Artist '%s' found by Name", a.Artist)
|
||||
if artist.MbzID == nil {
|
||||
err := d.UpdateArtist(ctx, db.UpdateArtistOpts{
|
||||
ID: artist.ID,
|
||||
MusicBrainzID: a.Mbid,
|
||||
})
|
||||
if err != nil {
|
||||
l.Err(err).Msg("matchArtistsByMBIDMappings: failed to update artist with MusicBrainz ID")
|
||||
return nil, fmt.Errorf("matchArtistsByMBIDMappings: %w", err)
|
||||
}
|
||||
l.Debug().Msgf("Updated artist '%s' with MusicBrainz ID", artist.Name)
|
||||
} else {
|
||||
l.Warn().Msgf("Attempted to update artist %s with MusicBrainz ID, but an existing ID was already found", artist.Name)
|
||||
}
|
||||
err = d.UpdateArtist(ctx, db.UpdateArtistOpts{ID: artist.ID, MusicBrainzID: a.Mbid})
|
||||
if err != nil {
|
||||
l.Err(err).Msgf("matchArtistsByMBIDMappings: Failed to associate artist '%s' with MusicBrainz ID", artist.Name)
|
||||
|
|
|
|||
|
|
@ -61,10 +61,19 @@ func matchTrackByMbzID(ctx context.Context, d db.DB, opts AssociateTrackOpts) (*
|
|||
return nil, fmt.Errorf("matchTrackByMbzID: %w", err)
|
||||
}
|
||||
l.Debug().Msgf("Updating track '%s' with MusicBrainz ID %s", opts.TrackName, opts.TrackMbzID)
|
||||
err = d.UpdateTrack(ctx, db.UpdateTrackOpts{
|
||||
ID: track.ID,
|
||||
MusicBrainzID: opts.TrackMbzID,
|
||||
})
|
||||
if track.MbzID == nil || *track.MbzID == uuid.Nil {
|
||||
err := d.UpdateTrack(ctx, db.UpdateTrackOpts{
|
||||
ID: track.ID,
|
||||
MusicBrainzID: opts.TrackMbzID,
|
||||
})
|
||||
if err != nil {
|
||||
l.Err(err).Msg("matchArtistsByMBIDMappings: failed to update track with MusicBrainz ID")
|
||||
return nil, fmt.Errorf("matchArtistsByMBIDMappings: %w", err)
|
||||
}
|
||||
l.Debug().Msgf("Updated track '%s' with MusicBrainz ID", track.Title)
|
||||
} else {
|
||||
l.Warn().Msgf("Attempted to update track %s with MusicBrainz ID, but an existing ID was already found", track.Title)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("matchTrackByMbzID: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -282,6 +282,67 @@ func TestSubmitListen_MatchAllMbzIDs(t *testing.T) {
|
|||
assert.Equal(t, 1, count, "duplicate artist created")
|
||||
}
|
||||
|
||||
func TestSubmitListen_DoNotOverwriteMbzIDs(t *testing.T) {
|
||||
setupTestDataWithMbzIDs(t)
|
||||
|
||||
// artist gets matched with musicbrainz id
|
||||
// release gets matched with mbz id
|
||||
// track gets matched with mbz id
|
||||
|
||||
ctx := context.Background()
|
||||
mbzc := &mbz.MbzMockCaller{
|
||||
Artists: mbzArtistData,
|
||||
Releases: mbzReleaseData,
|
||||
Tracks: mbzTrackData,
|
||||
}
|
||||
artistMbzID := uuid.MustParse("10000000-0000-0000-0000-000000000000")
|
||||
releaseMbzID := uuid.MustParse("01000000-0000-0000-0000-000000000000")
|
||||
trackMbzID := uuid.MustParse("00100000-0000-0000-0000-000000000000")
|
||||
opts := catalog.SubmitListenOpts{
|
||||
MbzCaller: mbzc,
|
||||
ArtistNames: []string{"ATARASHII GAKKO!"},
|
||||
Artist: "ATARASHII GAKKO!",
|
||||
ArtistMbzIDs: []uuid.UUID{
|
||||
artistMbzID,
|
||||
},
|
||||
TrackTitle: "Tokyo Calling",
|
||||
RecordingMbzID: trackMbzID,
|
||||
ReleaseTitle: "AG! Calling",
|
||||
ReleaseMbzID: releaseMbzID,
|
||||
Time: time.Now(),
|
||||
UserID: 1,
|
||||
}
|
||||
|
||||
err := catalog.SubmitListen(ctx, store, opts)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify that the listen was saved
|
||||
exists, err := store.RowExists(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM listens
|
||||
WHERE track_id = $1
|
||||
)`, 1)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, exists, "expected listen row to exist")
|
||||
|
||||
// verify that track, release group, and artist are existing ones and not duplicates
|
||||
count, err := store.Count(ctx, `
|
||||
SELECT COUNT(*) FROM tracks_with_title WHERE musicbrainz_id = $1
|
||||
`, trackMbzID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count, "duplicate track created")
|
||||
count, err = store.Count(ctx, `
|
||||
SELECT COUNT(*) FROM releases_with_title WHERE musicbrainz_id = $1
|
||||
`, releaseMbzID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count, "duplicate release group created")
|
||||
count, err = store.Count(ctx, `
|
||||
SELECT COUNT(*) FROM artists_with_name WHERE musicbrainz_id = $1
|
||||
`, artistMbzID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count, "duplicate artist created")
|
||||
}
|
||||
|
||||
func TestSubmitListen_MatchTrackFromMbzTitle(t *testing.T) {
|
||||
setupTestDataSansMbzIDs(t)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue