fix(sync): an artist picture found after the artist first synced reaches the Android app (#8080)
The library sync sends each artist's picture paths, and an app learns that a row changed only from the change log. The artist enricher wrote the paths without logging, so an artist synced before its picture arrived kept showing the album-cover fallback on Android until something else touched it. A picture found is now logged in the same transaction as the write; settling "none" changes nothing an app shows and isn't logged. The MBID backfill's heals of albums.mbid and artists.mbid, also part of the sync payload, are now logged too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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{
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user