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:
@@ -11,6 +11,15 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const clearPlaylistTracks = `-- name: ClearPlaylistTracks :exec
|
||||
DELETE FROM playlist_tracks WHERE playlist_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) ClearPlaylistTracks(ctx context.Context, playlistID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, clearPlaylistTracks, playlistID)
|
||||
return err
|
||||
}
|
||||
|
||||
const clearStaleSystemPlaylistInFlight = `-- name: ClearStaleSystemPlaylistInFlight :exec
|
||||
UPDATE system_playlist_runs SET in_flight = false WHERE in_flight = true
|
||||
`
|
||||
@@ -75,14 +84,14 @@ func (q *Queries) CreateSystemPlaylist(ctx context.Context, arg CreateSystemPlay
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteSystemPlaylistsForUser = `-- name: DeleteSystemPlaylistsForUser :exec
|
||||
DELETE FROM playlists WHERE user_id = $1 AND kind = 'system'
|
||||
const deletePlaylistsByIDs = `-- name: DeletePlaylistsByIDs :exec
|
||||
DELETE FROM playlists WHERE id = ANY($1::uuid[])
|
||||
`
|
||||
|
||||
// Atomic-replace step. CASCADE on playlist_tracks.playlist_id handles
|
||||
// the join rows.
|
||||
func (q *Queries) DeleteSystemPlaylistsForUser(ctx context.Context, userID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteSystemPlaylistsForUser, userID)
|
||||
// Mixes a rebuild no longer makes. CASCADE on playlist_tracks.playlist_id
|
||||
// handles the join rows.
|
||||
func (q *Queries) DeletePlaylistsByIDs(ctx context.Context, ids []pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deletePlaylistsByIDs, ids)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -303,6 +312,46 @@ func (q *Queries) ListPlaylistsByUserAndKind(ctx context.Context, arg ListPlayli
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSystemPlaylistsForRebuild = `-- name: ListSystemPlaylistsForRebuild :many
|
||||
SELECT id, name, system_variant, seed_artist_id
|
||||
FROM playlists
|
||||
WHERE user_id = $1 AND kind = 'system'
|
||||
`
|
||||
|
||||
type ListSystemPlaylistsForRebuildRow struct {
|
||||
ID pgtype.UUID
|
||||
Name string
|
||||
SystemVariant *string
|
||||
SeedArtistID pgtype.UUID
|
||||
}
|
||||
|
||||
// The user's current mixes, so a rebuild can refill each in place and keep
|
||||
// its id.
|
||||
func (q *Queries) ListSystemPlaylistsForRebuild(ctx context.Context, userID pgtype.UUID) ([]ListSystemPlaylistsForRebuildRow, error) {
|
||||
rows, err := q.db.Query(ctx, listSystemPlaylistsForRebuild, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListSystemPlaylistsForRebuildRow
|
||||
for rows.Next() {
|
||||
var i ListSystemPlaylistsForRebuildRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.SystemVariant,
|
||||
&i.SeedArtistID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const pickSeedArtists = `-- name: PickSeedArtists :many
|
||||
WITH liked AS (
|
||||
SELECT gla.artist_id FROM general_likes_artists gla WHERE gla.user_id = $1
|
||||
@@ -528,6 +577,31 @@ func (q *Queries) PickTopPlayedTracksForUser(ctx context.Context, userID pgtype.
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const refreshSystemPlaylist = `-- name: RefreshSystemPlaylist :exec
|
||||
UPDATE playlists
|
||||
SET name = $2, description = $3, shelf_rank = $4
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type RefreshSystemPlaylistParams struct {
|
||||
ID pgtype.UUID
|
||||
Name string
|
||||
Description string
|
||||
ShelfRank int32
|
||||
}
|
||||
|
||||
// A rebuild refilling an existing mix: its name, description and shelf
|
||||
// place may move with the day's build. updated_at moves with the rollups.
|
||||
func (q *Queries) RefreshSystemPlaylist(ctx context.Context, arg RefreshSystemPlaylistParams) error {
|
||||
_, err := q.db.Exec(ctx, refreshSystemPlaylist,
|
||||
arg.ID,
|
||||
arg.Name,
|
||||
arg.Description,
|
||||
arg.ShelfRank,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const tryClaimSystemPlaylistRun = `-- name: TryClaimSystemPlaylistRun :one
|
||||
INSERT INTO system_playlist_runs (user_id, in_flight)
|
||||
VALUES ($1, true)
|
||||
|
||||
Reference in New Issue
Block a user