Rewrites the internal/db/queries comments to rule 32 and regenerates dbq, whose doc comments and embedded SQL carry them. The bugs behind the tiered mixes, the md5 order seed, the score bands and the round-robin arms are restated as the constraint each one enforces; counts from one library and stale cross-references are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4.5 KiB
SQL
102 lines
4.5 KiB
SQL
-- The release catalogue read from MusicBrainz.
|
|
|
|
-- name: UpsertMBArtist :exec
|
|
INSERT INTO mb_artists (mbid, name, sort_name, disambiguation, artist_type, country)
|
|
VALUES (sqlc.arg(mbid), sqlc.arg(name), sqlc.arg(sort_name), sqlc.arg(disambiguation),
|
|
sqlc.arg(artist_type), sqlc.arg(country))
|
|
ON CONFLICT (mbid) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
sort_name = EXCLUDED.sort_name,
|
|
disambiguation = EXCLUDED.disambiguation,
|
|
artist_type = EXCLUDED.artist_type,
|
|
country = EXCLUDED.country,
|
|
updated_at = now();
|
|
|
|
-- name: GetMBArtist :one
|
|
SELECT * FROM mb_artists WHERE mbid = sqlc.arg(mbid);
|
|
|
|
-- name: MarkMBArtistDiscographyRefreshed :exec
|
|
UPDATE mb_artists SET discography_refreshed_at = now() WHERE mbid = sqlc.arg(mbid);
|
|
|
|
-- name: ListMBArtistsDueForRefresh :many
|
|
-- Artists whose discography has never been read, or was read before the
|
|
-- cutoff: never-read first, then oldest first.
|
|
SELECT mbid FROM mb_artists
|
|
WHERE discography_refreshed_at IS NULL OR discography_refreshed_at < sqlc.arg(before)
|
|
ORDER BY discography_refreshed_at NULLS FIRST, mbid
|
|
LIMIT sqlc.arg(max_rows);
|
|
|
|
-- name: UpsertMBReleaseGroup :exec
|
|
-- Leaves releases_refreshed_at alone: re-reading a discography says nothing
|
|
-- about whether the group's track lists are current.
|
|
INSERT INTO mb_release_groups (mbid, title, primary_type, secondary_types, first_release_date, artist_credit)
|
|
VALUES (sqlc.arg(mbid), sqlc.arg(title), sqlc.arg(primary_type), sqlc.arg(secondary_types)::text[],
|
|
sqlc.arg(first_release_date), sqlc.arg(artist_credit))
|
|
ON CONFLICT (mbid) DO UPDATE SET
|
|
title = EXCLUDED.title,
|
|
primary_type = EXCLUDED.primary_type,
|
|
secondary_types = EXCLUDED.secondary_types,
|
|
first_release_date = EXCLUDED.first_release_date,
|
|
artist_credit = EXCLUDED.artist_credit,
|
|
updated_at = now();
|
|
|
|
-- name: GetMBReleaseGroup :one
|
|
SELECT * FROM mb_release_groups WHERE mbid = sqlc.arg(mbid);
|
|
|
|
-- name: DeleteMBArtistReleaseGroups :exec
|
|
DELETE FROM mb_artist_release_groups WHERE artist_mbid = sqlc.arg(artist_mbid);
|
|
|
|
-- name: AddMBArtistReleaseGroup :exec
|
|
INSERT INTO mb_artist_release_groups (artist_mbid, release_group_mbid)
|
|
VALUES (sqlc.arg(artist_mbid), sqlc.arg(release_group_mbid))
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- name: ListMBArtistReleaseGroups :many
|
|
-- An artist's discography, oldest first; undated groups last.
|
|
SELECT g.* FROM mb_release_groups g
|
|
JOIN mb_artist_release_groups ag ON ag.release_group_mbid = g.mbid
|
|
WHERE ag.artist_mbid = sqlc.arg(artist_mbid)
|
|
ORDER BY g.first_release_date = '', g.first_release_date, g.title, g.mbid;
|
|
|
|
-- name: MarkMBReleaseGroupReleasesRefreshed :exec
|
|
UPDATE mb_release_groups SET releases_refreshed_at = now() WHERE mbid = sqlc.arg(mbid);
|
|
|
|
-- name: DeleteMBReleasesForGroup :exec
|
|
-- Tracks go with their releases (ON DELETE CASCADE).
|
|
DELETE FROM mb_releases WHERE release_group_mbid = sqlc.arg(release_group_mbid);
|
|
|
|
-- name: InsertMBRelease :exec
|
|
INSERT INTO mb_releases (mbid, release_group_mbid, title, status, release_date, country,
|
|
disambiguation, formats, track_count)
|
|
VALUES (sqlc.arg(mbid), sqlc.arg(release_group_mbid), sqlc.arg(title), sqlc.arg(status),
|
|
sqlc.arg(release_date), sqlc.arg(country), sqlc.arg(disambiguation), sqlc.arg(formats),
|
|
sqlc.arg(track_count))
|
|
ON CONFLICT (mbid) DO UPDATE SET
|
|
release_group_mbid = EXCLUDED.release_group_mbid,
|
|
title = EXCLUDED.title,
|
|
status = EXCLUDED.status,
|
|
release_date = EXCLUDED.release_date,
|
|
country = EXCLUDED.country,
|
|
disambiguation = EXCLUDED.disambiguation,
|
|
formats = EXCLUDED.formats,
|
|
track_count = EXCLUDED.track_count;
|
|
|
|
-- name: InsertMBReleaseTrack :exec
|
|
INSERT INTO mb_release_tracks (release_mbid, medium_position, position, medium_format, number,
|
|
title, length_ms, recording_mbid)
|
|
VALUES (sqlc.arg(release_mbid), sqlc.arg(medium_position), sqlc.arg(position),
|
|
sqlc.arg(medium_format), sqlc.arg(number), sqlc.arg(title), sqlc.arg(length_ms),
|
|
sqlc.arg(recording_mbid));
|
|
|
|
-- name: ListMBReleasesForGroup :many
|
|
-- Official editions first, then by date; undated last.
|
|
SELECT * FROM mb_releases
|
|
WHERE release_group_mbid = sqlc.arg(release_group_mbid)
|
|
ORDER BY status <> 'Official', release_date = '', release_date, title, mbid;
|
|
|
|
-- name: ListMBReleaseTracksForGroup :many
|
|
SELECT t.* FROM mb_release_tracks t
|
|
JOIN mb_releases r ON r.mbid = t.release_mbid
|
|
WHERE r.release_group_mbid = sqlc.arg(release_group_mbid)
|
|
ORDER BY t.release_mbid, t.medium_position, t.position;
|