diff --git a/README.md b/README.md index 27c391e..572fbf1 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ The key is stored in the database, in clear text, and is never returned by the A **Tags.** `GET /api/v1/artists/{id}/tags` asks each provider that is on, at the same time, and keeps the answers with the same rules (7 days, 1 for an unknown artist, old data if a provider fails). Each provider has its own entry with its own weights: Last.fm's are relative to the artist (1 to 100), MusicBrainz's are counts of votes, and they are never mixed. MusicBrainz also marks the tags it lists as genres. Its tags and genres are CC BY-NC-SA, so show the `attribution` that comes with them. -**Genres from your own files.** The scan also reads the genre tag of each file (several genres separated by `;`, at most five per file) and `GET /artists/{id}/tags` adds a `files` entry: the genres of the artist's playable tracks, each weighted by how many tracks have it. It needs no provider, asks nobody, and is always there. +**Genres from your own files.** The scan also reads the genre tag of each file (several genres separated by `;`, at most five per file) and `GET /artists/{id}/tags` adds a `files` entry: the genres of the playable tracks the artist is credited on, each weighted by how many tracks have it. It needs no provider, asks nobody, and is always there. **MusicBrainz ids come from your files, never from a guess.** If your files carry MusicBrainz artist ids in their tags (taggers like Picard write them), the scan reads them, offline, and keeps them on your artists, but only when each id can be paired with its artist for certain. MusicBrainz is asked only about artists whose id is known; for the others its entry says `no_id`. Sonda never searches MusicBrainz by name, because a search returns look-alikes and picking one would be a guess. A normal scan reads a file's tags when the file is new or changed. To pick up ids and genres for music that is already indexed, scan with `full=true` (`POST /library/scan?full=true`, or `POST /libraries/{id}/scan?full=true`): every file is read again, keeps its identity, and your listening history is untouched. diff --git a/internal/api/http/handlers_library.go b/internal/api/http/handlers_library.go index 0f95314..f4fe083 100644 --- a/internal/api/http/handlers_library.go +++ b/internal/api/http/handlers_library.go @@ -38,11 +38,11 @@ type scanStatusJSON struct { // fullParam reads ?full=. Only "true" and "false" are accepted, and it answers // 400 itself otherwise: a typo must not quietly start a scan of another kind. func fullParam(c *gin.Context) (full, ok bool) { - raw, present := c.GetQuery("full") + values := c.QueryArray("full") switch { - case !present, raw == "false": + case len(values) == 0, len(values) == 1 && values[0] == "false": return false, true - case raw == "true": + case len(values) == 1 && values[0] == "true": return true, true } writeError(c, http.StatusBadRequest, "invalid_input", "full must be true or false") diff --git a/internal/api/http/library_test.go b/internal/api/http/library_test.go index 4a056b5..4a48112 100644 --- a/internal/api/http/library_test.go +++ b/internal/api/http/library_test.go @@ -122,7 +122,7 @@ func TestScanCanBeAskedToReadEveryFileAgain(t *testing.T) { starts := scan.starts scan.running = false - for _, q := range []string{"?full=maybe", "?full=1", "?full=TRUE", "?full="} { + for _, q := range []string{"?full=maybe", "?full=1", "?full=TRUE", "?full=", "?full=true&full=bogus", "?full=false&full=true"} { for _, path := range []string{"/api/v1/library/scan", "/api/v1/libraries/7/scan"} { if r := do(t, h, "POST", path+q, tok, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("%s%s: %d %v, want 400 invalid_input", path, q, r.Code, r.Body) diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 44dbdd7..81b2617 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -618,12 +618,13 @@ paths: description: | What each provider says kind of music this artist plays: Last.fm's top tags and MusicBrainz's tags and genres, and last the genres of the artist's own music files - (`provider` is `files`: the genre tags the scan read, each weighted by how many playable - tracks have it; it asks nobody, is always `ok` and has a null `retrieved_at`). There is + (`provider` is `files`: the genre tags the scan read, each weighted by how many of the + playable tracks the artist is credited on have it; it asks nobody, is always `ok` and has a null `retrieved_at`). There is always one entry for each, Last.fm first. The weights are the providers' own numbers, kept as they came and never mixed: Last.fm's is relative to the artist (1 to 100, the top tag is usually 100; a tag with no votes is dropped), - MusicBrainz's is a count of votes. `genre` is true for a MusicBrainz tag that it also lists among its curated genres. + MusicBrainz's is a count of votes. `genre` is true for a MusicBrainz tag that it also lists among its curated genres, and for + every tag of the `files` entry, which are genres by definition. A provider is only asked when an admin switched it on and its stored answer is missing or old (7 days; a "no such artist" answer is kept 1 day), with the same rules as diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 388e995..d321443 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -200,6 +200,9 @@ func (s *Service) Similar(ctx context.Context, artistID int64, limit int) (Resul case <-ctx.Done(): return Result{}, ctx.Err() case r := <-done: + if errors.Is(r.Err, ErrNotFound) { + return Result{}, ErrNotFound // removed while it was being read + } if r.Err != nil { s.log.Warn("similar artists could not be read", zap.String("provider", Provider), zap.Error(r.Err)) return result(snap, StatusError, snap.Fetched), nil diff --git a/internal/discovery/discovery_test.go b/internal/discovery/discovery_test.go index 39166a7..5e05244 100644 --- a/internal/discovery/discovery_test.go +++ b/internal/discovery/discovery_test.go @@ -33,6 +33,7 @@ type fakeStore struct { snaps map[int64]Snapshot saves []saved lastLim int + saveErr error // returned by SaveSimilar when set } func newStore() *fakeStore { @@ -66,6 +67,9 @@ func (f *fakeStore) Similar(_ context.Context, id int64, _ string, limit int) (S func (f *fakeStore) SaveSimilar(_ context.Context, id int64, provider string, at time.Time, found bool, cands []lastfm.Similar) error { f.mu.Lock() defer f.mu.Unlock() + if f.saveErr != nil { + return f.saveErr + } f.saves = append(f.saves, saved{id, provider, at, found, cands}) snap := Snapshot{Fetched: true, NotFound: !found, RetrievedAt: at} for i, c := range cands { @@ -413,3 +417,13 @@ func TestNoPageCanAskForMoreThanIsStored(t *testing.T) { t.Errorf("MaxLimit = %d is above FetchLimit = %d", MaxLimit, FetchLimit) } } + +// An artist removed while its list was being read is not there any more: that is +// "not found", not a provider failure. +func TestAnArtistRemovedWhileItsListWasReadIsNotFound(t *testing.T) { + r := newRig(true) + r.store.saveErr = ErrNotFound + if _, err := r.svc.Similar(context.Background(), 1, 10); !errors.Is(err, ErrNotFound) { + t.Fatalf("err = %v, want ErrNotFound", err) + } +} diff --git a/internal/discovery/tags.go b/internal/discovery/tags.go index 2949053..b74afcf 100644 --- a/internal/discovery/tags.go +++ b/internal/discovery/tags.go @@ -199,6 +199,9 @@ func (s *TagService) one(ctx context.Context, a Artist, p tagProvider) (TagSourc case <-ctx.Done(): return TagSourceResult{}, ctx.Err() case r := <-done: + if errors.Is(r.Err, ErrNotFound) { + return TagSourceResult{}, ErrNotFound // removed while it was being read + } if r.Err != nil { s.log.Warn("tags could not be read", zap.String("provider", p.name), zap.Error(r.Err)) return res(StatusError, snap.Fetched, snap), nil diff --git a/internal/discovery/tags_test.go b/internal/discovery/tags_test.go index 24543ee..e7bc667 100644 --- a/internal/discovery/tags_test.go +++ b/internal/discovery/tags_test.go @@ -28,6 +28,7 @@ type tagStore struct { saves []savedTags genres map[int64][]Tag // what the artist's own files say genErr error + saveErr error // returned by SaveTags when set } func newTagStore() *tagStore { @@ -67,6 +68,9 @@ func (f *tagStore) Tags(_ context.Context, id int64, provider string) (TagSnapsh func (f *tagStore) SaveTags(_ context.Context, id int64, provider string, at time.Time, found bool, tags []Tag) error { f.mu.Lock() defer f.mu.Unlock() + if f.saveErr != nil { + return f.saveErr + } f.saves = append(f.saves, savedTags{id, provider, at, found, tags}) f.snaps[tkey(id, provider)] = TagSnapshot{Fetched: true, NotFound: !found, RetrievedAt: at, Tags: tags} return nil @@ -329,3 +333,11 @@ func TestAFailureReadingFileGenresIsARealError(t *testing.T) { t.Fatal("the database failed and nobody was told") } } + +func TestAnArtistRemovedWhileItsTagsWereReadIsNotFound(t *testing.T) { + r := newTagRig(true, false) + r.store.saveErr = ErrNotFound + if _, err := r.svc.Tags(context.Background(), 1); !errors.Is(err, ErrNotFound) { + t.Fatalf("err = %v, want ErrNotFound", err) + } +} diff --git a/internal/library/plan_test.go b/internal/library/plan_test.go index f8db7e7..77ac561 100644 --- a/internal/library/plan_test.go +++ b/internal/library/plan_test.go @@ -59,8 +59,10 @@ func TestFullPlanReadsEveryPresentFileAgain(t *testing.T) { if len(full.parse) != 4 || !ids[1] || !ids[2] || !ids[3] || !ids[0] { t.Errorf("full plan parses %+v, want the four present files (three known, one new)", full.parse) } - if full.unchanged != 0 || len(full.restore) != 0 { - t.Errorf("full plan: unchanged=%d restore=%v, want none: a re-read clears the missing flag itself", full.unchanged, full.restore) + // A file that came back is restored even if reading it fails this time, as in a + // normal scan, and it is read as well. + if full.unchanged != 0 || len(full.restore) != 1 || full.restore[0] != 3 { + t.Errorf("full plan: unchanged=%d restore=%v, want only back.mp3 restored", full.unchanged, full.restore) } // What is gone is still gone. if len(full.missing) != 1 || full.missing[0] != 4 { diff --git a/internal/library/scanner.go b/internal/library/scanner.go index b480428..e61bdd4 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -384,9 +384,14 @@ func makePlan(w walkResult, existing []ExistingFile, o scanOptions) plan { continue } seen[f.RelPath] = true + changed := e.Size != f.Size || e.MTimeUnix != f.MTimeUnix switch { - case e.Size != f.Size || e.MTimeUnix != f.MTimeUnix || o.full: + case changed || o.full: p.parse = append(p.parse, parseJob{file: f, existingID: e.ID}) + if e.Missing && !changed { + // Back, and read again. It is back even if reading fails this time. + p.restore = append(p.restore, e.ID) + } case e.Missing: p.restore = append(p.restore, e.ID) default: diff --git a/internal/library/scanner_test.go b/internal/library/scanner_test.go index 24ab2ce..ed7facb 100644 --- a/internal/library/scanner_test.go +++ b/internal/library/scanner_test.go @@ -931,3 +931,36 @@ func TestManagerCanStartAFullScan(t *testing.T) { t.Fatal("the scan did not finish") } } + +// A file that comes back and cannot be read during a full scan is still back. +func TestFullScanStillRestoresAFileItCannotRead(t *testing.T) { + root := t.TempDir() + p := filepath.Join(root, "a.mp3") + write(t, p, "1") + store, reader := newFakeStore(), &fakeReader{} + s, _ := newScanner(store, reader, 1) + mustScan(t, s, root) + + if err := os.Remove(p); err != nil { + t.Fatal(err) + } + write(t, filepath.Join(root, "other.mp3"), "22") // so the root is not empty + mustScan(t, s, root) + if !store.row(root, "a.mp3").Missing { + t.Fatal("the file should be missing") + } + + write(t, p, "1") // same size; the mtime is set below to what was indexed + old := store.row(root, "a.mp3").MTimeUnix + if err := os.Chtimes(p, time.Unix(old, 0), time.Unix(old, 0)); err != nil { + t.Fatal(err) + } + reader.fail = map[string]bool{"a.mp3": true} + sum, err := s.Scan(context.Background(), libsOf(root), FullRescan) + if err != nil { + t.Fatal(err) + } + if sum.Restored != 1 || sum.Failed != 1 || store.row(root, "a.mp3").Missing { + t.Errorf("summary = %+v, missing = %v: the file is there, even if its tags could not be read", sum, store.row(root, "a.mp3").Missing) + } +} diff --git a/internal/library/taglib.go b/internal/library/taglib.go index 3b40695..c5c9e74 100644 --- a/internal/library/taglib.go +++ b/internal/library/taglib.go @@ -43,7 +43,7 @@ func (TaglibReader) Read(path string) (Metadata, error) { } m := Metadata{ Tags: Tags{ - Genres: cleanGenres(tags["GENRE"]), + Genres: CleanGenres(tags["GENRE"]), Title: first(tags, "TITLE"), Album: first(tags, "ALBUM"), AlbumArtist: albumArtist, @@ -78,10 +78,10 @@ func (TaglibReader) Read(path string) (Metadata, error) { // maxGenreLen bounds one genre; a longer one is a sentence, not a genre. const maxGenreLen = 100 -// cleanGenres turns the values of the GENRE tag into a short list of lower case +// CleanGenres turns the values of the GENRE tag into a short list of lower case // genres without repeats. A tagger may write several genres as several values or // as one value joined with ";". A slash is not a separator: "R&B/Soul" is one genre. -func cleanGenres(values []string) []string { +func CleanGenres(values []string) []string { var out []string seen := map[string]bool{} for _, v := range values { diff --git a/internal/library/taglib_test.go b/internal/library/taglib_test.go index c9db592..dc37302 100644 --- a/internal/library/taglib_test.go +++ b/internal/library/taglib_test.go @@ -265,11 +265,11 @@ func TestGenresAreReadCleanedAndBounded(t *testing.T) { func TestCleanGenres(t *testing.T) { long := strings.Repeat("x", 101) - got := cleanGenres([]string{"a;b;c;d;e;f;g", long, "", " "}) + got := CleanGenres([]string{"a;b;c;d;e;f;g", long, "", " "}) if len(got) != MaxGenresPerTrack || got[0] != "a" || got[MaxGenresPerTrack-1] != "e" { t.Errorf("genres = %v, want the first %d", got, MaxGenresPerTrack) } - if got := cleanGenres([]string{long}); len(got) != 0 { + if got := CleanGenres([]string{long}); len(got) != 0 { t.Errorf("a 101-character genre is noise, kept as %v", got) } } diff --git a/internal/storage/discovery.go b/internal/storage/discovery.go index 1aa3838..a14ea01 100644 --- a/internal/storage/discovery.go +++ b/internal/storage/discovery.go @@ -123,6 +123,9 @@ func (r *DiscoveryRepo) SaveSimilar(ctx context.Context, id int64, provider stri if err := lockArtistWrites(ctx, tx); err != nil { return err } + if err := requireArtist(ctx, tx, id); err != nil { + return err + } ids, err := resolveArtists(ctx, tx, id, cands) if err != nil { return err @@ -166,22 +169,39 @@ func (r *DiscoveryRepo) SaveSimilar(ctx context.Context, id int64, provider stri }) } -// removeUnnamedArtists deletes the discovered artists, among those matching the -// extra condition, that no list names and that name no list. Real artists (any -// that a file ever matched) are never touched. Their tags go with them. +// removeUnnamedArtists deletes the discovered artists that no list names, among +// those matching the extra condition first, and then, over and over until nothing +// more goes, among all of them: removing an artist removes the list it was the +// source of, which can leave the artists only that list named without anyone +// naming them. Real artists (any that a file ever matched) are never touched. +// Tags and records of a removed artist go with it. The caller holds the artist +// lock, so nothing is adopted or named while this runs. func removeUnnamedArtists(ctx context.Context, tx bun.Tx, extra string, args ...any) (int, error) { - res, err := tx.ExecContext(ctx, - "DELETE FROM artists WHERE "+extra+`discovered = ? - AND NOT EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = artists.id) - AND NOT EXISTS (SELECT 1 FROM artist_similar s WHERE s.artist_id = artists.id)`, - append(args, true)...) - if err != nil { - return 0, fmt.Errorf("remove artists nobody names: %w", err) + total := 0 + for pass := 0; pass < maxCleanupPasses; pass++ { + cond, condArgs := "", []any(nil) + if pass == 0 { + cond, condArgs = extra, args + } + res, err := tx.ExecContext(ctx, + "DELETE FROM artists WHERE "+cond+`discovered = ? + AND NOT EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = artists.id)`, + append(condArgs, true)...) + if err != nil { + return total, fmt.Errorf("remove artists nobody names: %w", err) + } + n, _ := res.RowsAffected() + total += int(n) + if n == 0 { + break // nothing went, so nothing is left unnamed by it + } } - n, _ := res.RowsAffected() - return int(n), nil + return total, nil } +// maxCleanupPasses bounds how deep a chain of discovered artists is followed. +const maxCleanupPasses = 20 + // resolveArtists returns the artist id of each candidate, creating the ones that // are not known. A candidate that is the artist itself, or an artist already // seen earlier in the list, gets 0. @@ -281,6 +301,20 @@ func insertDiscovered(ctx context.Context, tx bun.Tx, name, mbid string) (int64, return id, nil } +// requireArtist returns discovery.ErrNotFound when the artist is gone, as it can be +// when it was removed while a provider was being asked about it. +func requireArtist(ctx context.Context, tx bun.Tx, id int64) error { + var one int + err := tx.QueryRowContext(ctx, "SELECT 1 FROM artists WHERE id = ?", id).Scan(&one) + if errors.Is(err, sql.ErrNoRows) { + return discovery.ErrNotFound + } + if err != nil { + return fmt.Errorf("check artist: %w", err) + } + return nil +} + // artistWritesLock is the key of the PostgreSQL advisory lock that serializes the // creation of artists. SQLite needs none: its transactions already run one at a time. const artistWritesLock = 0x534f4e4441 // "SONDA" @@ -338,6 +372,13 @@ func (r *DiscoveryRepo) Tags(ctx context.Context, id int64, provider string) (di // SaveTags replaces the tags a provider gave to an artist and records the answer. func (r *DiscoveryRepo) SaveTags(ctx context.Context, id int64, provider string, at time.Time, found bool, tags []discovery.Tag) error { return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Artists can be removed by a purge or a cleanup: take the same turn they do. + if err := lockArtistWrites(ctx, tx); err != nil { + return err + } + if err := requireArtist(ctx, tx, id); err != nil { + return err + } if _, err := tx.ExecContext(ctx, "DELETE FROM artist_tags WHERE artist_id = ? AND provider = ?", id, provider); err != nil { return fmt.Errorf("clear tags: %w", err) } @@ -411,6 +452,11 @@ func (r *DiscoveryRepo) FileGenres(ctx context.Context, artistID int64) ([]disco func (r *DiscoveryRepo) Purge(ctx context.Context, provider string) (providers.Purged, error) { var out providers.Purged err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Artists are removed here: take the turn the scanner and discovery take, + // or a file could be matched to an artist this transaction then deletes. + if err := lockArtistWrites(ctx, tx); err != nil { + return err + } for _, step := range []struct { what string query string diff --git a/internal/storage/discovery_test.go b/internal/storage/discovery_test.go index fde9592..03c61b3 100644 --- a/internal/storage/discovery_test.go +++ b/internal/storage/discovery_test.go @@ -935,3 +935,96 @@ func TestReplacingAListRemovesTheArtistsNoOneNamesAnymore(t *testing.T) { } }) } + +// ---- review: purge, cleanup chains and deleted artists ---- + +// A purge deleting a discovered artist while a scan adopts it must not leave the +// scan with the id of an artist that is gone. +func TestPurgeAndAdoptionDoNotCollide(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + s := seedCatalog(t, db) + ctx := context.Background() + repo, lib := NewDiscoveryRepo(db), NewLibraryRepo(db) + radiohead := artistID(t, db, "Radiohead") + + errs := make(chan error, 100) + for round := 0; round < 25; round++ { + name := fmt.Sprintf("Collide %d", round) + if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{ + {Name: name, MBID: fmt.Sprintf("dddddddd-0000-4000-8000-%012d", round), Match: 1}}); err != nil { + t.Fatal(err) + } + var wg sync.WaitGroup + wg.Add(2) + go func() { + defer wg.Done() + if _, err := repo.Purge(ctx, "lastfm"); err != nil { + errs <- fmt.Errorf("Purge: %w", err) + } + }() + go func() { + defer wg.Done() + f := pf(fmt.Sprintf("col/%d.flac", round), fmt.Sprintf("Collide song %d", round), "", "", 0, 0, "flac", name) + if _, err := lib.SaveFile(ctx, s.rock, f, 0); err != nil { + errs <- fmt.Errorf("SaveFile: %w", err) + } + }() + wg.Wait() + } + close(errs) + for err := range errs { + t.Error(err) + } + }) +} + +// An artist that was only a source of a list, and that no list names any more, +// goes too, and so do the artists only its own list kept alive. +func TestAChainOfDiscoveredArtistsIsCleanedUpTogether(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + seedCatalog(t, db) + ctx := context.Background() + repo := NewDiscoveryRepo(db) + radiohead := artistID(t, db, "Radiohead") + + _ = repo.SaveSimilar(ctx, radiohead, "lastfm", at, true, []lastfm.Similar{{Name: "Dee", Match: 1}, {Name: "Keep", Match: 0.5}}) + dee := artistID(t, db, "Dee") + _ = repo.SaveSimilar(ctx, dee, "lastfm", at, true, []lastfm.Similar{{Name: "Eee", Match: 1}}) + eee := artistID(t, db, "Eee") + _ = repo.SaveSimilar(ctx, eee, "lastfm", at, true, []lastfm.Similar{{Name: "Fff", Match: 1}}) + + if err := repo.SaveSimilar(ctx, radiohead, "lastfm", at.Add(time.Hour), true, []lastfm.Similar{{Name: "Keep", Match: 1}}); err != nil { + t.Fatal(err) + } + for _, name := range []string{"Dee", "Eee", "Fff"} { + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = '"+name+"'"); n != 0 { + t.Errorf("%s is kept alive only by a list nobody can reach, and is still there", name) + } + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'Keep'"); n != 1 { + t.Error("an artist the list still names was removed") + } + if n := count(t, db, "SELECT COUNT(*) FROM artist_similar"); n != 1 { + t.Errorf("edges left = %d, want 1", n) + } + }) +} + +// Writing about an artist that was removed meanwhile is "not found", not an +// obscure foreign key error. +func TestSavingForAnArtistThatIsGoneIsNotFound(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + seedCatalog(t, db) + ctx := context.Background() + repo := NewDiscoveryRepo(db) + if err := repo.SaveSimilar(ctx, 99999, "lastfm", at, true, []lastfm.Similar{{Name: "X", Match: 1}}); !errors.Is(err, discovery.ErrNotFound) { + t.Errorf("SaveSimilar: %v", err) + } + if err := repo.SaveTags(ctx, 99999, "lastfm", at, true, []discovery.Tag{{Name: "x", Weight: 1}}); !errors.Is(err, discovery.ErrNotFound) { + t.Errorf("SaveTags: %v", err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE name = 'X'"); n != 0 { + t.Error("an artist was created for a list nobody can read") + } + }) +} diff --git a/internal/storage/library.go b/internal/storage/library.go index 406472f..1af6dfb 100644 --- a/internal/storage/library.go +++ b/internal/storage/library.go @@ -505,18 +505,13 @@ func findOrCreateArtist(ctx context.Context, tx bun.Tx, name, mbid string) (int6 return found.ID, nil } -// replaceGenres makes the genres of a track those the file says now. +// replaceGenres makes the genres of a track those the file says now. The reader +// already cleaned them; cleaning is idempotent, so a store used another way is safe too. func replaceGenres(ctx context.Context, tx bun.Tx, trackID int64, genres []string) error { if _, err := tx.ExecContext(ctx, "DELETE FROM track_genres WHERE track_id = ?", trackID); err != nil { return fmt.Errorf("clear genres: %w", err) } - seen := map[string]bool{} - for _, g := range genres { - g = strings.ToLower(strings.TrimSpace(g)) - if g == "" || seen[g] || len(seen) == library.MaxGenresPerTrack { - continue - } - seen[g] = true + for _, g := range library.CleanGenres(genres) { if _, err := tx.ExecContext(ctx, "INSERT INTO track_genres (track_id, genre) VALUES (?, ?)", trackID, g); err != nil { return fmt.Errorf("insert genre: %w", err) }