From 85673f988193ea5fbb8fb89a6a2fba46467cf791 Mon Sep 17 00:00:00 2001 From: bvandeusen Date: Sun, 19 Apr 2026 02:31:36 +0000 Subject: [PATCH] M1/#292: track queries (upsert-by-file_path/get-by-id/get-by-path/list-by-album) --- internal/db/queries/tracks.sql | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 internal/db/queries/tracks.sql diff --git a/internal/db/queries/tracks.sql b/internal/db/queries/tracks.sql new file mode 100644 index 00000000..250a9766 --- /dev/null +++ b/internal/db/queries/tracks.sql @@ -0,0 +1,29 @@ +-- name: UpsertTrack :one +-- file_path is the canonical identity for library scan; mbid is secondary. +INSERT INTO tracks ( + title, album_id, artist_id, track_number, disc_number, + duration_ms, file_path, file_size, file_format, bitrate, mbid, genre +) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) +ON CONFLICT (file_path) DO UPDATE SET + title = EXCLUDED.title, + album_id = EXCLUDED.album_id, + artist_id = EXCLUDED.artist_id, + track_number = EXCLUDED.track_number, + disc_number = EXCLUDED.disc_number, + duration_ms = EXCLUDED.duration_ms, + file_size = EXCLUDED.file_size, + file_format = EXCLUDED.file_format, + bitrate = EXCLUDED.bitrate, + mbid = EXCLUDED.mbid, + genre = EXCLUDED.genre, + updated_at = now() +RETURNING *; + +-- name: GetTrackByID :one +SELECT * FROM tracks WHERE id = $1; + +-- name: GetTrackByPath :one +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;