feat(db): cascade-cleanup queries for M7 #372 track removal

DeleteTrack returns album_id + artist_id so the calling service can
chain the album-empty / artist-empty cascade. DeleteAlbumIfEmpty and
DeleteArtistIfEmpty are no-ops when other rows still reference the
parent — the service treats pgx.ErrNoRows as "not orphaned, skip".
This commit is contained in:
2026-05-02 21:53:33 -04:00
parent 9c53c9894a
commit 30b0edf97b
6 changed files with 103 additions and 0 deletions
+10
View File
@@ -64,3 +64,13 @@ LEFT JOIN LATERAL (
) cnt ON true
ORDER BY artists.sort_name, artists.name, artists.id
LIMIT $1 OFFSET $2;
-- name: DeleteArtistIfEmpty :one
-- M7 #372: deletes the artist row only when it has no remaining albums
-- AND no remaining tracks (defensive — tracks can in principle exist
-- without an album, though the scanner doesn't write that shape today).
DELETE FROM artists a
WHERE a.id = $1
AND 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)
RETURNING a.id;