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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -96,3 +96,12 @@ ORDER BY albums.release_date NULLS LAST, albums.sort_title,
|
||||
-- matched artist (sum across all the artist's albums) while a request
|
||||
-- is still in flight.
|
||||
SELECT COUNT(*) FROM tracks WHERE artist_id = $1;
|
||||
|
||||
-- name: DeleteTrack :one
|
||||
-- M7 #372: hard delete with FK cascade. The CASCADE on track_id from
|
||||
-- play_events / general_likes_tracks / lidarr_quarantine /
|
||||
-- lidarr_quarantine_actions handles their cleanup. RETURNING gives us
|
||||
-- album_id + artist_id for the album-empty / artist-empty cascade
|
||||
-- checks the service does next.
|
||||
DELETE FROM tracks WHERE id = $1
|
||||
RETURNING id, album_id, artist_id, file_path, mbid;
|
||||
|
||||
Reference in New Issue
Block a user