mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 21:48:18 -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,6 +89,7 @@ func createOrUpdateAlbumWithMbzReleaseID(ctx context.Context, d db.DB, opts Asso
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
l.Debug().Msgf("Found album %s, updating with MusicBrainz Release ID...", album.Title)
|
l.Debug().Msgf("Found album %s, updating with MusicBrainz Release ID...", album.Title)
|
||||||
|
if album.MbzID == nil {
|
||||||
err := d.UpdateAlbum(ctx, db.UpdateAlbumOpts{
|
err := d.UpdateAlbum(ctx, db.UpdateAlbumOpts{
|
||||||
ID: album.ID,
|
ID: album.ID,
|
||||||
MusicBrainzID: opts.ReleaseMbzID,
|
MusicBrainzID: opts.ReleaseMbzID,
|
||||||
|
|
@ -98,6 +99,9 @@ func createOrUpdateAlbumWithMbzReleaseID(ctx context.Context, d db.DB, opts Asso
|
||||||
return nil, fmt.Errorf("createOrUpdateAlbumWithMbzReleaseID: %w", err)
|
return nil, fmt.Errorf("createOrUpdateAlbumWithMbzReleaseID: %w", err)
|
||||||
}
|
}
|
||||||
l.Debug().Msgf("Updated album '%s' with MusicBrainz Release ID", album.Title)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
if opts.ReleaseGroupMbzID != uuid.Nil {
|
if opts.ReleaseGroupMbzID != uuid.Nil {
|
||||||
aliases, err := opts.Mbzc.GetReleaseTitles(ctx, opts.ReleaseGroupMbzID)
|
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 {
|
if err == nil {
|
||||||
l.Debug().Msgf("Artist '%s' found by Name", a.Artist)
|
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})
|
err = d.UpdateArtist(ctx, db.UpdateArtistOpts{ID: artist.ID, MusicBrainzID: a.Mbid})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Err(err).Msgf("matchArtistsByMBIDMappings: Failed to associate artist '%s' with MusicBrainz ID", artist.Name)
|
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)
|
return nil, fmt.Errorf("matchTrackByMbzID: %w", err)
|
||||||
}
|
}
|
||||||
l.Debug().Msgf("Updating track '%s' with MusicBrainz ID %s", opts.TrackName, opts.TrackMbzID)
|
l.Debug().Msgf("Updating track '%s' with MusicBrainz ID %s", opts.TrackName, opts.TrackMbzID)
|
||||||
err = d.UpdateTrack(ctx, db.UpdateTrackOpts{
|
if track.MbzID == nil || *track.MbzID == uuid.Nil {
|
||||||
|
err := d.UpdateTrack(ctx, db.UpdateTrackOpts{
|
||||||
ID: track.ID,
|
ID: track.ID,
|
||||||
MusicBrainzID: opts.TrackMbzID,
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("matchTrackByMbzID: %w", err)
|
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")
|
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) {
|
func TestSubmitListen_MatchTrackFromMbzTitle(t *testing.T) {
|
||||||
setupTestDataSansMbzIDs(t)
|
setupTestDataSansMbzIDs(t)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue