Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

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

coderdailyonebd7aa4011914
diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go
index a127bc17d..2e3eedb43 100644
--- a/internal/application/repository/wiki_page.go
+++ b/internal/application/repository/wiki_page.go
@@ -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))
}
}
diff --git a/internal/application/repository/wiki_page_test.go b/internal/application/repository/wiki_page_test.go
index 66d82bb5a..19bc8f621 100644
--- a/internal/application/repository/wiki_page_test.go
+++ b/internal/application/repository/wiki_page_test.go
@@ -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.