refactor(wiki): unify category cleaning and push directory listing to SQL
Centralize the wiki category-path normalization (separator/quote cleanup, type-label stripping, dedup, depth cap) into a single types helper shared by the service, repository, and taxonomy layers so stored and queried paths are always cleaned identically and cannot drift. Flatten category_path into fixed category_l1/l2/l3 columns (kept in sync on write) and rewrite ListDistinctCategoryPathsByType to group, count, and paginate directly in cross-dialect SQL, returning the page of child folders plus the total distinct count. This removes the prior full-column scan and in-memory pagination (whose maxPaths cap was effectively dead) and makes Total and later pages correct. Add the level columns + composite index to the wiki migrations and the repo test schema, and cover the SQL grouping/counting/pagination with a new test.
This commit is contained in:
@@ -27,6 +27,9 @@ CREATE TABLE IF NOT EXISTS wiki_pages (
|
||||
wiki_path VARCHAR(1024) NOT NULL DEFAULT '',
|
||||
depth INT NOT NULL DEFAULT 0,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
category_l1 VARCHAR(255) NOT NULL DEFAULT '',
|
||||
category_l2 VARCHAR(255) NOT NULL DEFAULT '',
|
||||
category_l3 VARCHAR(255) NOT NULL DEFAULT '',
|
||||
source_refs JSONB DEFAULT '[]'::JSONB,
|
||||
chunk_refs JSONB DEFAULT '[]'::JSONB,
|
||||
in_links JSONB DEFAULT '[]'::JSONB,
|
||||
@@ -56,6 +59,9 @@ CREATE INDEX IF NOT EXISTS idx_wiki_pages_parent_slug
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_tree
|
||||
ON wiki_pages (knowledge_base_id, page_type, wiki_path, sort_order, title);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_category_levels
|
||||
ON wiki_pages (knowledge_base_id, page_type, category_l1, category_l2, category_l3);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_tenant_id
|
||||
ON wiki_pages (tenant_id);
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
-- Migration: 000061_wiki_page_hierarchy (rollback)
|
||||
|
||||
DROP INDEX IF EXISTS idx_wiki_pages_category_levels;
|
||||
DROP INDEX IF EXISTS idx_wiki_pages_tree;
|
||||
DROP INDEX IF EXISTS idx_wiki_pages_parent_slug;
|
||||
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l3;
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l2;
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l1;
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS sort_order;
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS depth;
|
||||
ALTER TABLE wiki_pages DROP COLUMN IF EXISTS wiki_path;
|
||||
|
||||
@@ -8,6 +8,11 @@ ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_path JSONB DEFAULT '[]'
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS wiki_path VARCHAR(1024) NOT NULL DEFAULT '';
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS depth INT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS sort_order INT NOT NULL DEFAULT 0;
|
||||
-- Flat mirrors of category_path[0..2] for cheap, cross-dialect directory
|
||||
-- grouping/pagination (kept in sync by the application on write).
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l1 VARCHAR(255) NOT NULL DEFAULT '';
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l2 VARCHAR(255) NOT NULL DEFAULT '';
|
||||
ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l3 VARCHAR(255) NOT NULL DEFAULT '';
|
||||
|
||||
UPDATE wiki_pages
|
||||
SET
|
||||
@@ -20,10 +25,21 @@ SET
|
||||
END
|
||||
WHERE wiki_path = '' OR wiki_path IS NULL OR category_path IS NULL OR depth IS NULL;
|
||||
|
||||
-- Backfill the flat level columns from the JSON category_path.
|
||||
UPDATE wiki_pages
|
||||
SET
|
||||
category_l1 = COALESCE(category_path->>0, ''),
|
||||
category_l2 = COALESCE(category_path->>1, ''),
|
||||
category_l3 = COALESCE(category_path->>2, '')
|
||||
WHERE category_path IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_parent_slug
|
||||
ON wiki_pages (knowledge_base_id, parent_slug);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_tree
|
||||
ON wiki_pages (knowledge_base_id, page_type, wiki_path, sort_order, title);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_wiki_pages_category_levels
|
||||
ON wiki_pages (knowledge_base_id, page_type, category_l1, category_l2, category_l3);
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000061] wiki page hierarchy schema applied successfully'; END $$;
|
||||
|
||||
Reference in New Issue
Block a user