Files
minstrel/internal/db/queries/covers.sql
T
bvandeusen 34615fffbd feat(server/m7-380): one-shot MBID backfill worker for existing libraries
Adds a boot-time goroutine that walks albums with NULL mbid, re-reads
tags from one track per album via dhowden/tag, and persists album + artist
MBIDs. Healed albums also get their cover_art_source='none' cleared so the
enricher's next batch retries the MBCAA fetch. Caps at 5000 albums per
boot to avoid stalling startup on huge libraries.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-04 19:40:27 -04:00

65 lines
2.4 KiB
SQL

-- M7 #353: album cover enrichment queries.
-- name: ListAlbumsMissingCover :many
-- Albums where cover_art_source has never been set. Used by the boot
-- backfill and post-scan enrichment passes. LIMIT supplied by caller.
SELECT a.id, a.mbid, a.title, a.artist_id
FROM albums a
WHERE a.cover_art_source IS NULL
ORDER BY a.created_at ASC
LIMIT $1;
-- name: ListAlbumsRetryMissing :many
-- Bulk-retry candidates: NULL (never tried) plus 'none' (tried, failed).
-- Used by the admin "refetch missing covers" endpoint. LIMIT supplied.
SELECT a.id, a.mbid, a.title, a.artist_id
FROM albums a
WHERE a.cover_art_source IS NULL OR a.cover_art_source = 'none'
ORDER BY a.created_at ASC
LIMIT $1;
-- name: GetAlbumWithFirstTrackPath :one
-- Returns the album row and one of its track file paths (used to derive
-- the album directory). Pick lowest position track for determinism.
SELECT a.id, a.mbid, a.title, a.cover_art_path, a.cover_art_source,
t.file_path AS track_file_path
FROM albums a
LEFT JOIN tracks t ON t.album_id = a.id
WHERE a.id = $1
ORDER BY t.disc_number NULLS LAST, t.track_number NULLS LAST, t.id
LIMIT 1;
-- name: SetAlbumCover :exec
-- Persist enrichment result. Pass cover_art_path = '' to clear (turns into NULL).
UPDATE albums
SET cover_art_path = NULLIF($2::text, ''),
cover_art_source = $3
WHERE id = $1;
-- name: ClearAlbumCover :exec
-- Used by the admin retry endpoint to wipe state before re-running the
-- enricher. Sets both fields to NULL.
UPDATE albums
SET cover_art_path = NULL,
cover_art_source = NULL
WHERE id = $1;
-- name: ClearAlbumCoverNone :exec
-- M7 #380: when MBID backfill heals an album, clear its 'none' cover_art_source
-- back to NULL so the enricher's next batch retries the MBCAA fetch.
-- Idempotent — only updates rows currently stamped 'none'.
UPDATE albums
SET cover_art_source = NULL
WHERE id = $1 AND cover_art_source = 'none';
-- name: CountAlbumCoverSources :one
-- Diagnostic for the admin overview: counts by source, including NULL.
-- Returns one row with named tallies.
SELECT
COUNT(*) FILTER (WHERE cover_art_source IS NULL) ::bigint AS untried,
COUNT(*) FILTER (WHERE cover_art_source = 'sidecar') ::bigint AS sidecar,
COUNT(*) FILTER (WHERE cover_art_source = 'embedded')::bigint AS embedded,
COUNT(*) FILTER (WHERE cover_art_source = 'mbcaa') ::bigint AS mbcaa,
COUNT(*) FILTER (WHERE cover_art_source = 'none') ::bigint AS none
FROM albums;