From e40d54fd40c8295af4a6b0fd7a954722f7434838 Mon Sep 17 00:00:00 2001 From: ump45nose <52391318+ump45nose@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:35:05 +0800 Subject: [PATCH] fix(search): apply the artist library scope to every search path - #6340 (#6415) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(persistence): apply the artist library scope to every search path - #6340 Signed-off-by: yuwk <1729065730@qq.com> * test(persistence): expect a scoped artist search to find the artist The library-scope specs only showed that searching another library returns nothing. Also search with library_id = [lib2.ID] and expect the artist that belongs to that library. Signed-off-by: Vincent <52391318+ump45nose@users.noreply.github.com> Co-authored-by: ump45nose * refactor(search): streamline artist search configuration and improve library filtering logic --------- Signed-off-by: yuwk <1729065730@qq.com> Signed-off-by: Vincent <52391318+ump45nose@users.noreply.github.com> Co-authored-by: Deluan Quintão Co-authored-by: ump45nose --- persistence/artist_repository.go | 9 +++++--- persistence/artist_repository_test.go | 30 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index b90a48c48..e1e332248 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -613,7 +613,7 @@ func (r *artistRepository) RefreshStats(ctx context.Context, allArtists bool) (i return totalRowsAffected, nil } -// searchCfg builds the per-search config. scope is the set of library IDs the rowid Phase 1 must +// searchCfg builds the per-search config. scope is the set of library IDs the search must // restrict artists to, or nil to skip the filter (fast-path). See [artistRepository.searchScope]. func (r *artistRepository) searchCfg(scope []int) searchConfig { return searchConfig{ @@ -655,13 +655,16 @@ func (r *artistRepository) Search(ctx context.Context, q string, options ...mode opts = options[0] } // Artists have no library_id column, so the library_id filter callers pass (same as albums/songs) - // can't be applied directly: consume it and realize it as a join-free Phase-1 scope (searchCfg). + // can't be applied directly: consume it and realize it as a join-free scope (searchCfg). scope := r.searchScope(ctx, opts.Filters) if isLibraryIDFilter(opts.Filters) { opts.Filters = nil } + cfg := r.searchCfg(scope) + // Also scope the full SELECT, like albums/songs do: the LIKE and MBID paths have no Phase 1. + sq := cfg.LibraryFilter(r.selectArtist(ctx, opts)) var res dbArtists - err := r.doSearch(ctx, r.selectArtist(ctx, opts), q, &res, r.searchCfg(scope), opts) + err := r.doSearch(ctx, sq, q, &res, cfg, opts) if err != nil { return nil, fmt.Errorf("searching artist %q: %w", q, err) } diff --git a/persistence/artist_repository_test.go b/persistence/artist_repository_test.go index 914c2f89a..67da1d41e 100644 --- a/persistence/artist_repository_test.go +++ b/persistence/artist_repository_test.go @@ -830,6 +830,36 @@ var _ = Describe("ArtistRepository", func() { _, _ = raw.executeSQL(ctx, squirrel.Delete(raw.tableName).Where(squirrel.Eq{"id": inaccessibleArtist.ID})) } }) + + DescribeTable("narrows artist search to the requested library", + func(backend string, artist model.Artist, query string) { + DeferCleanup(configtest.SetupConfig()) + if backend != "" { + conf.Server.Search.Backend = backend + } + Expect(createArtistWithLibrary(ctx, repo, &artist, lib2.ID)).To(Succeed()) + DeferCleanup(func() { + raw := repo.(*artistRepository) + _, _ = raw.executeSQL(ctx, squirrel.Delete(raw.tableName).Where(squirrel.Eq{"id": artist.ID})) + }) + + search := func(filters squirrel.Sqlizer) model.Artists { + results, err := repo.Search(ctx, query, model.QueryOptions{Max: 100, Filters: filters}) + Expect(err).ToNot(HaveOccurred()) + return results + } + Expect(search(nil)).To(HaveLen(1)) + Expect(search(squirrel.Eq{"library_id": []int{1}})).To(BeEmpty()) + Expect(search(squirrel.Eq{"library_id": []int{lib2.ID}})).To(HaveLen(1)) + }, + Entry("legacy (LIKE) backend", "legacy", + model.Artist{ID: "legacy-search-lib2-artist", Name: "Folder Scope Zeta Artist"}, "Folder Scope"), + Entry("CJK LIKE fallback", "", + model.Artist{ID: "cjk-search-lib2-artist", Name: "文件夹范围测试艺术家"}, "文件夹"), + Entry("MBID match", "", + createTestArtistWithMBID("mbid-search-lib2-artist", "Folder Scope MBID Artist", "3f2b1e58-8a4c-4a1b-9c3d-7e2f5a1b9c04"), + "3f2b1e58-8a4c-4a1b-9c3d-7e2f5a1b9c04"), + ) }) Context("Empty Query (sync pagination)", func() {