Files
minstrel/internal/db/queries/missing_pairs.sql
T
bvandeusenandClaude Opus 5.5 285a24f0ab
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m48s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m56s
release / android (push) Successful in 4m19s
release / Build signed APK (push) Successful in 4m40s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(db): fold every migration into one baseline at version 106
0106_baseline.up.sql is the schema at version 106 and the settings rows
it starts with, taken from a database migrated through 0001–0106; a
database built from the baseline alone dumps identically. A database
already at 106 skips it. Comments that pointed at a migration number now
name the table or constraint instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 00:57:18 -05:00

81 lines
3.6 KiB
SQL

-- Missing-pair pass. A track marked missing whose replacement is already
-- on disk under another row — most often a quality upgrade, mp3 → flac at a new
-- path — is folded into that replacement, so its history moves across and
-- re-acquisition stops chasing music the library already has.
--
-- Move adoption cannot catch these: it links a new file to a missing row
-- at the moment the file is first inserted, and the replacement arrives as a
-- re-encode with no recording MBID, so it has nothing to match on. AcoustID may
-- give it an MBID later; by then both rows exist and only a fold can join them.
-- name: ListMissingTrackPairs :many
-- Every candidate (missing, present) pair, with how many candidates each side
-- has. Two rules:
--
-- mbid the same recording MBID, within the album group (the album row, or
-- albums sharing a release group).
-- title the same album row and the same title, ignoring case. The
-- replacements are often not the original file — another edition,
-- re-encoded, untagged — so the metadata is what is left to go on.
--
-- Deliberately NOT gated on duration or track position. Pairs known to be one
-- recording often differ by over 2s (old mp3 durations are poor estimates),
-- and the poorly tagged set is exactly where numbering is broken.
--
-- A pair whose MBIDs are both set and differ is not a title match: the tags say
-- they are different recordings. The caller folds a pair only when each side has
-- exactly one candidate; anything else is ambiguous and left alone.
--
-- A slot the duplicate resolver emptied (emptied_at) pairs by
-- title only with a track added since: the album held no copy of the song as
-- long as it is, so one already there under the title is another song.
WITH grp AS (
SELECT id AS album_id, COALESCE(release_group_mbid, id::text) AS g
FROM albums
),
cand AS (
SELECT m.id AS missing_id, p.id AS present_id, 'mbid'::text AS rule
FROM tracks m
JOIN grp gm ON gm.album_id = m.album_id
JOIN grp gp ON gp.g = gm.g
JOIN tracks p ON p.album_id = gp.album_id
AND p.missing_since IS NULL
AND p.mbid = m.mbid
WHERE m.missing_since IS NOT NULL
AND m.mbid IS NOT NULL
UNION ALL
SELECT m.id, p.id, 'title'::text
FROM tracks m
JOIN tracks p ON p.album_id = m.album_id
AND p.missing_since IS NULL
AND lower(p.title) = lower(m.title)
WHERE m.missing_since IS NOT NULL
AND (m.mbid IS NULL OR p.mbid IS NULL OR m.mbid = p.mbid)
AND (m.emptied_at IS DISTINCT FROM m.missing_since OR p.added_at > m.emptied_at)
),
pairs AS (
-- A pair both rules found is one candidate, credited to the stronger rule.
SELECT missing_id, present_id, min(rule)::text AS rule
FROM cand
GROUP BY missing_id, present_id
)
SELECT pr.missing_id, pr.present_id, pr.rule,
m.file_path AS missing_path, p.file_path AS present_path,
(count(*) OVER (PARTITION BY pr.missing_id))::int AS missing_candidates,
(count(*) OVER (PARTITION BY pr.present_id))::int AS present_candidates
FROM pairs pr
JOIN tracks m ON m.id = pr.missing_id
JOIN tracks p ON p.id = pr.present_id
ORDER BY m.file_path, p.file_path;
-- name: LockTracksForPairing :many
-- Locks both rows of a pair for the fold's transaction, in id order so two
-- passes cannot deadlock, and returns what the fold re-checks: the missing row
-- must still be missing and the present one still present. A scan may have
-- restored or adopted the first, or marked the second, since the pairs were listed.
SELECT id, missing_since
FROM tracks
WHERE id = ANY(sqlc.arg(ids)::uuid[])
ORDER BY id
FOR UPDATE;