fix: address code review findings

This commit is contained in:
2026-07-19 19:52:27 +03:00
parent a7803615cd
commit 0bee9b9b27
6 changed files with 138 additions and 31 deletions

View File

@@ -120,13 +120,16 @@ func IsTypeIncluded(releaseType string) bool {
// MBID) must NOT be stored here, because artist_settings is keyed by the
// Navidrome ID and the foreign key / join would otherwise never match.
func (rg *ReleaseGroup) ToExternalRelease(artistID string) *database.ExternalRelease {
// Only the primary type is persisted; secondary types are used transiently
// for filtering above and are not stored in the external_releases schema.
// The primary type and the secondary types are both persisted so that the
// cache-hit path in SyncArtistDiscography can re-apply the same
// IgnoreSingles / IgnoreCompilations rules (which consider secondary types)
// as the cache-miss path, keeping results stable across cache refreshes.
return &database.ExternalRelease{
RGID: rg.ID,
ArtistID: artistID,
Title: rg.Title,
Type: rg.Type,
ReleaseDate: rg.ReleaseDate,
RGID: rg.ID,
ArtistID: artistID,
Title: rg.Title,
Type: rg.Type,
ReleaseDate: rg.ReleaseDate,
SecondaryTypes: rg.SecondaryTypes,
}
}

View File

@@ -59,10 +59,10 @@ func SyncArtistDiscography(
}
filtered := make([]database.ExternalRelease, 0, len(cachedReleases))
for _, r := range cachedReleases {
if opts.IgnoreSingles && r.Type == "Single" {
if opts.IgnoreSingles && (r.Type == "Single" || hasSliceType(r.SecondaryTypes, "Single")) {
continue
}
if opts.IgnoreCompilations && r.Type == "Compilation" {
if opts.IgnoreCompilations && (r.Type == "Compilation" || hasSliceType(r.SecondaryTypes, "Compilation")) {
continue
}
filtered = append(filtered, r)
@@ -152,6 +152,18 @@ func SyncArtistDiscography(
return releases, nil
}
// hasSliceType reports whether the slice contains the wanted value. It mirrors
// hasSecondaryType in api.go but operates on the persisted []string form read
// back from external_releases (cache-hit path).
func hasSliceType(types []string, wanted string) bool {
for _, t := range types {
if t == wanted {
return true
}
}
return false
}
// getArtistFilterOptions reads per-artist type filtering preferences.
// Defaults to no filtering if artist_settings row doesn't exist.
// artistID is the Navidrome artist ID (artist_settings.id), not the MusicBrainz ID.

View File

@@ -827,8 +827,50 @@ func TestSyncArtistDiscography_IgnoreCompilations(t *testing.T) {
}
// -----------------------------------------------------------------------
// Test: resync with notifications_sent does not violate FK constraint
// Test: cache-hit path applies secondary-type filtering consistently with the
// cache-miss path. A release whose primary type is "Album" but which is also
// a "Compilation" via its secondary type must be dropped by IgnoreCompilations
// on a cache hit, exactly as FilterReleaseGroups drops it on a cache miss.
// -----------------------------------------------------------------------
func TestSyncArtistDiscography_CacheHit_IgnoreSecondaryCompilation(t *testing.T) {
artistID := "nav-comp-secondary-test"
artistName := "Secondary Comp Artist"
db := newTestDB(t)
defer db.Close()
if _, err := db.Conn().Exec(
"INSERT INTO artist_settings (id, name, ignore_compilations, monitored) VALUES (?, ?, 1, 1)",
artistID, artistName,
); err != nil {
t.Fatalf("seed artist: %v", err)
}
// Seed a cached release: primary "Album" + secondary "Compilation".
// cached_at is set far in the past so it is still within any TTL (TTL 0).
if err := database.SaveExternalRelease(db, &database.ExternalRelease{
RGID: "rg-comp",
ArtistID: artistID,
Title: "Greatest Hits",
Type: "Album",
SecondaryTypes: []string{"Compilation"},
IsIgnored: false,
CachedAt: time.Now().UTC(),
}); err != nil {
t.Fatalf("seed cached release: %v", err)
}
// No MusicBrainz server is started; a cache hit must not hit the API.
client := newTestClient("http://unused.invalid")
ctx := context.Background()
releases, err := SyncArtistDiscography(ctx, client, db, artistID, "mbid-unused", 0)
if err != nil {
t.Fatalf("SyncArtistDiscography() error: %v", err)
}
if len(releases) != 0 {
t.Fatalf("expected 0 releases (secondary compilation filtered on cache hit), got %d", len(releases))
}
}
func TestSyncArtistDiscography_ResyncWithNotifications(t *testing.T) {
artistMBID := "artist-fk-test"
artistID := "nav-fk-test"