feat: 支持删除文档类型标签
This commit is contained in:
@@ -3,15 +3,16 @@
|
||||
-- a "未分类" tag, but this may affect entries that were intentionally tagged.
|
||||
|
||||
-- WARNING: This rollback is destructive and should only be used if absolutely necessary.
|
||||
-- It will set tag_id to empty string for all FAQ entries that reference "未分类" tags.
|
||||
-- It will set tag_id to empty string for all chunks, knowledges, and embeddings that reference "未分类" tags.
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
kb_record RECORD;
|
||||
untagged_tag_id VARCHAR(36);
|
||||
updated_chunks INT;
|
||||
updated_knowledges INT;
|
||||
BEGIN
|
||||
RAISE NOTICE '[Migration 000008 Rollback] WARNING: This rollback will clear tag_id for all FAQ entries referencing "未分类" tags';
|
||||
RAISE NOTICE '[Migration 000008 Rollback] WARNING: This rollback will clear tag_id for all entries referencing "未分类" tags';
|
||||
|
||||
-- Find all "未分类" tags
|
||||
FOR kb_record IN
|
||||
@@ -21,16 +22,25 @@ BEGIN
|
||||
LOOP
|
||||
untagged_tag_id := kb_record.id;
|
||||
|
||||
-- Clear tag_id for chunks referencing this tag
|
||||
-- Clear tag_id for chunks referencing this tag (both faq and document types)
|
||||
UPDATE chunks
|
||||
SET tag_id = '', updated_at = NOW()
|
||||
WHERE tag_id = untagged_tag_id
|
||||
AND chunk_type = 'faq';
|
||||
AND chunk_type IN ('faq', 'document');
|
||||
|
||||
GET DIAGNOSTICS updated_chunks = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Cleared tag_id for % chunks referencing tag %',
|
||||
updated_chunks, untagged_tag_id;
|
||||
|
||||
-- Clear tag_id for knowledges referencing this tag
|
||||
UPDATE knowledges
|
||||
SET tag_id = '', updated_at = NOW()
|
||||
WHERE tag_id = untagged_tag_id;
|
||||
|
||||
GET DIAGNOSTICS updated_knowledges = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Cleared tag_id for % knowledges referencing tag %',
|
||||
updated_knowledges, untagged_tag_id;
|
||||
|
||||
-- Clear tag_id in embeddings if column exists
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
-- Migration: Create "未分类" tag for each knowledge base that has untagged FAQ entries
|
||||
-- and update those entries to reference the new tag
|
||||
-- Migration: Create "未分类" tag for each knowledge base that has untagged entries
|
||||
-- and update chunks, knowledges, and embeddings to reference the new tag
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
kb_record RECORD;
|
||||
new_tag_id VARCHAR(36);
|
||||
updated_chunks INT;
|
||||
updated_knowledges INT;
|
||||
updated_embeddings INT;
|
||||
BEGIN
|
||||
-- Find all knowledge bases that have FAQ chunks with empty or NULL tag_id
|
||||
-- Find all knowledge bases that have untagged chunks or knowledges
|
||||
FOR kb_record IN
|
||||
SELECT DISTINCT c.tenant_id, c.knowledge_base_id
|
||||
FROM chunks c
|
||||
WHERE c.chunk_type = 'faq'
|
||||
AND (c.tag_id = '' OR c.tag_id IS NULL)
|
||||
SELECT DISTINCT tenant_id, knowledge_base_id
|
||||
FROM (
|
||||
-- Untagged chunks (FAQ or document)
|
||||
SELECT c.tenant_id, c.knowledge_base_id
|
||||
FROM chunks c
|
||||
WHERE c.chunk_type IN ('faq', 'document')
|
||||
AND (c.tag_id = '' OR c.tag_id IS NULL)
|
||||
UNION
|
||||
-- Untagged knowledges (documents)
|
||||
SELECT k.tenant_id, k.knowledge_base_id
|
||||
FROM knowledges k
|
||||
WHERE k.deleted_at IS NULL
|
||||
AND (k.tag_id = '' OR k.tag_id IS NULL)
|
||||
) AS untagged
|
||||
LOOP
|
||||
-- Check if "未分类" tag already exists for this knowledge base
|
||||
SELECT id INTO new_tag_id
|
||||
@@ -35,18 +46,30 @@ BEGIN
|
||||
new_tag_id, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
END IF;
|
||||
|
||||
-- Update chunks with empty tag_id to use the new tag
|
||||
-- Update chunks with empty tag_id (both faq and document types)
|
||||
UPDATE chunks
|
||||
SET tag_id = new_tag_id, updated_at = NOW()
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND chunk_type = 'faq'
|
||||
AND chunk_type IN ('faq', 'document')
|
||||
AND (tag_id = '' OR tag_id IS NULL);
|
||||
|
||||
GET DIAGNOSTICS updated_chunks = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008] Updated % chunks for tenant_id: %, kb_id: %',
|
||||
updated_chunks, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
|
||||
-- Update knowledges with empty tag_id (documents)
|
||||
UPDATE knowledges
|
||||
SET tag_id = new_tag_id, updated_at = NOW()
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND deleted_at IS NULL
|
||||
AND (tag_id = '' OR tag_id IS NULL);
|
||||
|
||||
GET DIAGNOSTICS updated_knowledges = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008] Updated % knowledges for tenant_id: %, kb_id: %',
|
||||
updated_knowledges, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
|
||||
-- Update embeddings with empty tag_id (if embeddings table exists and has tag_id column)
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
@@ -60,7 +83,7 @@ BEGIN
|
||||
SELECT id FROM chunks
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND chunk_type = 'faq'
|
||||
AND chunk_type IN ('faq', 'document')
|
||||
);
|
||||
|
||||
GET DIAGNOSTICS updated_embeddings = ROW_COUNT;
|
||||
@@ -69,5 +92,5 @@ BEGIN
|
||||
END IF;
|
||||
END LOOP;
|
||||
|
||||
RAISE NOTICE '[Migration 000008] Completed migration of untagged FAQ entries';
|
||||
RAISE NOTICE '[Migration 000008] Completed migration of untagged entries';
|
||||
END $$;
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
-- Remove last_faq_import_result column from knowledge table
|
||||
|
||||
ALTER TABLE knowledges DROP COLUMN last_faq_import_result;
|
||||
ALTER TABLE knowledges DROP COLUMN IF EXISTS last_faq_import_result;
|
||||
@@ -1,4 +1,4 @@
|
||||
-- Add last_faq_import_result column to knowledge table
|
||||
-- This field stores the latest FAQ import result for FAQ type knowledge
|
||||
|
||||
ALTER TABLE knowledges ADD COLUMN last_faq_import_result JSON DEFAULT NULL;
|
||||
ALTER TABLE knowledges ADD COLUMN IF NOT EXISTS last_faq_import_result JSON DEFAULT NULL;
|
||||
Reference in New Issue
Block a user