refactor(server/sql): unify *ForUser track queries via nullable user_id (A1)

This commit is contained in:
2026-05-08 08:15:19 -04:00
parent b1ef3c3688
commit bffa397250
7 changed files with 49 additions and 163 deletions
+12 -22
View File
@@ -26,23 +26,9 @@ SELECT * FROM tracks WHERE id = $1;
SELECT * FROM tracks WHERE file_path = $1;
-- name: ListTracksByAlbum :many
SELECT * FROM tracks WHERE album_id = $1 ORDER BY disc_number NULLS LAST, track_number NULLS LAST;
-- name: CountTracksByAlbum :one
SELECT count(*) FROM tracks WHERE album_id = $1;
-- name: SearchTracks :many
SELECT * FROM tracks
WHERE title ILIKE '%' || $1 || '%'
ORDER BY title
LIMIT $2 OFFSET $3;
-- name: CountTracksMatching :one
SELECT COUNT(*) FROM tracks WHERE title ILIKE '%' || $1::text || '%';
-- name: ListTracksByAlbumForUser :many
-- Same as ListTracksByAlbum but excludes tracks the user has quarantined.
-- $1 = album_id, $2 = user_id.
-- $1 = album_id, $2 = user_id. Pass pgtype.UUID{Valid: false} (NULL)
-- to skip the per-user quarantine filter; the NOT EXISTS clause on
-- a NULL user_id never matches a row, so every track passes through.
SELECT * FROM tracks
WHERE album_id = $1
AND NOT EXISTS (
@@ -51,10 +37,14 @@ WHERE album_id = $1
)
ORDER BY disc_number NULLS LAST, track_number NULLS LAST;
-- name: SearchTracksForUser :many
-- $1 = title query, $2 = user_id, $3 = limit, $4 = offset.
-- name: CountTracksByAlbum :one
SELECT count(*) FROM tracks WHERE album_id = $1;
-- name: SearchTracks :many
-- $1 = title query, $2 = user_id (NULL to skip quarantine filter),
-- $3 = limit, $4 = offset.
SELECT * FROM tracks
WHERE title ILIKE '%' || $1 || '%'
WHERE title ILIKE '%' || $1::text || '%'
AND NOT EXISTS (
SELECT 1 FROM lidarr_quarantine q
WHERE q.user_id = $2 AND q.track_id = tracks.id
@@ -62,8 +52,8 @@ WHERE title ILIKE '%' || $1 || '%'
ORDER BY title
LIMIT $3 OFFSET $4;
-- name: CountTracksMatchingForUser :one
-- $1 = title query, $2 = user_id.
-- name: CountTracksMatching :one
-- $1 = title query, $2 = user_id (NULL to skip quarantine filter).
SELECT COUNT(*) FROM tracks
WHERE title ILIKE '%' || $1::text || '%'
AND NOT EXISTS (