feat(library): albums are filed under their album artist, and empty rows are tidied after a scan
release / go (push) Failing after 1m13s
release / web (push) Successful in 1m16s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m4s
release / android (push) Successful in 4m48s
release / Build signed APK (push) Successful in 5m13s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped

The scanner read only the track artist, so a compilation tagged
Various Artists, or an album with one featured-artist track, became one
album per track artist. It now files the album under the album artist
the file names (ALBUMARTIST / TPE2 / aART, with its MusicBrainz id),
and each track keeps its own artist. Tag-read version 9 re-reads the
library so existing tracks move back onto one album.

A scan now ends by removing album rows no track points to, and artists
left with no albums or tracks, telling clients they are gone. Albums
whose tracks are only missing stay for Needs a file, an artist a Songs
like playlist is seeded from stays, and rows made in the last ten
minutes are left alone so a file being added is never caught half way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 19:08:21 -05:00
co-authored by Claude Opus 5.5
parent ad75f3ac7e
commit 1a98a23d1f
8 changed files with 291 additions and 3 deletions
+31
View File
@@ -137,6 +137,37 @@ func (q *Queries) DeleteAlbumIfEmpty(ctx context.Context, id pgtype.UUID) (Delet
return i, err
}
const deleteEmptyAlbums = `-- name: DeleteEmptyAlbums :many
DELETE FROM albums a
WHERE NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)
AND a.created_at < now() - interval '10 minutes'
RETURNING a.id
`
// The scan's tidy-up: removes every album no track points to, missing or
// not. A row made in the last ten minutes is left alone, so a file being
// added right now (its album created, its track not yet written) is never
// caught between the two.
func (q *Queries) DeleteEmptyAlbums(ctx context.Context) ([]pgtype.UUID, error) {
rows, err := q.db.Query(ctx, deleteEmptyAlbums)
if err != nil {
return nil, err
}
defer rows.Close()
var items []pgtype.UUID
for rows.Next() {
var id pgtype.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAlbumByArtistAndTitle = `-- name: GetAlbumByArtistAndTitle :one
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at FROM albums
WHERE albums.artist_id = $1 AND albums.title = $2
+34
View File
@@ -66,6 +66,40 @@ func (q *Queries) DeleteArtistIfEmpty(ctx context.Context, id pgtype.UUID) (pgty
return id_2, err
}
const deleteEmptyArtists = `-- name: DeleteEmptyArtists :many
DELETE FROM artists a
WHERE NOT EXISTS (SELECT 1 FROM albums al WHERE al.artist_id = a.id)
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.artist_id = a.id)
AND NOT EXISTS (SELECT 1 FROM playlists p WHERE p.seed_artist_id = a.id)
AND a.created_at < now() - interval '10 minutes'
RETURNING a.id
`
// The scan's tidy-up: removes every artist with no albums and no tracks.
// An artist a Songs like playlist is seeded from is kept, since that
// playlist must always name its seed; it goes when the playlist is next
// rebuilt without it. Rows made in the last ten minutes are left alone, as
// in DeleteEmptyAlbums.
func (q *Queries) DeleteEmptyArtists(ctx context.Context) ([]pgtype.UUID, error) {
rows, err := q.db.Query(ctx, deleteEmptyArtists)
if err != nil {
return nil, err
}
defer rows.Close()
var items []pgtype.UUID
for rows.Next() {
var id pgtype.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getArtistByID = `-- name: GetArtistByID :one
SELECT id, name, sort_name, mbid, created_at, updated_at, artist_thumb_path, artist_fanart_path, artist_art_source, artist_art_sources_version FROM artists WHERE id = $1
`
+10
View File
@@ -221,6 +221,16 @@ WHERE a.id = $1
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)
RETURNING a.id, a.artist_id;
-- name: DeleteEmptyAlbums :many
-- The scan's tidy-up: removes every album no track points to, missing or
-- not. A row made in the last ten minutes is left alone, so a file being
-- added right now (its album created, its track not yet written) is never
-- caught between the two.
DELETE FROM albums a
WHERE NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)
AND a.created_at < now() - interval '10 minutes'
RETURNING a.id;
-- name: SetAlbumMbidIfNull :exec
-- Heal MBID on existing album rows during rescan. Idempotent —
-- only updates when the existing mbid is NULL.
+13
View File
@@ -70,6 +70,19 @@ LEFT JOIN LATERAL (
ORDER BY artists.sort_name, artists.name, artists.id
LIMIT $1 OFFSET $2;
-- name: DeleteEmptyArtists :many
-- The scan's tidy-up: removes every artist with no albums and no tracks.
-- An artist a Songs like playlist is seeded from is kept, since that
-- playlist must always name its seed; it goes when the playlist is next
-- rebuilt without it. Rows made in the last ten minutes are left alone, as
-- in DeleteEmptyAlbums.
DELETE FROM artists a
WHERE NOT EXISTS (SELECT 1 FROM albums al WHERE al.artist_id = a.id)
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.artist_id = a.id)
AND NOT EXISTS (SELECT 1 FROM playlists p WHERE p.seed_artist_id = a.id)
AND a.created_at < now() - interval '10 minutes'
RETURNING a.id;
-- name: DeleteArtistIfEmpty :one
-- Deletes the artist row only when it has no remaining albums
-- AND no remaining tracks (defensive — tracks can in principle exist