fix(wiki): make the category_path directory filter match on SQLite

StringArray is stored as a BLOB on SQLite, so the TEXT equality used by the directory listing never matched and every wiki folder listed empty on the SQLite build. Compare the text form there.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFSMrLZZ3oq1dKmJFAofz6
main
coderdailyone 2026-09-07 08:25:51 +00:00 committed by lyingbug
parent bd6c4a9b96
commit bd7aa40119
2 changed files with 62 additions and 2 deletions

View File

@ -30,6 +30,13 @@ func NewWikiPageRepository(db *gorm.DB) interfaces.WikiPageRepository {
return &wikiPageRepository{db: db}
}
func (r *wikiPageRepository) wikiDialect() string {
if r.db == nil || r.db.Dialector == nil {
return ""
}
return r.db.Name()
}
func (r *wikiPageRepository) wikiCategoryRankOrder() string {
if r.db != nil && r.db.Dialector != nil && r.db.Dialector.Name() == "sqlite" {
return "CASE WHEN COALESCE(json_array_length(category_path), 0) > 0 THEN 0 ELSE 1 END ASC"
@ -378,9 +385,15 @@ func (r *wikiPageRepository) List(ctx context.Context, req *types.WikiPageListRe
}
if wantPath := types.TrimWikiFolderSegments(req.CategoryPath); len(wantPath) > 0 {
if encoded, err := json.Marshal([]string(wantPath)); err == nil {
if r.db.Dialector != nil && r.db.Dialector.Name() == "postgres" {
switch r.wikiDialect() {
case "postgres":
query = query.Where("category_path::jsonb = ?::jsonb", string(encoded))
} else {
case "sqlite":
// StringArray.Value yields []byte, which SQLite stores as a BLOB;
// a BLOB never compares equal to a TEXT bind, so compare the
// text form instead.
query = query.Where("CAST(category_path AS TEXT) = ?", string(encoded))
default:
query = query.Where("category_path = ?", string(encoded))
}
}

View File

@ -177,6 +177,53 @@ func TestList_WikiPathSortReturnsCategorizedPagesFirst(t *testing.T) {
assert.Equal(t, "entity/001-root", got[2].Slug)
}
// TestList_CategoryPathFilterMatchesOnSQLite protects the directory view on the
// SQLite build: the sidebar loads a folder's pages with category_path +
// category_depth, and StringArray is stored as a BLOB there, so a plain
// equality against a TEXT bind never matched and every folder listed empty.
func TestList_CategoryPathFilterMatchesOnSQLite(t *testing.T) {
db := setupWikiPagesTestDB(t)
repo := NewWikiPageRepository(db)
ctx := context.Background()
entity, published := types.WikiPageTypeEntity, types.WikiPageStatusPublished
pages := []*types.WikiPage{
makeCategorizedWikiPage("kb-c", "entity/in-ai", entity, published, "AI"),
makeCategorizedWikiPage("kb-c", "entity/in-ai-llm", entity, published, "AI", "LLM"),
makeCategorizedWikiPage("kb-c", "entity/in-people", entity, published, "人物"),
makeWikiPage("kb-c", "entity/root", entity, published),
}
for _, p := range pages {
require.NoError(t, repo.Create(ctx, p))
}
depth := 1
got, total, err := repo.List(ctx, &types.WikiPageListRequest{
KnowledgeBaseID: "kb-c",
PageType: types.WikiPageTypeEntity,
CategoryPath: types.StringArray{"AI"},
CategoryDepth: &depth,
Page: 1,
PageSize: 10,
})
require.NoError(t, err)
assert.Equal(t, int64(1), total)
require.Len(t, got, 1)
assert.Equal(t, "entity/in-ai", got[0].Slug)
got, total, err = repo.List(ctx, &types.WikiPageListRequest{
KnowledgeBaseID: "kb-c",
PageType: types.WikiPageTypeEntity,
CategoryPath: types.StringArray{"人物"},
Page: 1,
PageSize: 10,
})
require.NoError(t, err)
assert.Equal(t, int64(1), total)
require.Len(t, got, 1)
assert.Equal(t, "entity/in-people", got[0].Slug)
}
// TestFolderTree_CRUDAndChildListing exercises the wiki_folders repository:
// child listing ordered by sort_order/name, find-by-name, page counting under
// a folder, and that ListDistinctCategoryPaths reflects the folder paths.