diff --git a/internal/coverart/artist_enricher.go b/internal/coverart/artist_enricher.go index 2af0a052..6835dcc0 100644 --- a/internal/coverart/artist_enricher.go +++ b/internal/coverart/artist_enricher.go @@ -11,6 +11,7 @@ import ( "github.com/jackc/pgx/v5/pgtype" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" + syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync" ) // EnrichArtist runs the chain for a single artist. Parallel to @@ -115,9 +116,12 @@ func (e *Enricher) EnrichArtist(ctx context.Context, artistID pgtype.UUID, dataD // recordArtistArt writes the new artist_thumb_path / // artist_fanart_path / artist_art_source / artist_art_sources_version -// atomically. +// atomically. A picture found is logged as a change to the artist in the +// same transaction: the library sync sends the picture paths, and an app +// learns a row changed only from the change log, so without it an artist +// first synced before its picture arrived would stay pictureless there. +// Settling "none" changes nothing an app shows and isn't logged. func (e *Enricher) recordArtistArt(ctx context.Context, artistID pgtype.UUID, thumbPath, fanartPath, source string, version int32) error { - q := dbq.New(e.pool) src := source var thumbArg, fanartArg *string if thumbPath != "" { @@ -126,12 +130,20 @@ func (e *Enricher) recordArtistArt(ctx context.Context, artistID pgtype.UUID, th if fanartPath != "" { fanartArg = &fanartPath } - return q.SetArtistArtWithVersion(ctx, dbq.SetArtistArtWithVersionParams{ - ID: artistID, - ArtistThumbPath: thumbArg, - ArtistFanartPath: fanartArg, - ArtistArtSource: &src, - ArtistArtSourcesVersion: version, + return pgx.BeginFunc(ctx, e.pool, func(tx pgx.Tx) error { + if err := dbq.New(tx).SetArtistArtWithVersion(ctx, dbq.SetArtistArtWithVersionParams{ + ID: artistID, + ArtistThumbPath: thumbArg, + ArtistFanartPath: fanartArg, + ArtistArtSource: &src, + ArtistArtSourcesVersion: version, + }); err != nil { + return err + } + if thumbArg == nil && fanartArg == nil { + return nil + } + return syncpkg.LogChange(ctx, tx, syncpkg.EntityArtist, syncpkg.FormatUUID(artistID), syncpkg.OpUpsert) }) } diff --git a/internal/coverart/artist_enricher_test.go b/internal/coverart/artist_enricher_test.go index 13f5f6d7..4b3792af 100644 --- a/internal/coverart/artist_enricher_test.go +++ b/internal/coverart/artist_enricher_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" @@ -532,3 +533,52 @@ func TestEnrichArtist_NoMBID_SettlesNone(t *testing.T) { t.Errorf("source = %v, want 'none' (settled)", row.ArtistArtSource) } } + +// artistChanges counts the change-log rows for an artist: what tells a +// syncing app to fetch the artist again. +func artistChanges(t *testing.T, pool *pgxpool.Pool, artistID pgtype.UUID) int { + t.Helper() + var n int + if err := pool.QueryRow(context.Background(), + `SELECT count(*) FROM library_changes WHERE entity_type = 'artist' AND entity_id = $1`, + artistID.String()).Scan(&n); err != nil { + t.Fatalf("count changes: %v", err) + } + return n +} + +// A picture found for an artist is logged as a change to it, so an app that +// synced the artist before the picture arrived fetches it again. Settling +// "none" changes nothing an app shows and logs nothing. +func TestEnrichArtist_APictureFoundIsInTheSyncLog(t *testing.T) { + pool := dbtest.Pool(t) + ctx := context.Background() + q := dbq.New(pool) + resetRegistryForTests() + t.Cleanup(resetRegistryForTests) + stub := &stubArtistProvider{ + fakeProvider: fakeProvider{id: "sync-log-provider", defaultOn: true}, + thumb: []byte("thumb"), + } + Register(stub) + e := newTestEnricher(t, pool) + + found := insertArtist(t, q, ctx, "Pictured Artist", "88888888-8888-8888-8888-888888888888") + before := artistChanges(t, pool, found) + if err := e.EnrichArtist(ctx, found, t.TempDir()); err != nil { + t.Fatalf("EnrichArtist: %v", err) + } + if got := artistChanges(t, pool, found); got != before+1 { + t.Fatalf("change rows for the pictured artist = %d, want %d", got, before+1) + } + + stub.thumb, stub.err = nil, ErrNotFound + bare := insertArtist(t, q, ctx, "Unpictured Artist", "99999999-9999-9999-9999-999999999999") + before = artistChanges(t, pool, bare) + if err := e.EnrichArtist(ctx, bare, t.TempDir()); err != nil { + t.Fatalf("EnrichArtist: %v", err) + } + if got := artistChanges(t, pool, bare); got != before { + t.Fatalf("change rows for an artist settled none = %d, want %d", got, before) + } +} diff --git a/internal/library/mbidbackfill.go b/internal/library/mbidbackfill.go index 0ba2744f..98bd521c 100644 --- a/internal/library/mbidbackfill.go +++ b/internal/library/mbidbackfill.go @@ -10,6 +10,7 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/db" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" + syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync" "git.fabledsword.com/bvandeusen/minstrel/internal/tagread" ) @@ -90,6 +91,12 @@ func BackfillMBIDs(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, continue } + // The album's mbid is part of what the library sync sends, so an app + // hears about the heal through the change log. + if err := syncpkg.LogChange(ctx, pool, syncpkg.EntityAlbum, syncpkg.FormatUUID(r.AlbumID), syncpkg.OpUpsert); err != nil { + logger.Warn("mbid backfill: log album change failed", "album_id", r.AlbumID, "err", err) + } + // Heal artist (if we got one). if artistMBID != "" { am := artistMBID @@ -103,6 +110,8 @@ func BackfillMBIDs(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, logger.Warn("mbid backfill: set artist mbid failed", "artist_id", r.ArtistID, "err", err) } + } else if err := syncpkg.LogChange(ctx, pool, syncpkg.EntityArtist, syncpkg.FormatUUID(r.ArtistID), syncpkg.OpUpsert); err != nil { + logger.Warn("mbid backfill: log artist change failed", "artist_id", r.ArtistID, "err", err) } }