Repository navigation
fix(collections): correct multi-library sync — match all bound libraries and dedupe items - #184
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughLibrary collection sync is refactored to check item membership across multiple library IDs instead of a single library. ChangesCollection sync multi-library support and robustness
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Collection sync resolved item membership against only the legacy library_collections.library_id, so a collection bound to multiple libraries (N:N library_collection_libraries) had its items collapsed to a single library on every sync. Switch the MDBList/TMDB/Trakt sync matchers to the existing LibraryItemRepository.GetItemsInFolders over collection.LibraryIDs. GetByID already populates LibraryIDs from the N:N table (falling back to [LibraryID]), so single-library collections are unaffected. No schema change; listing and the catalog open path were already N:N-aware. Unblocks merging duplicated per-library collections into single multi-library collections.
84b2a0a to
baa5949
Compare
…eItems A collection sync resolves source entries (external IDs) to library content_ids and appends one item per matched entry without deduping by media_item_id. When two different source entries resolve to the same library item, the same content_id is appended twice; ReplaceItems then hits the library_collection_items composite PK (collection_id, media_item_id) with a duplicate-key violation, failing the whole sync with an HTTP 500 and zero items. The multi-library union added in the previous commit (GetItemsInFolders over collection.LibraryIDs) widens the matched set and so raises the collision probability, making this latent bug more likely to fire. Dedupe at the single choke point in ReplaceItems (first occurrence wins, preserving rank order) so all five sync paths are covered at once, and renumber position over the surviving rows so it stays dense. Also log the underlying error in HandleSyncAdminCollection, which previously mapped every non-sentinel sync error to a generic 500 with no log line, making this failure invisible on the server.
|
Added a second commit ( It fixes a dedupe bug: if the same media item showed up twice in a collection, the sync crashed on a primary-key collision ( The duplicate happens when the same item appears more than once — either because it's in multiple of the collection's libraries, or because the mdblist itself lists it twice. The multi-library union in this PR makes it more likely, which is how it surfaced. The commit dedupes by |
…ies and dedupe items (Silo-Server#184) * fix(collections): sync matches across all bound libraries Collection sync resolved item membership against only the legacy library_collections.library_id, so a collection bound to multiple libraries (N:N library_collection_libraries) had its items collapsed to a single library on every sync. Switch the MDBList/TMDB/Trakt sync matchers to the existing LibraryItemRepository.GetItemsInFolders over collection.LibraryIDs. GetByID already populates LibraryIDs from the N:N table (falling back to [LibraryID]), so single-library collections are unaffected. No schema change; listing and the catalog open path were already N:N-aware. Unblocks merging duplicated per-library collections into single multi-library collections. * fix(collections): dedupe matched items by media_item_id before ReplaceItems A collection sync resolves source entries (external IDs) to library content_ids and appends one item per matched entry without deduping by media_item_id. When two different source entries resolve to the same library item, the same content_id is appended twice; ReplaceItems then hits the library_collection_items composite PK (collection_id, media_item_id) with a duplicate-key violation, failing the whole sync with an HTTP 500 and zero items. The multi-library union added in the previous commit (GetItemsInFolders over collection.LibraryIDs) widens the matched set and so raises the collision probability, making this latent bug more likely to fire. Dedupe at the single choke point in ReplaceItems (first occurrence wins, preserving rank order) so all five sync paths are covered at once, and renumber position over the surviving rows so it stays dense. Also log the underlying error in HandleSyncAdminCollection, which previously mapped every non-sentinel sync error to a generic 500 with no log line, making this failure invisible on the server.
Two commits, both correcting collection sync: one fixes which libraries are matched, the second hardens the write path against a duplicate-key 500 that the first commit makes more likely.
Problem
1. Collection sync silently dropped items from all but one library.
A collection can be bound to several libraries (for example a "Netflix Originals"
collection spanning both the movie and series libraries). On the admin/library
collections view, such a collection listed and opened correctly — but every time it
ran a scheduled or manual sync, its membership collapsed down to just one library's
matches. Operators saw collections that opened with ~161 items shrink to ~50 after a
sync, with the entire other class (e.g. all the series) disappearing. Worst hit were the
cross-class merged collections where the surviving "legacy" library was a single
movie-only or series-only folder, so a whole media class vanished.
2. Some collections became permanently un-syncable, with no error in the logs.
A single collection could start returning a generic failure on every sync — HTTP 500,
0 items, in ~250ms, with no error or warning line on the server. Through the CDN it
rendered as a misleading "524 - A Timeout Occurred" page, so it looked like a network
timeout rather than an application error. It was data-dependent, so it surfaced
unpredictably as catalogs grew (on dev, 1 of 50 collections hit it). The multi-library
union in the first fix widens the set of matched items, which makes this failure more
likely to appear.
Solution
fix(collections): sync matches across all bound libraries
Collections were historically 1:1 with a library via the legacy
library_collections.library_id. Migration 015 added the N:N membership tablelibrary_collection_libraries; the listing and catalog open read paths werealready updated to use it, and
GetByIDalready populatescollection.LibraryIDsfromthe N:N table (falling back to
[LibraryID]when empty —internal/catalog/library_collection_repo.go:169). Only the sync path was leftmatching against the single legacy
collection.LibraryID, soReplaceItems(a fulloverwrite) rebuilt membership from just one library.
The fix routes every sync matcher through the already-existing multi-folder helper
LibraryItemRepository.GetItemsInFolders(internal/catalog/library_repo.go:164,media_folder_id = ANY($1)) and threadscollection.LibraryIDsthrough the resolvers,all in
internal/catalog/library_collection_service.go:syncMDBListCollectionmembership query now usesGetItemsInFolders(ctx, candidateIDs, collection.LibraryIDs)(
library_collection_service.go:337).resolveTMDBEntry(:1069) andresolveTraktEntry(:1104) takelibraryIDs []intand call
GetItemsInFoldersinternally (:1092,:1128).:472, tmdb_franchise:636, tmdb_discover:811,trakt_preset
:973) passcollection.LibraryIDs.%voverLibraryIDs).Because
GetByIDguaranteesLibraryIDscontains at least the legacy id, single-librarycollections are byte-for-byte unaffected. No schema change.
This unblocks merging the duplicated per-library collections into single multi-library
collections.
fix(collections): dedupe matched items by media_item_id before ReplaceItems
Each sync resolves source entries (external IDs) to library
content_ids and appends oneLibraryCollectionItemInput{MediaItemID: <content_id>}per matched entry, withoutdeduping by
MediaItemID. When two different source entries resolve to the samelibrary item — a remake/duplicate listing, one entry matched by IMDb and another by TMDB
pointing at the same local item, or (post-union) the same item present in more than one of
the collection's bound folders — the same
content_idis appended twice.library_collection_itemshas a composite primary key(collection_id, media_item_id)(migrations/sql/001_schema.sql:1302).ReplaceItems(internal/catalog/library_collection_repo.go:724) inserts each rowindividually, so the second insert of a colliding
media_item_idraises a duplicate-keyviolation that fails the whole transaction. All five sync paths
(
syncMDBListCollection,syncTMDBPresetCollection,syncTMDBFranchiseCollection,syncTMDBDiscoverCollection,syncTraktPresetCollection) share the pattern.The fix dedupes at the single choke point in
ReplaceItems: aseenset skips repeatedmedia_item_ids (first occurrence wins, preserving rank order), andpositionisrenumbered over the surviving rows so it stays dense. This covers all five paths at once
and is why it lives in the repo rather than each matcher.
Separately,
HandleSyncAdminCollection(
internal/api/handlers/library_collections.go:1559) mapped every non-sentinel syncerror to a generic 500 and logged nothing, which is why this was invisible on the
server. It now
slog.Errors the underlying error with the collection id before returningthe 500.
Why bundle the two fixes: they touch the same five sync functions and the same
ReplaceItemswrite path, the branch is unmerged, and the first commit's multi-libraryunion directly increases the duplicate-collision rate the second commit defends against —
so shipping the union without the dedupe guard would ship a partial regression.
Risk / follow-ups
HandleGetLibraryCollectionItems(internal/api/handlers/library_collections.go) stillgates on
collection.LibraryID != libraryID. Only the pinned-collection carouselconsumes it and it hides the rail silently on 404 — converting that to a membership
check is a low-priority follow-up, out of scope here.
library_idcolumn's role entirely is a larger change, deferred.ReplaceItemsmeanspositionis renumbered over survivors; any callerrelying on positions matching the pre-dedupe input slice would see denser values. No
current caller does.
lossy workaround for the dedupe bug; that workaround should be removed now this is fixed.
items/libraryItemsare concreteDB-backed repositories (not interfaces), so a meaningful test requires a Postgres
integration harness that does not yet exist in this package. Existing single-library
tests cover the
LibraryIDs == [LibraryID]path and stay green.Verification
go build ./internal/catalog/... ./internal/api/handlers/...go vet ./internal/catalog/...go test ./internal/catalog/...(passes)GetItemsInFolder/collection.LibraryIDreferences in the sync matchers, and that thededupe path preserves rank order and dense positions.
a collider returned 500 at
limit=100and 200 atlimit<=75; with the fix it syncs toN-1 distinct items without error.
AI-use disclosure
Implemented with AI assistance (Claude) from a human-authored handoff spec and bug report.
Summary by CodeRabbit