release / web (push) Successful in 1m16s
release / go (push) Successful in 1m47s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m59s
release / android (push) Successful in 4m30s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 15s
release / Verify release artifacts (tag releases only) (push) Skipped
Subsonic apps can now list genres and play one (getGenres, getSongsByGenre), and apps that browse by folder, such as DSub, can open an artist to its albums and an album to its songs (getMusicDirectory), with the album lists and search they use in that mode (getAlbumList, search2). Genres are split and counted as the web's genre index counts them, now with an album count beside the song count. A genre's songs leave out the listener's quarantined ones and missing files. Folders are Minstrel's artists and albums, under the same ids as the tag-based calls. getAlbumList2 and search3 now share their selection with the folder versions, so the two modes list and find the same things. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
8.2 KiB
SQL
193 lines
8.2 KiB
SQL
-- Every query in this file filters `tracks.missing_since IS NULL`. A row whose
|
|
-- file has vanished keeps its genre forever — the scanner walks the
|
|
-- filesystem, so it never revisits a path that no longer exists — and
|
|
-- browsing is a way of finding something to play, so a track that cannot play
|
|
-- should not shape it.
|
|
--
|
|
-- The year axis filters too, so the two axes agree: an album whose every file
|
|
-- has vanished is absent from both, rather than absent from its genre and
|
|
-- still listed under its year, where opening it would find nothing playable.
|
|
--
|
|
-- Hiding rather than showing-and-marking, because browsing is how you go
|
|
-- looking for something to play; the admin missing-files surface is where
|
|
-- absence is reported.
|
|
--
|
|
-- The predicate is "has at least one playable track", so it also excludes an
|
|
-- album with no tracks at all. That is the same answer for the same reason:
|
|
-- nothing to play, nothing to browse to.
|
|
|
|
-- name: ListGenresWithCount :many
|
|
-- Genre browse index.
|
|
--
|
|
-- Genres live inline on tracks.genre as a delimited string, so this splits on
|
|
-- the same [;,] pattern already used by recommendation.sql and discover.sql —
|
|
-- a track tagged "Rock;Pop" must count toward both, and diverging from the
|
|
-- established pattern here would make the browse surface disagree with what
|
|
-- the recommendation engine believes the library contains.
|
|
--
|
|
-- trim() but deliberately NO lower(): trimming repairs an artifact of OUR
|
|
-- splitting ("Rock; Pop" yields " Pop", and showing that as a distinct genre
|
|
-- would be a bug), whereas case is what the tag actually says. Raw ID3 is
|
|
-- exposed as-is, so "Rock" and "rock" appear as separate rows.
|
|
--
|
|
-- COUNT(DISTINCT) because a sloppy tag like "Rock;Rock" would otherwise
|
|
-- inflate its own row.
|
|
--
|
|
-- Ordered by count first: raw ID3 data has a long tail of one-off junk tags,
|
|
-- so alphabetical would bury the handful of genres a library actually has
|
|
-- a lot of. Name breaks ties for a stable order.
|
|
--
|
|
-- album_count is the albums holding at least one of those tracks, which is
|
|
-- what a Subsonic app shows beside the genre.
|
|
SELECT trim(g.genre) AS genre, COUNT(DISTINCT tracks.id)::bigint AS track_count,
|
|
COUNT(DISTINCT tracks.album_id)::bigint AS album_count
|
|
FROM tracks
|
|
JOIN LATERAL regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre) ON true
|
|
WHERE trim(g.genre) <> ''
|
|
AND tracks.missing_since IS NULL
|
|
GROUP BY trim(g.genre)
|
|
-- Ordered by the expression, not the output alias: `ORDER BY genre` is
|
|
-- ambiguous between the alias and tracks.genre, and sqlc rejects it.
|
|
ORDER BY track_count DESC, trim(g.genre);
|
|
|
|
-- name: ListAlbumsByGenreWithArtist :many
|
|
-- Albums for one genre, joined with artist_name for the browse grid.
|
|
-- An album belongs to a genre when ANY of its tracks carry it. Splits and
|
|
-- trims identically to ListGenresWithCount — if the list is built by
|
|
-- splitting and the detail matched exactly, every multi-genre track would
|
|
-- produce a genre row that leads to an empty page.
|
|
SELECT sqlc.embed(albums), artists.name AS artist_name
|
|
FROM albums
|
|
JOIN artists ON artists.id = albums.artist_id
|
|
WHERE EXISTS (
|
|
SELECT 1
|
|
FROM tracks
|
|
JOIN LATERAL regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre) ON true
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
AND trim(g.genre) = trim(sqlc.arg(genre)::text)
|
|
)
|
|
ORDER BY albums.sort_title, albums.id
|
|
LIMIT sqlc.arg(lim) OFFSET sqlc.arg(off);
|
|
|
|
-- name: ListTracksByGenre :many
|
|
-- One genre's songs for the Subsonic getSongsByGenre, a page at a time.
|
|
-- Splits and trims identically to ListGenresWithCount, so the songs a genre
|
|
-- leads to are the ones it counted. The listener's quarantined tracks are
|
|
-- left out: an app asks for these to play them. Album by album in track
|
|
-- order, so paging walks a stable order.
|
|
SELECT sqlc.embed(tracks)
|
|
FROM tracks
|
|
JOIN albums ON albums.id = tracks.album_id
|
|
WHERE tracks.missing_since IS NULL
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre)
|
|
WHERE trim(g.genre) = trim(sqlc.arg(genre)::text)
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM quarantine q
|
|
WHERE q.user_id = sqlc.arg(user_id) AND q.track_id = tracks.id
|
|
)
|
|
ORDER BY albums.sort_title, albums.id,
|
|
tracks.disc_number NULLS FIRST, tracks.track_number NULLS FIRST, tracks.id
|
|
LIMIT sqlc.arg(lim) OFFSET sqlc.arg(off);
|
|
|
|
-- name: CountAlbumsByGenre :one
|
|
-- Total for the paging envelope. EXISTS mirrors the list query exactly; a
|
|
-- JOIN + DISTINCT here would count differently the moment an album has two
|
|
-- tracks carrying the same genre.
|
|
SELECT COUNT(*) FROM albums
|
|
WHERE EXISTS (
|
|
SELECT 1
|
|
FROM tracks
|
|
JOIN LATERAL regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre) ON true
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
AND trim(g.genre) = trim(sqlc.arg(genre)::text)
|
|
);
|
|
|
|
-- name: ListAlbumYearsWithCount :many
|
|
-- Year browse index. Only albums with a release_date appear — an
|
|
-- album with no date isn't "year unknown" as a browsable bucket, it's absent
|
|
-- from this axis, and the UI says so rather than inventing a 0 row.
|
|
-- Newest first: recent releases are the likelier browse target.
|
|
SELECT EXTRACT(YEAR FROM release_date)::int AS year, COUNT(*)::bigint AS album_count
|
|
FROM albums
|
|
WHERE release_date IS NOT NULL
|
|
AND EXISTS (
|
|
SELECT 1 FROM tracks
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
)
|
|
GROUP BY year
|
|
ORDER BY year DESC;
|
|
|
|
-- name: ListAlbumsByYearRangeWithArtist :many
|
|
-- Albums released within an inclusive year range, for the albums-page filter.
|
|
SELECT sqlc.embed(albums), artists.name AS artist_name
|
|
FROM albums
|
|
JOIN artists ON artists.id = albums.artist_id
|
|
WHERE albums.release_date IS NOT NULL
|
|
AND EXTRACT(YEAR FROM albums.release_date)::int
|
|
BETWEEN sqlc.arg(year_from)::int AND sqlc.arg(year_to)::int
|
|
AND EXISTS (
|
|
SELECT 1 FROM tracks
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
)
|
|
ORDER BY albums.sort_title, albums.id
|
|
LIMIT sqlc.arg(lim) OFFSET sqlc.arg(off);
|
|
|
|
-- name: CountAlbumsByYearRange :one
|
|
SELECT COUNT(*) FROM albums
|
|
WHERE release_date IS NOT NULL
|
|
AND EXTRACT(YEAR FROM release_date)::int
|
|
BETWEEN sqlc.arg(year_from)::int AND sqlc.arg(year_to)::int
|
|
AND EXISTS (
|
|
SELECT 1 FROM tracks
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
);
|
|
|
|
-- name: ListGenresForAlbum :many
|
|
-- Distinct genres carried by an album's tracks, for the album detail page's
|
|
-- quick-jump chips. Split and trimmed identically to ListGenresWithCount, so a
|
|
-- chip always leads to a page that actually contains this album.
|
|
SELECT DISTINCT trim(g.genre) AS genre
|
|
FROM tracks
|
|
JOIN LATERAL regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre) ON true
|
|
WHERE tracks.album_id = $1 AND trim(g.genre) <> ''
|
|
AND tracks.missing_since IS NULL
|
|
ORDER BY trim(g.genre);
|
|
|
|
-- name: ListGenresForArtist :many
|
|
-- Same, across everything by one artist. Alphabetical rather than by count:
|
|
-- an artist's genre set is small, and a stable order reads better than a
|
|
-- frequency ranking nobody asked about.
|
|
SELECT DISTINCT trim(g.genre) AS genre
|
|
FROM tracks
|
|
JOIN LATERAL regexp_split_to_table(coalesce(tracks.genre, ''), '[;,]') AS g(genre) ON true
|
|
WHERE tracks.artist_id = $1 AND trim(g.genre) <> ''
|
|
AND tracks.missing_since IS NULL
|
|
ORDER BY trim(g.genre);
|
|
|
|
-- name: ListAlbumsByYearOrdered :many
|
|
-- Subsonic getAlbumList2 type=byYear: albums released within an inclusive
|
|
-- year range, oldest first, or newest first when the caller asks for the
|
|
-- range backwards (fromYear > toYear), as the Subsonic API defines it.
|
|
SELECT albums.* FROM albums
|
|
WHERE albums.release_date IS NOT NULL
|
|
AND EXTRACT(YEAR FROM albums.release_date)::int
|
|
BETWEEN LEAST(sqlc.arg(from_year)::int, sqlc.arg(to_year)::int)
|
|
AND GREATEST(sqlc.arg(from_year)::int, sqlc.arg(to_year)::int)
|
|
AND EXISTS (
|
|
SELECT 1 FROM tracks
|
|
WHERE tracks.album_id = albums.id
|
|
AND tracks.missing_since IS NULL
|
|
)
|
|
ORDER BY CASE WHEN sqlc.arg(from_year)::int <= sqlc.arg(to_year)::int THEN albums.release_date END ASC,
|
|
CASE WHEN sqlc.arg(from_year)::int > sqlc.arg(to_year)::int THEN albums.release_date END DESC,
|
|
albums.sort_title, albums.id
|
|
LIMIT sqlc.arg(lim) OFFSET sqlc.arg(off);
|