Skip to content

fix(catalog): qualify content_id in discovery queries under library join - #80

Closed
d3v1l1989 wants to merge 1 commit into
Silo-Server:mainfrom
d3v1l1989:fix/discovery-repo-ambiguous-content-id
Closed

d3v1l1989 wants to merge 1 commit into
Silo-Server:mainfrom
d3v1l1989:fix/discovery-repo-ambiguous-content-id

Conversation

@d3v1l1989

@d3v1l1989 d3v1l1989 commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Two catalog issues were failing in nearby paths:

  1. Home/library discovery sections backed by DiscoveryRepository fail at runtime for library-scoped viewers with:
ERROR: column reference "content_id" is ambiguous (SQLSTATE 42702)

This affects the hidden_gems, critically_acclaimed, and forgotten_favorites section recipes when access joins media_item_libraries.

  1. The late orphan cleanup queries used after folder membership deletion did not reuse the full provisional media-item predicate, so the package-level regression test failed and the delete path could omit the status guard.

Root cause

ListByRatingThreshold, ListUnplayedHighRated, and ListForgottenFavorites conditionally join media_item_libraries mil to enforce library access. media_item_libraries also has a content_id column, but the queries selected from the unqualified itemColumns constant.

For late orphan cleanup, the collect/delete SQL fragments used only orphanedMediaItemSafetyConditions instead of orphanedProvisionalMediaItemConditions, dropping the mi.status IN (pending, unmatched, ambiguous) guard.

Fix

  • Select qualifiedItemColumns("mi") in the discovery query builders.
  • Reuse orphanedProvisionalMediaItemConditions in both late orphan collect/delete queries.

Testing

  • go test ./internal/catalog -run 'Discovery|RatingThreshold|Unplayed|ForgottenFavorites|TestLateOrphanCleanupUsesProvisionalSafetyPredicate' -count=1
  • go test ./internal/catalog -count=1
  • git diff --check

The hidden_gems, critically_acclaimed, and forgotten_favorites section
recipes failed for viewers scoped to specific libraries:

  ERROR: column reference "content_id" is ambiguous (SQLSTATE 42702)

ListByRatingThreshold, ListUnplayedHighRated, and ListForgottenFavorites
join media_item_libraries (which also has a content_id column) when access
is library-restricted, but selected the unqualified itemColumns list,
making the leading content_id ambiguous.

Select qualifiedItemColumns("mi") instead. The SQL-building logic for each
query is extracted into a testable builder so the regression test exercises
the real production query rather than a duplicated copy in the test file.
@coderabbitai

coderabbitai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR extracts SQL-building helpers for three discovery queries (rating-threshold, unplayed-high-rated, forgotten-favorites) to centralize access-filter logic. Each helper returns an empty query string when all libraries are excluded, and public methods now early-return empty results instead of handling this case inline. Column selections are qualified with the mi table alias to prevent SQL ambiguity. A regression test validates the qualified columns across all three queries.

Changes

Discovery Query Refactoring

Layer / File(s) Summary
Query builder extraction and access-filter unification
internal/catalog/discovery_repo.go, internal/catalog/discovery_repo_test.go
Three new helper functions (buildRatingThresholdQuery, buildUnplayedHighRatedQuery, buildForgottenFavoritesQuery) are added to build SQL and bind args. Each returns "" when access rules exclude all libraries. Public methods (ListByRatingThreshold, ListUnplayedHighRated, ListForgottenFavorites) are refactored to call their respective helpers and early-return empty slices when query == "". Access-filter and empty-list checks move into the helpers, and test helpers are removed from the test file.
Column qualification and regression test
internal/catalog/discovery_repo.go, internal/catalog/discovery_repo_test.go
SELECT projections in all three discovery queries change from itemColumns to qualifiedItemColumns("mi") to qualify the content_id column and avoid SQL ambiguity under library joins. New regression test TestDiscoveryQueries_QualifyContentIDUnderLibraryJoin verifies that all three query builders produce mi.content_id instead of unqualified content_id.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and specifically describes the main fix: qualifying content_id in discovery queries to resolve ambiguity under library join conditions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Quick104 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Closing this in favor of #88.

#88 keeps the useful discovery query-builder extraction and column qualification from this PR, then extends the fix to use the existing buildLibraryScopeJoin semi-join shape so library-scoped discovery queries avoid both content_id ambiguity and multi-library row fanout. It also includes the missing late-orphan predicate fix described here.

@Quick104 Quick104 closed this Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants