diff --git a/migrations/versioned/000036_wiki_pages.down.sql b/migrations/versioned/000036_wiki_pages.down.sql deleted file mode 100644 index a69575de..00000000 --- a/migrations/versioned/000036_wiki_pages.down.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Migration: 000036_wiki_pages (rollback) --- Description: Remove wiki_pages table and wiki_config column from knowledge_bases. -DO $$ BEGIN RAISE NOTICE '[Migration 000036] Removing wiki_pages table and wiki_config column'; END $$; - -DROP TABLE IF EXISTS wiki_pages; - -ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS wiki_config; - -DO $$ BEGIN RAISE NOTICE '[Migration 000036] wiki_pages table and wiki_config column removed successfully'; END $$; diff --git a/migrations/versioned/000036_wiki_pages.up.sql b/migrations/versioned/000036_wiki_pages.up.sql deleted file mode 100644 index 91e7a179..00000000 --- a/migrations/versioned/000036_wiki_pages.up.sql +++ /dev/null @@ -1,59 +0,0 @@ --- Migration: 000036_wiki_pages --- Description: Add wiki_pages table and wiki_config column to knowledge_bases. --- Wiki pages are LLM-generated, interlinked markdown documents that form a persistent wiki. -DO $$ BEGIN RAISE NOTICE '[Migration 000036] Creating wiki_pages table and adding wiki_config column'; END $$; - --- Add wiki_config column to knowledge_bases -ALTER TABLE knowledge_bases ADD COLUMN IF NOT EXISTS wiki_config JSONB; - -COMMENT ON COLUMN knowledge_bases.wiki_config IS 'Wiki configuration: {"auto_ingest": bool, "synthesis_model_id": string, "wiki_language": string, "max_pages_per_ingest": int}'; - --- Create wiki_pages table -CREATE TABLE IF NOT EXISTS wiki_pages ( - id VARCHAR(36) PRIMARY KEY, - tenant_id BIGINT NOT NULL, - knowledge_base_id VARCHAR(36) NOT NULL, - slug VARCHAR(255) NOT NULL, - title VARCHAR(512) NOT NULL DEFAULT '', - page_type VARCHAR(32) NOT NULL DEFAULT 'summary', - status VARCHAR(32) NOT NULL DEFAULT 'published', - content TEXT NOT NULL DEFAULT '', - summary TEXT NOT NULL DEFAULT '', - source_refs JSONB DEFAULT '[]'::JSONB, - chunk_refs JSONB DEFAULT '[]'::JSONB, - in_links JSONB DEFAULT '[]'::JSONB, - out_links JSONB DEFAULT '[]'::JSONB, - page_metadata JSONB DEFAULT '{}'::JSONB, - aliases JSONB DEFAULT '[]'::JSONB, - version INT NOT NULL DEFAULT 1, - created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), - updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), - deleted_at TIMESTAMP WITH TIME ZONE -); - --- Unique constraint: slug must be unique within a knowledge base (for non-deleted pages) -CREATE UNIQUE INDEX IF NOT EXISTS idx_wiki_pages_kb_slug - ON wiki_pages (knowledge_base_id, slug) - WHERE deleted_at IS NULL; - --- Index for listing pages by knowledge base -CREATE INDEX IF NOT EXISTS idx_wiki_pages_kb_id - ON wiki_pages (knowledge_base_id); - --- Index for filtering by page type -CREATE INDEX IF NOT EXISTS idx_wiki_pages_page_type - ON wiki_pages (knowledge_base_id, page_type); - --- Index for tenant isolation -CREATE INDEX IF NOT EXISTS idx_wiki_pages_tenant_id - ON wiki_pages (tenant_id); - --- Index for soft delete queries -CREATE INDEX IF NOT EXISTS idx_wiki_pages_deleted_at - ON wiki_pages (deleted_at); - --- Full-text search index on title and content -CREATE INDEX IF NOT EXISTS idx_wiki_pages_fulltext - ON wiki_pages USING GIN (to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(content, ''))); - -DO $$ BEGIN RAISE NOTICE '[Migration 000036] wiki_pages table and wiki_config column created successfully'; END $$; diff --git a/migrations/versioned/000037_wiki_and_indexing.down.sql b/migrations/versioned/000037_wiki_and_indexing.down.sql new file mode 100644 index 00000000..dbe111f4 --- /dev/null +++ b/migrations/versioned/000037_wiki_and_indexing.down.sql @@ -0,0 +1,13 @@ +-- Migration: 000037_wiki_and_indexing (rollback) +-- Description: Reverse the wiki + indexing schema in the opposite order of the up migration. +DO $$ BEGIN RAISE NOTICE '[Migration 000037 DOWN] Reverting wiki + indexing schema'; END $$; + +ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS indexing_strategy; + +DROP TABLE IF EXISTS wiki_page_issues; + +DROP TABLE IF EXISTS wiki_pages; + +ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS wiki_config; + +DO $$ BEGIN RAISE NOTICE '[Migration 000037 DOWN] wiki + indexing schema reverted successfully'; END $$; diff --git a/migrations/versioned/000037_wiki_and_indexing.up.sql b/migrations/versioned/000037_wiki_and_indexing.up.sql new file mode 100644 index 00000000..3b1dfecb --- /dev/null +++ b/migrations/versioned/000037_wiki_and_indexing.up.sql @@ -0,0 +1,100 @@ +-- Migration: 000037_wiki_and_indexing +-- Description: Wiki feature schema (wiki_pages table + wiki_config column + wiki_page_issues table) +-- and the indexing_strategy column on knowledge_bases. +DO $$ BEGIN RAISE NOTICE '[Migration 000037] Applying wiki + indexing schema'; END $$; + +-- --------------------------------------------------------------------------- +-- 1) wiki_pages table and wiki_config column +-- Wiki pages are LLM-generated, interlinked markdown documents that form a +-- persistent wiki for a knowledge base. +-- --------------------------------------------------------------------------- +ALTER TABLE knowledge_bases ADD COLUMN IF NOT EXISTS wiki_config JSONB; + +COMMENT ON COLUMN knowledge_bases.wiki_config IS 'Wiki configuration: {"auto_ingest": bool, "synthesis_model_id": string, "wiki_language": string, "max_pages_per_ingest": int}'; + +CREATE TABLE IF NOT EXISTS wiki_pages ( + id VARCHAR(36) PRIMARY KEY, + tenant_id BIGINT NOT NULL, + knowledge_base_id VARCHAR(36) NOT NULL, + slug VARCHAR(255) NOT NULL, + title VARCHAR(512) NOT NULL DEFAULT '', + page_type VARCHAR(32) NOT NULL DEFAULT 'summary', + status VARCHAR(32) NOT NULL DEFAULT 'published', + content TEXT NOT NULL DEFAULT '', + summary TEXT NOT NULL DEFAULT '', + source_refs JSONB DEFAULT '[]'::JSONB, + chunk_refs JSONB DEFAULT '[]'::JSONB, + in_links JSONB DEFAULT '[]'::JSONB, + out_links JSONB DEFAULT '[]'::JSONB, + page_metadata JSONB DEFAULT '{}'::JSONB, + aliases JSONB DEFAULT '[]'::JSONB, + version INT NOT NULL DEFAULT 1, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + deleted_at TIMESTAMP WITH TIME ZONE +); + +-- slug must be unique within a knowledge base (for non-deleted pages) +CREATE UNIQUE INDEX IF NOT EXISTS idx_wiki_pages_kb_slug + ON wiki_pages (knowledge_base_id, slug) + WHERE deleted_at IS NULL; + +CREATE INDEX IF NOT EXISTS idx_wiki_pages_kb_id + ON wiki_pages (knowledge_base_id); + +CREATE INDEX IF NOT EXISTS idx_wiki_pages_page_type + ON wiki_pages (knowledge_base_id, page_type); + +CREATE INDEX IF NOT EXISTS idx_wiki_pages_tenant_id + ON wiki_pages (tenant_id); + +CREATE INDEX IF NOT EXISTS idx_wiki_pages_deleted_at + ON wiki_pages (deleted_at); + +CREATE INDEX IF NOT EXISTS idx_wiki_pages_fulltext + ON wiki_pages USING GIN (to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(content, ''))); + +-- --------------------------------------------------------------------------- +-- 2) wiki_page_issues table +-- Reports issues against generated wiki pages (LLM-flagged or user-flagged). +-- --------------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS wiki_page_issues ( + id VARCHAR(36) PRIMARY KEY, + tenant_id BIGINT NOT NULL, + knowledge_base_id VARCHAR(36) NOT NULL, + slug VARCHAR(255) NOT NULL, + issue_type VARCHAR(50) NOT NULL, + description TEXT NOT NULL, + suspected_knowledge_ids JSONB, + status VARCHAR(20) DEFAULT 'pending' NOT NULL, + reported_by VARCHAR(100) NOT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP WITH TIME ZONE +); + +CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_tenant_id ON wiki_page_issues(tenant_id); +CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_knowledge_base_id ON wiki_page_issues(knowledge_base_id); +CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_slug ON wiki_page_issues(slug); +CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_status ON wiki_page_issues(status); + +-- --------------------------------------------------------------------------- +-- 3) indexing_strategy column on knowledge_bases +-- Controls which indexing pipelines are active (vector, keyword, wiki, graph). +-- Backfill: existing rows get vector+keyword=true (legacy default behavior); +-- wiki_enabled / graph_enabled stay false until explicitly enabled. +-- --------------------------------------------------------------------------- +ALTER TABLE knowledge_bases ADD COLUMN IF NOT EXISTS indexing_strategy JSONB; + +COMMENT ON COLUMN knowledge_bases.indexing_strategy IS 'Indexing pipelines strategy: {"vector_enabled": bool, "keyword_enabled": bool, "wiki_enabled": bool, "graph_enabled": bool}'; + +UPDATE knowledge_bases +SET indexing_strategy = jsonb_build_object( + 'vector_enabled', TRUE, + 'keyword_enabled', TRUE, + 'wiki_enabled', FALSE, + 'graph_enabled', FALSE +) +WHERE indexing_strategy IS NULL; + +DO $$ BEGIN RAISE NOTICE '[Migration 000037] wiki + indexing schema applied successfully'; END $$; diff --git a/migrations/versioned/000037_wiki_page_issues.down.sql b/migrations/versioned/000037_wiki_page_issues.down.sql deleted file mode 100644 index d347dde0..00000000 --- a/migrations/versioned/000037_wiki_page_issues.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS wiki_page_issues; diff --git a/migrations/versioned/000037_wiki_page_issues.up.sql b/migrations/versioned/000037_wiki_page_issues.up.sql deleted file mode 100644 index e9fed1f9..00000000 --- a/migrations/versioned/000037_wiki_page_issues.up.sql +++ /dev/null @@ -1,19 +0,0 @@ -CREATE TABLE IF NOT EXISTS wiki_page_issues ( - id VARCHAR(36) PRIMARY KEY, - tenant_id BIGINT NOT NULL, - knowledge_base_id VARCHAR(36) NOT NULL, - slug VARCHAR(255) NOT NULL, - issue_type VARCHAR(50) NOT NULL, - description TEXT NOT NULL, - suspected_knowledge_ids JSONB, - status VARCHAR(20) DEFAULT 'pending' NOT NULL, - reported_by VARCHAR(100) NOT NULL, - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP WITH TIME ZONE -); - -CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_tenant_id ON wiki_page_issues(tenant_id); -CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_knowledge_base_id ON wiki_page_issues(knowledge_base_id); -CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_slug ON wiki_page_issues(slug); -CREATE INDEX IF NOT EXISTS idx_wiki_page_issues_status ON wiki_page_issues(status); diff --git a/migrations/versioned/000038_add_indexing_strategy.down.sql b/migrations/versioned/000038_add_indexing_strategy.down.sql deleted file mode 100644 index dc9c2215..00000000 --- a/migrations/versioned/000038_add_indexing_strategy.down.sql +++ /dev/null @@ -1,7 +0,0 @@ --- Migration: 000038_add_indexing_strategy (down) --- Description: Remove indexing_strategy column from knowledge_bases. -DO $$ BEGIN RAISE NOTICE '[Migration 000038] Dropping indexing_strategy column from knowledge_bases'; END $$; - -ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS indexing_strategy; - -DO $$ BEGIN RAISE NOTICE '[Migration 000038] indexing_strategy column dropped successfully'; END $$; diff --git a/migrations/versioned/000038_add_indexing_strategy.up.sql b/migrations/versioned/000038_add_indexing_strategy.up.sql deleted file mode 100644 index 2bdcecce..00000000 --- a/migrations/versioned/000038_add_indexing_strategy.up.sql +++ /dev/null @@ -1,21 +0,0 @@ --- Migration: 000038_add_indexing_strategy --- Description: Add indexing_strategy column to knowledge_bases table. --- Controls which indexing pipelines are active (vector, keyword, wiki, graph). -DO $$ BEGIN RAISE NOTICE '[Migration 000038] Adding indexing_strategy column to knowledge_bases'; END $$; - -ALTER TABLE knowledge_bases ADD COLUMN IF NOT EXISTS indexing_strategy JSONB; - -COMMENT ON COLUMN knowledge_bases.indexing_strategy IS 'Indexing pipelines strategy: {"vector_enabled": bool, "keyword_enabled": bool, "wiki_enabled": bool, "graph_enabled": bool}'; - --- Backfill: existing rows get vector+keyword=true (legacy default behavior), --- wiki_enabled synced from wiki_config.enabled, graph_enabled synced from extract_config.enabled. -UPDATE knowledge_bases -SET indexing_strategy = jsonb_build_object( - 'vector_enabled', TRUE, - 'keyword_enabled', TRUE, - 'wiki_enabled', FALSE, - 'graph_enabled', FALSE -) -WHERE indexing_strategy IS NULL; - -DO $$ BEGIN RAISE NOTICE '[Migration 000038] indexing_strategy column added and backfilled successfully'; END $$;