947f944fe9
The hand-rolled extractor in internal/library/mbids.go looked up keys that
dhowden/tag never produces, for every supported audio format:
Vorbis (FLAC/OGG): dhowden lowercases keys at parse time (vorbis.go:59),
we looked up MUSICBRAINZ_ALBUMID. Always missed.
ID3v2 (MP3): dhowden stores TXXX frames under bare TXXX/TXXX_N keys
with *tag.Comm values; the Picard tag name is on the value's
Description field, not part of the key. We looked up
TXXX:MusicBrainz Album Id. Always missed.
MP4 (M4A): dhowden stores freeform iTunes atoms under their bare
sub-name. We looked up ----:com.apple.iTunes:MusicBrainz Album Id.
Always missed.
Net effect: every album in the m7-380 boot backfill reported
processed=N, healed=0, skipped=N. The m7-379 enricher gate then
short-circuited NULL on every album, so MBCAA was never queried.
Symptom: "all albums skipped" during cover enrichment.
The pre-existing unit tests passed because they hand-built Raw() maps
with the intended-but-incorrect keys, never round-tripping through
dhowden's parser.
Replace the hand-rolled extractor with dhowden's own mbz sub-package,
which knows the per-format Raw() conventions and uses lowercase
canonical keys (mbz.Album, mbz.Artist). Update both call sites to pass
the full tag.Metadata instead of just Raw(). Rewrite the test with a
stubMeta implementing tag.Metadata, asserting against the actual
per-format Raw() shape, plus a regression guard that pins "uppercase
Vorbis keys must not match" so the v0 bug can't sneak back.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
124 lines
3.6 KiB
Go
124 lines
3.6 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/dhowden/tag"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// BackfillMBIDsResult tallies what the one-shot worker did.
|
|
type BackfillMBIDsResult struct {
|
|
Processed int // Albums considered (mbid IS NULL).
|
|
Healed int // Albums whose mbid we set.
|
|
Skipped int // Albums where the file was missing or had no MBID tag.
|
|
}
|
|
|
|
// BackfillMBIDs walks albums with NULL mbid, opens one track per album,
|
|
// extracts MusicBrainz IDs from the tags, and persists them. Also heals
|
|
// the artist's mbid when the same tag-read yields one. When a row is
|
|
// healed, any 'none' cover_art_source on the album is cleared back to
|
|
// NULL so the enricher batch retries.
|
|
//
|
|
// Intended use: one-shot at server boot. Re-running it after the first
|
|
// pass costs the same N file reads but is a no-op for healed rows.
|
|
//
|
|
// limit caps how many albums are processed in one call. Pass -1 for no
|
|
// cap. The caller normally batches (e.g. limit=500 per boot) to avoid
|
|
// stalling startup on huge libraries.
|
|
func BackfillMBIDs(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, limit int) (BackfillMBIDsResult, error) {
|
|
q := dbq.New(pool)
|
|
queryLimit := int32(limit)
|
|
if limit < 0 {
|
|
queryLimit = 1<<31 - 1 // effectively unbounded
|
|
}
|
|
|
|
rows, err := q.ListAlbumsMissingMbidWithTrack(ctx, queryLimit)
|
|
if err != nil {
|
|
return BackfillMBIDsResult{}, fmt.Errorf("list missing mbid: %w", err)
|
|
}
|
|
|
|
var res BackfillMBIDsResult
|
|
for i, r := range rows {
|
|
if ctx.Err() != nil {
|
|
return res, ctx.Err()
|
|
}
|
|
res.Processed++
|
|
|
|
albumMBID, artistMBID := readMBIDsForFile(r.TrackFilePath, logger)
|
|
if albumMBID == "" {
|
|
res.Skipped++
|
|
continue
|
|
}
|
|
|
|
// Heal album.
|
|
m := albumMBID
|
|
if err := q.SetAlbumMbidIfNull(ctx, dbq.SetAlbumMbidIfNullParams{
|
|
ID: r.AlbumID, Mbid: &m,
|
|
}); err != nil {
|
|
logger.Warn("mbid backfill: set album mbid failed",
|
|
"album_id", r.AlbumID, "err", err)
|
|
res.Skipped++
|
|
continue
|
|
}
|
|
|
|
// Heal artist (if we got one).
|
|
if artistMBID != "" {
|
|
am := artistMBID
|
|
if err := q.SetArtistMbidIfNull(ctx, dbq.SetArtistMbidIfNullParams{
|
|
ID: r.ArtistID, Mbid: &am,
|
|
}); err != nil {
|
|
logger.Warn("mbid backfill: set artist mbid failed",
|
|
"artist_id", r.ArtistID, "err", err)
|
|
}
|
|
}
|
|
|
|
// Clear cover_art_source = 'none' so the cover enricher's next
|
|
// batch retries the MBCAA fetch for this album.
|
|
if err := q.ClearAlbumCoverNone(ctx, r.AlbumID); err != nil {
|
|
logger.Warn("mbid backfill: clear cover none failed",
|
|
"album_id", r.AlbumID, "err", err)
|
|
}
|
|
|
|
res.Healed++
|
|
|
|
// Progress log every 100 healed.
|
|
if (i+1)%100 == 0 {
|
|
logger.Info("mbid backfill progress",
|
|
"processed", res.Processed, "healed", res.Healed, "skipped", res.Skipped)
|
|
}
|
|
}
|
|
|
|
logger.Info("mbid backfill complete",
|
|
"processed", res.Processed,
|
|
"healed", res.Healed,
|
|
"skipped", res.Skipped)
|
|
return res, nil
|
|
}
|
|
|
|
// readMBIDsForFile opens a single audio file, reads tags via dhowden/tag,
|
|
// and returns extracted album + artist MBIDs. Returns ("", "") when the
|
|
// file can't be opened or the tag read fails — those errors are logged
|
|
// at Warn level but not propagated, so a single broken file doesn't
|
|
// halt the whole backfill.
|
|
func readMBIDsForFile(path string, logger *slog.Logger) (albumMBID, artistMBID string) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
logger.Warn("mbid backfill: open failed", "path", path, "err", err)
|
|
return "", ""
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
|
|
meta, err := tag.ReadFrom(f)
|
|
if err != nil {
|
|
logger.Warn("mbid backfill: tag read failed", "path", path, "err", err)
|
|
return "", ""
|
|
}
|
|
return extractMBIDs(meta)
|
|
}
|