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:
@@ -85,3 +85,14 @@ SELECT COUNT(*) FROM albums;
|
||||
-- Used by request-progress reporting to count how many albums have been
|
||||
-- ingested for a matched artist while a request is still in flight.
|
||||
SELECT COUNT(*) FROM albums WHERE artist_id = $1;
|
||||
|
||||
-- name: DeleteAlbumIfEmpty :one
|
||||
-- M7 #372: deletes the album row only when it has no remaining tracks.
|
||||
-- Used after a track delete to tidy up orphaned albums. RETURNING gives
|
||||
-- us the artist_id so the service can chain into the artist-empty check.
|
||||
-- Returns no rows when the album still has tracks; the service treats
|
||||
-- pgx.ErrNoRows as "album not orphaned, skip cascade".
|
||||
DELETE FROM albums a
|
||||
WHERE a.id = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)
|
||||
RETURNING a.id, a.artist_id;
|
||||
|
||||
Reference in New Issue
Block a user