test-web / test (push) Successful in 57s
test-go / test (push) Successful in 1m16s
test-go / integration (push) Successful in 3m39s
release / Build signed APK (releases and dev) (push) Successful in 4m46s
release / Build + push container image (push) Successful in 26s
release / Verify release artifacts (tag releases only) (push) Skipped
Merge keeps one copy of a duplicate group and removes the rest. Every
table that references tracks does so ON DELETE CASCADE, so deleting a
duplicate's row outright would silently destroy its likes, plays,
playlist entries and tags. The merge moves all of that onto the kept
copy first, then deletes the empty row.
In one transaction, holding a lock on the group:
- repoints play_events, skip_events, contextual_likes, playback_errors,
lidarr_requests.matched_track_id and playlist_tracks. The last is
keyed by position, so every entry stays where it was.
- merges general_likes one per user, dated to the earlier like
- takes the union of track_tags, keeping the kept copy's own weight on
a shared tag
- rewrites track_similarity onto the kept copy, dropping edges that
would point a track at itself and keeping the kept copy's existing
edge on a collision
- lets the kept copy take a recording MBID only the removed copy had
- deletes the removed copies' rows, tidies emptied albums and artists,
marks the group merged
- logs sync changes: track deletes, and like and playlist-track
delete/upsert pairs
The removed copies' files are deleted first, before any row changes,
through the same helper as DeleteTrackFile (now shared, along with the
album tidy-up). A merge that left the file behind would be undone by
the next scan re-importing it. An unwritable library answers 409
library_not_writable and nothing changes.
tracks.Service.MergeDuplicates wraps it with the opt-in Lidarr unmonitor
from RemoveTrack, skipped when the removed copy is a second file of the
kept copy's own album track: unmonitoring that would stop Lidarr
managing the kept file. It writes a duplicate_merge audit row after
commit, per the audit package's best-effort contract, naming both
paths.
POST /api/admin/library/duplicates/{id}/merge takes an optional
survivor_track_id (the report's proposal otherwise) and unmonitor.
On the report page:
- each copy gets a Keep choice, defaulting to the proposed one
- Merge needs a second click, on a button that says how many files it
removes, with the consequence stated beside an opt-in Lidarr checkbox
Integration tests cover:
- every piece of history landing on the kept copy exactly: likes
deduped at the earlier time, plays and skips counted, playlist
position unchanged, tags unioned, similarity rewritten with no
duplicate or self-edge, MBID inherited
- the removed file gone, and a second merge refused
- an unwritable file leaving likes, plays, row and group untouched
- a survivor outside the group refused
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SQ31KQpYbStyK5y58UmPLH
102 lines
4.6 KiB
SQL
102 lines
4.6 KiB
SQL
-- Duplicate merge (Scribe #3911). Every statement here runs inside the one
|
|
-- transaction library.MergeDuplicateGroup opens, after the removed copy's file
|
|
-- is already gone. The loser's own track row is deleted last with DeleteTrack;
|
|
-- what these do is move everything it carries onto the survivor first, so that
|
|
-- delete's CASCADE finds nothing left to destroy.
|
|
|
|
-- name: LockDuplicateGroupForMerge :one
|
|
-- Locks the group for the rest of the transaction, so two merges of one group
|
|
-- cannot run at once.
|
|
SELECT id, tier, status
|
|
FROM duplicate_groups
|
|
WHERE id = sqlc.arg(id)
|
|
FOR UPDATE;
|
|
|
|
-- name: ListDuplicateGroupMergeMembers :many
|
|
SELECT t.id, t.file_path, t.file_format, t.file_size, t.added_at, t.album_id,
|
|
t.mbid, albums.mbid AS album_mbid
|
|
FROM duplicate_group_members m
|
|
JOIN tracks t ON t.id = m.track_id
|
|
JOIN albums ON albums.id = t.album_id
|
|
WHERE m.group_id = sqlc.arg(group_id)
|
|
ORDER BY t.id;
|
|
|
|
-- Plain repoints: no unique key involves track_id, so moving rows cannot collide.
|
|
|
|
-- name: MergeRepointPlayEvents :execrows
|
|
UPDATE play_events SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_id = sqlc.arg(loser_id)::uuid;
|
|
|
|
-- name: MergeRepointSkipEvents :execrows
|
|
UPDATE skip_events SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_id = sqlc.arg(loser_id)::uuid;
|
|
|
|
-- name: MergeRepointContextualLikes :execrows
|
|
UPDATE contextual_likes SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_id = sqlc.arg(loser_id)::uuid;
|
|
|
|
-- name: MergeRepointPlaybackErrors :execrows
|
|
UPDATE playback_errors SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_id = sqlc.arg(loser_id)::uuid;
|
|
|
|
-- name: MergeRepointLidarrRequests :execrows
|
|
UPDATE lidarr_requests SET matched_track_id = sqlc.arg(survivor_id)::uuid
|
|
WHERE matched_track_id = sqlc.arg(loser_id)::uuid;
|
|
|
|
-- name: MergeRepointPlaylistTracks :many
|
|
-- playlist_tracks is keyed by (playlist_id, position), so repointing keeps every
|
|
-- entry exactly where it was. A playlist that held both copies simply holds the
|
|
-- survivor twice — the user put two entries there, and both stay.
|
|
UPDATE playlist_tracks SET track_id = sqlc.arg(survivor_id)::uuid
|
|
WHERE track_id = sqlc.arg(loser_id)::uuid
|
|
RETURNING playlist_id;
|
|
|
|
-- Collision-safe merges: a unique key includes track_id, so the survivor may
|
|
-- already hold a matching row. Copy what it lacks; DeleteTrack's CASCADE then
|
|
-- removes the loser's originals.
|
|
|
|
-- name: MergeCopyGeneralLikes :many
|
|
-- One like per user. A user who liked both copies keeps a single like, dated to
|
|
-- the earlier of the two.
|
|
INSERT INTO general_likes (user_id, track_id, liked_at)
|
|
SELECT user_id, sqlc.arg(survivor_id)::uuid, liked_at
|
|
FROM general_likes
|
|
WHERE track_id = sqlc.arg(loser_id)::uuid
|
|
ON CONFLICT (user_id, track_id) DO UPDATE
|
|
SET liked_at = LEAST(general_likes.liked_at, EXCLUDED.liked_at)
|
|
RETURNING user_id;
|
|
|
|
-- name: MergeCopyTrackTags :execrows
|
|
INSERT INTO track_tags (track_id, tag, weight)
|
|
SELECT sqlc.arg(survivor_id)::uuid, tag, weight
|
|
FROM track_tags
|
|
WHERE track_id = sqlc.arg(loser_id)::uuid
|
|
ON CONFLICT (track_id, tag) DO NOTHING;
|
|
|
|
-- name: MergeCopyTrackSimilarity :execrows
|
|
-- Rewrites the loser to the survivor on either side of an edge. An edge between
|
|
-- the two copies would become a track similar to itself — the table forbids
|
|
-- that, and it means nothing — so it is dropped. An edge the survivor already
|
|
-- has from the same source is kept as it is.
|
|
INSERT INTO track_similarity (track_a_id, track_b_id, score, source, fetched_at)
|
|
SELECT CASE WHEN track_a_id = sqlc.arg(loser_id)::uuid THEN sqlc.arg(survivor_id)::uuid ELSE track_a_id END,
|
|
CASE WHEN track_b_id = sqlc.arg(loser_id)::uuid THEN sqlc.arg(survivor_id)::uuid ELSE track_b_id END,
|
|
score, source, fetched_at
|
|
FROM track_similarity
|
|
WHERE (track_a_id = sqlc.arg(loser_id)::uuid OR track_b_id = sqlc.arg(loser_id)::uuid)
|
|
AND (CASE WHEN track_a_id = sqlc.arg(loser_id)::uuid THEN sqlc.arg(survivor_id)::uuid ELSE track_a_id END)
|
|
<> (CASE WHEN track_b_id = sqlc.arg(loser_id)::uuid THEN sqlc.arg(survivor_id)::uuid ELSE track_b_id END)
|
|
ON CONFLICT (track_a_id, track_b_id, source) DO NOTHING;
|
|
|
|
-- name: MergeInheritTrackMbid :exec
|
|
-- A recording MBID is what the similarity pipeline keys on. If only the removed
|
|
-- copy carried one, the survivor takes it rather than going dark to similarity.
|
|
UPDATE tracks AS survivor
|
|
SET mbid = loser.mbid
|
|
FROM tracks AS loser
|
|
WHERE survivor.id = sqlc.arg(survivor_id)::uuid
|
|
AND loser.id = sqlc.arg(loser_id)::uuid
|
|
AND survivor.mbid IS NULL
|
|
AND loser.mbid IS NOT NULL;
|
|
|
|
-- name: MarkDuplicateGroupMerged :execrows
|
|
UPDATE duplicate_groups
|
|
SET status = 'merged', resolved_at = now()
|
|
WHERE id = sqlc.arg(id) AND status = 'pending';
|