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): support orphan count on sqlite

wolfkillf195e0494cdd
diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go
index b59979f46..9e85f3084 100644
--- a/internal/application/repository/wiki_page.go
+++ b/internal/application/repository/wiki_page.go
@@ -36,6 +36,13 @@ func (r *wikiPageRepository) wikiCategoryRankOrder() string {
return "CASE WHEN COALESCE(jsonb_array_length(category_path), 0) > 0 THEN 0 ELSE 1 END ASC"
}
+func (r *wikiPageRepository) wikiEmptyInLinksPredicate() string {
+ if r.db != nil && r.db.Dialector != nil && r.db.Dialector.Name() == "sqlite" {
+ return "(in_links IS NULL OR json_array_length(in_links) = 0)"
+ }
+ return "(in_links IS NULL OR in_links = '[]'::JSONB)"
+}
+
// Create inserts a new wiki page record
func (r *wikiPageRepository) Create(ctx context.Context, page *types.WikiPage) error {
return r.db.WithContext(ctx).Create(page).Error
@@ -1059,7 +1066,7 @@ func (r *wikiPageRepository) CountOrphans(ctx context.Context, kbID string) (int
if err := r.db.WithContext(ctx).
Model(&types.WikiPage{}).
Where("knowledge_base_id = ?", kbID).
- Where("(in_links IS NULL OR in_links = '[]'::JSONB)").
+ Where(r.wikiEmptyInLinksPredicate()).
// Exclude index and log pages as they are naturally root pages
Where("page_type NOT IN ?", []string{types.WikiPageTypeIndex, types.WikiPageTypeLog}).
Count(&count).Error; err != nil {
diff --git a/internal/application/repository/wiki_page_test.go b/internal/application/repository/wiki_page_test.go
index 71be13592..2c3e90d7b 100644
--- a/internal/application/repository/wiki_page_test.go
+++ b/internal/application/repository/wiki_page_test.go
@@ -332,3 +332,28 @@ func TestListByTypeLight_ClampsLimit(t *testing.T) {
require.NoError(t, err)
assert.LessOrEqual(t, len(clampedEntries), 200)
}
+
+func TestCountOrphans_SQLiteCountsEmptyInLinks(t *testing.T) {
+ db := setupWikiPagesTestDB(t)
+ repo := NewWikiPageRepository(db)
+ ctx := context.Background()
+
+ orphan := makeWikiPage("kb-orphans", "entity/orphan", types.WikiPageTypeEntity, types.WikiPageStatusPublished)
+ orphan.InLinks = types.StringArray{}
+ linked := makeWikiPage("kb-orphans", "entity/linked", types.WikiPageTypeEntity, types.WikiPageStatusPublished)
+ linked.InLinks = types.StringArray{"entity/source"}
+ indexPage := makeWikiPage("kb-orphans", "index", types.WikiPageTypeIndex, types.WikiPageStatusPublished)
+ indexPage.InLinks = types.StringArray{}
+ logPage := makeWikiPage("kb-orphans", "log", types.WikiPageTypeLog, types.WikiPageStatusPublished)
+ logPage.InLinks = types.StringArray{}
+ otherKB := makeWikiPage("kb-other", "entity/other", types.WikiPageTypeEntity, types.WikiPageStatusPublished)
+ otherKB.InLinks = types.StringArray{}
+
+ for _, p := range []*types.WikiPage{orphan, linked, indexPage, logPage, otherKB} {
+ require.NoError(t, repo.Create(ctx, p))
+ }
+
+ got, err := repo.CountOrphans(ctx, "kb-orphans")
+ require.NoError(t, err)
+ assert.Equal(t, int64(1), got)
+}