fix(playlists): each daily mix keeps its id across rebuilds (#8081)

The nightly rebuild deleted every system playlist and inserted new rows, so
each mix got a new id every day. A Subsonic app caches, pins, downloads and
syncs a playlist by id, and saw its mixes deleted and recreated daily: a
stored mix answered "not found" and an offline download was orphaned.

The rebuild now refills yesterday's row for the same mix in place (keyed by
kind, plus the seed artist for "Songs like" and the name for a genre mix),
inserts only mixes that are new, and deletes the ones today's build didn't
make, along with their collage files. The rollups move updated_at, which is
the "changed" time Subsonic apps use to see a mix was refreshed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 23:57:41 -05:00
co-authored by Claude Opus 5.5
parent dbf39f90f8
commit d1f76167c8
6 changed files with 236 additions and 65 deletions
+32 -23
View File
@@ -228,39 +228,48 @@ func TestBuildSystemPlaylists_QuarantineExcluded(t *testing.T) {
}
}
func TestBuildSystemPlaylists_AtomicReplace(t *testing.T) {
// A rebuild refills each mix in place: a Subsonic app caches, pins and
// downloads a playlist by id, so the same mix keeps its id from one build to
// the next, and no mix is left behind twice.
func TestBuildSystemPlaylists_ARebuildKeepsEachMixsID(t *testing.T) {
pool := dbtest.Pool(t)
logger := discardLogger()
u, _ := seedActiveLibrary(t, pool, "atomic", 3, 3)
ctx := context.Background()
dir := t.TempDir()
// First build.
if err := playlists.BuildSystemPlaylists(ctx, pool, logger, u.ID, time.Now().UTC(), t.TempDir()); err != nil {
list := func() map[string]pgtype.UUID {
t.Helper()
rows, err := dbq.New(pool).ListPlaylistsByUserAndKind(ctx, dbq.ListPlaylistsByUserAndKindParams{
UserID: u.ID, Column2: "system",
})
if err != nil {
t.Fatalf("list: %v", err)
}
out := map[string]pgtype.UUID{}
for _, r := range rows {
if _, dup := out[r.Name]; dup {
t.Fatalf("mix %q is held twice after a rebuild", r.Name)
}
out[r.Name] = r.ID
}
return out
}
if err := playlists.BuildSystemPlaylists(ctx, pool, logger, u.ID, time.Now().UTC(), dir); err != nil {
t.Fatalf("first build: %v", err)
}
var ids1 []pgtype.UUID
rows1, _ := dbq.New(pool).ListPlaylistsByUserAndKind(ctx, dbq.ListPlaylistsByUserAndKindParams{
UserID: u.ID, Column2: "system",
})
for _, r := range rows1 {
ids1 = append(ids1, r.ID)
first := list()
if len(first) == 0 {
t.Fatal("no system playlists from the first build")
}
if len(ids1) == 0 {
t.Skip("no system playlists from first build; can't verify replace")
}
// Second build — old rows must be gone, replaced with fresh ones.
if err := playlists.BuildSystemPlaylists(ctx, pool, logger, u.ID, time.Now().UTC(), t.TempDir()); err != nil {
if err := playlists.BuildSystemPlaylists(ctx, pool, logger, u.ID, time.Now().UTC(), dir); err != nil {
t.Fatalf("second build: %v", err)
}
rows2, _ := dbq.New(pool).ListPlaylistsByUserAndKind(ctx, dbq.ListPlaylistsByUserAndKindParams{
UserID: u.ID, Column2: "system",
})
for _, r := range rows2 {
for _, oldID := range ids1 {
if r.ID.Bytes == oldID.Bytes {
t.Errorf("playlist id %s persisted across rebuilds — atomic replace failed", uuidString(oldID))
}
second := list()
for name, id := range first {
if again, ok := second[name]; ok && again != id {
t.Errorf("mix %q got a new id on rebuild: %s then %s", name, uuidString(id), uuidString(again))
}
}
}