feat: Add agent configuration and cleanup scripts for database migrations
This commit is contained in:
@@ -16,11 +16,14 @@ CREATE TABLE IF NOT EXISTS tenants (
|
||||
business VARCHAR(255) NOT NULL,
|
||||
storage_quota BIGINT NOT NULL DEFAULT 10737418240, -- 默认10GB配额(Bytes)
|
||||
storage_used BIGINT NOT NULL DEFAULT 0, -- 已使用的存储空间(Bytes)
|
||||
agent_config JSONB DEFAULT 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
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN tenants.agent_config IS 'Tenant-level agent configuration in JSON format';
|
||||
|
||||
-- Set the starting value for tenants id sequence
|
||||
ALTER SEQUENCE tenants_id_seq RESTART WITH 10000;
|
||||
|
||||
@@ -122,11 +125,16 @@ CREATE TABLE IF NOT EXISTS sessions (
|
||||
rerank_threshold FLOAT NOT NULL DEFAULT 0.65,
|
||||
summary_model_id VARCHAR(64),
|
||||
summary_parameters JSONB NOT NULL DEFAULT '{}',
|
||||
agent_config JSONB DEFAULT NULL,
|
||||
context_config JSONB DEFAULT 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
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN sessions.agent_config IS 'Session-level agent configuration in JSON format';
|
||||
COMMENT ON COLUMN sessions.context_config IS 'LLM context management configuration (separate from message storage)';
|
||||
|
||||
-- Create Index for sessions
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_tenant_id ON sessions(tenant_id);
|
||||
|
||||
@@ -139,12 +147,15 @@ CREATE TABLE IF NOT EXISTS messages (
|
||||
role VARCHAR(50) NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
knowledge_references JSONB NOT NULL DEFAULT '[]',
|
||||
agent_steps JSONB DEFAULT NULL,
|
||||
is_completed BOOLEAN NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN messages.agent_steps IS 'Agent execution steps (reasoning process and tool calls)';
|
||||
|
||||
-- Create Index for messages
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_session_id ON messages(session_id);
|
||||
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
-- 增量迁移:添加 agent_config 字段(如果不存在)
|
||||
-- 创建时间:2025-11-03
|
||||
-- 目的:为旧版本数据库添加 agent_config 支持
|
||||
-- 注意:新数据库可能已经有这些字段(通过01-migrate-to-paradedb.sql),使用 IF NOT EXISTS 确保兼容
|
||||
|
||||
-- ============================================
|
||||
-- 1. 为 tenants 表添加 agent_config
|
||||
-- ============================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'tenants'
|
||||
AND column_name = 'agent_config'
|
||||
) THEN
|
||||
ALTER TABLE tenants
|
||||
ADD COLUMN agent_config JSONB DEFAULT NULL;
|
||||
|
||||
COMMENT ON COLUMN tenants.agent_config IS 'Tenant-level agent configuration in JSON format';
|
||||
|
||||
RAISE NOTICE 'Added agent_config column to tenants table';
|
||||
ELSE
|
||||
RAISE NOTICE 'agent_config column already exists in tenants table';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- ============================================
|
||||
-- 2. 为 sessions 表添加 agent_config
|
||||
-- ============================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'sessions'
|
||||
AND column_name = 'agent_config'
|
||||
) THEN
|
||||
ALTER TABLE sessions
|
||||
ADD COLUMN agent_config JSONB DEFAULT NULL;
|
||||
|
||||
COMMENT ON COLUMN sessions.agent_config IS 'Session-level agent configuration in JSON format';
|
||||
|
||||
RAISE NOTICE 'Added agent_config column to sessions table';
|
||||
ELSE
|
||||
RAISE NOTICE 'agent_config column already exists in sessions table';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- ============================================
|
||||
-- 3. 为 sessions 表添加 context_config
|
||||
-- ============================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'sessions'
|
||||
AND column_name = 'context_config'
|
||||
) THEN
|
||||
ALTER TABLE sessions
|
||||
ADD COLUMN context_config JSONB DEFAULT NULL;
|
||||
|
||||
COMMENT ON COLUMN sessions.context_config IS 'LLM context management configuration (separate from message storage)';
|
||||
|
||||
RAISE NOTICE 'Added context_config column to sessions table';
|
||||
ELSE
|
||||
RAISE NOTICE 'context_config column already exists in sessions table';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- ============================================
|
||||
-- 4. 为 messages 表添加 agent_steps
|
||||
-- ============================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'messages'
|
||||
AND column_name = 'agent_steps'
|
||||
) THEN
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN agent_steps JSONB DEFAULT NULL;
|
||||
|
||||
COMMENT ON COLUMN messages.agent_steps IS 'Agent execution steps (reasoning process and tool calls)';
|
||||
|
||||
RAISE NOTICE 'Added agent_steps column to messages table';
|
||||
ELSE
|
||||
RAISE NOTICE 'agent_steps column already exists in messages table';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- ============================================
|
||||
-- 5. 为 JSON 字段添加 GIN 索引(提高查询性能)
|
||||
-- ============================================
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
-- 为 tenants.agent_config 添加索引
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_indexes
|
||||
WHERE tablename = 'tenants'
|
||||
AND indexname = 'idx_tenants_agent_config'
|
||||
) THEN
|
||||
CREATE INDEX idx_tenants_agent_config ON tenants USING GIN (agent_config);
|
||||
RAISE NOTICE 'Created index idx_tenants_agent_config';
|
||||
ELSE
|
||||
RAISE NOTICE 'Index idx_tenants_agent_config already exists';
|
||||
END IF;
|
||||
|
||||
-- 为 sessions.agent_config 添加索引
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_indexes
|
||||
WHERE tablename = 'sessions'
|
||||
AND indexname = 'idx_sessions_agent_config'
|
||||
) THEN
|
||||
CREATE INDEX idx_sessions_agent_config ON sessions USING GIN (agent_config);
|
||||
RAISE NOTICE 'Created index idx_sessions_agent_config';
|
||||
ELSE
|
||||
RAISE NOTICE 'Index idx_sessions_agent_config already exists';
|
||||
END IF;
|
||||
|
||||
-- 为 sessions.context_config 添加索引
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_indexes
|
||||
WHERE tablename = 'sessions'
|
||||
AND indexname = 'idx_sessions_context_config'
|
||||
) THEN
|
||||
CREATE INDEX idx_sessions_context_config ON sessions USING GIN (context_config);
|
||||
RAISE NOTICE 'Created index idx_sessions_context_config';
|
||||
ELSE
|
||||
RAISE NOTICE 'Index idx_sessions_context_config already exists';
|
||||
END IF;
|
||||
|
||||
-- 为 messages.agent_steps 添加索引
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_indexes
|
||||
WHERE tablename = 'messages'
|
||||
AND indexname = 'idx_messages_agent_steps'
|
||||
) THEN
|
||||
CREATE INDEX idx_messages_agent_steps ON messages USING GIN (agent_steps);
|
||||
RAISE NOTICE 'Created index idx_messages_agent_steps';
|
||||
ELSE
|
||||
RAISE NOTICE 'Index idx_messages_agent_steps already exists';
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- ============================================
|
||||
-- 验证:显示所有表的字段
|
||||
-- ============================================
|
||||
|
||||
SELECT 'Verifying tenants table...' as status;
|
||||
SELECT column_name, data_type, is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'tenants' AND column_name = 'agent_config';
|
||||
|
||||
SELECT 'Verifying sessions table...' as status;
|
||||
SELECT column_name, data_type, is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'sessions' AND column_name IN ('agent_config', 'context_config');
|
||||
|
||||
SELECT 'Verifying messages table...' as status;
|
||||
SELECT column_name, data_type, is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'messages' AND column_name = 'agent_steps';
|
||||
|
||||
SELECT 'Migration completed successfully!' as status;
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
-- ============================================================================
|
||||
-- Migration: Cleanup Unreferenced Models (PostgreSQL/ParadeDB)
|
||||
-- Description: Remove models that are not referenced by any knowledge base
|
||||
-- Created: 2025-11-03
|
||||
-- ============================================================================
|
||||
|
||||
-- This script removes models from the models table that meet ALL of these conditions:
|
||||
-- 1. Not referenced by any knowledge_bases (embedding_model_id, summary_model_id, rerank_model_id, vlm_model_id)
|
||||
-- 2. Not referenced by any knowledges (embedding_model_id)
|
||||
-- 3. Not a default model (is_default = false)
|
||||
-- 4. Not a system model (tenant_id != 0)
|
||||
-- 5. Not soft-deleted (deleted_at IS NULL)
|
||||
|
||||
-- WARNING: This operation is irreversible. Make sure to backup your database before running.
|
||||
-- Recommended: Run the SELECT query first to review which models will be deleted.
|
||||
|
||||
-- ============================================================================
|
||||
-- Step 1: Review models that will be deleted (DRY RUN)
|
||||
-- ============================================================================
|
||||
SELECT
|
||||
m.id,
|
||||
m.tenant_id,
|
||||
m.name,
|
||||
m.type,
|
||||
m.source,
|
||||
m.is_default,
|
||||
m.status,
|
||||
m.created_at
|
||||
FROM models m
|
||||
WHERE m.deleted_at IS NULL
|
||||
AND m.is_default = FALSE
|
||||
AND m.id NOT IN (
|
||||
-- Models referenced by knowledge_bases
|
||||
SELECT DISTINCT embedding_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
UNION
|
||||
SELECT DISTINCT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
UNION
|
||||
SELECT DISTINCT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
UNION
|
||||
SELECT DISTINCT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
UNION
|
||||
-- Models referenced by knowledges
|
||||
SELECT DISTINCT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
)
|
||||
ORDER BY m.created_at DESC;
|
||||
|
||||
-- ============================================================================
|
||||
-- Step 2: Soft delete unreferenced models (set deleted_at timestamp)
|
||||
-- ============================================================================
|
||||
-- Uncomment the following line to perform soft delete:
|
||||
-- UPDATE models m
|
||||
-- SET deleted_at = CURRENT_TIMESTAMP
|
||||
-- WHERE m.deleted_at IS NULL
|
||||
-- AND m.is_default = FALSE
|
||||
-- AND m.tenant_id != 0
|
||||
-- AND m.id NOT IN (
|
||||
-- SELECT DISTINCT embedding_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
-- );
|
||||
|
||||
-- ============================================================================
|
||||
-- Step 3: Hard delete unreferenced models (PERMANENT DELETION)
|
||||
-- ============================================================================
|
||||
-- WARNING: This is permanent and cannot be undone!
|
||||
-- Only use this if you're sure you want to permanently remove the records.
|
||||
-- Uncomment the following line to perform hard delete:
|
||||
-- DELETE FROM models
|
||||
-- WHERE deleted_at IS NULL
|
||||
-- AND is_default = FALSE
|
||||
-- AND tenant_id != 0
|
||||
-- AND id NOT IN (
|
||||
-- SELECT DISTINCT embedding_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT DISTINCT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
-- );
|
||||
|
||||
-- ============================================================================
|
||||
-- Alternative: Using CTE for better performance (PostgreSQL specific)
|
||||
-- ============================================================================
|
||||
-- This version uses Common Table Expressions for better readability and performance
|
||||
|
||||
-- DRY RUN with CTE:
|
||||
WITH referenced_models AS (
|
||||
SELECT embedding_model_id AS model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
UNION
|
||||
SELECT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
UNION
|
||||
SELECT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
UNION
|
||||
SELECT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
UNION
|
||||
SELECT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
)
|
||||
SELECT
|
||||
m.id,
|
||||
m.tenant_id,
|
||||
m.name,
|
||||
m.type,
|
||||
m.source,
|
||||
m.is_default,
|
||||
m.status,
|
||||
m.created_at
|
||||
FROM models m
|
||||
WHERE m.deleted_at IS NULL
|
||||
AND m.is_default = FALSE
|
||||
AND m.id NOT IN (SELECT model_id FROM referenced_models)
|
||||
ORDER BY m.created_at DESC;
|
||||
|
||||
-- Soft delete with CTE:
|
||||
WITH referenced_models AS (
|
||||
SELECT embedding_model_id AS model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
UNION
|
||||
SELECT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
UNION
|
||||
SELECT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
UNION
|
||||
SELECT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
UNION
|
||||
SELECT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
)
|
||||
UPDATE models m
|
||||
SET deleted_at = CURRENT_TIMESTAMP
|
||||
WHERE m.deleted_at IS NULL
|
||||
AND m.is_default = FALSE
|
||||
AND m.id NOT IN (SELECT model_id FROM referenced_models);
|
||||
|
||||
-- Hard delete with CTE:
|
||||
-- WITH referenced_models AS (
|
||||
-- SELECT embedding_model_id AS model_id FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT summary_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT rerank_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT vlm_model_id FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
-- UNION
|
||||
-- SELECT embedding_model_id FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
-- )
|
||||
-- DELETE FROM models
|
||||
-- WHERE deleted_at IS NULL
|
||||
-- AND is_default = FALSE
|
||||
-- AND tenant_id != 0
|
||||
-- AND id NOT IN (SELECT model_id FROM referenced_models);
|
||||
|
||||
-- ============================================================================
|
||||
-- Additional Queries for Statistics
|
||||
-- ============================================================================
|
||||
|
||||
-- Count total models by type
|
||||
SELECT
|
||||
type,
|
||||
COUNT(*) as total_count,
|
||||
SUM(CASE WHEN is_default = TRUE THEN 1 ELSE 0 END) as default_count,
|
||||
SUM(CASE WHEN deleted_at IS NULL THEN 1 ELSE 0 END) as active_count
|
||||
FROM models
|
||||
GROUP BY type;
|
||||
|
||||
-- Count models referenced by knowledge bases
|
||||
SELECT
|
||||
'embedding_model' as model_type,
|
||||
COUNT(DISTINCT embedding_model_id) as referenced_count
|
||||
FROM knowledge_bases
|
||||
WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
UNION ALL
|
||||
SELECT
|
||||
'summary_model' as model_type,
|
||||
COUNT(DISTINCT summary_model_id) as referenced_count
|
||||
FROM knowledge_bases
|
||||
WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
UNION ALL
|
||||
SELECT
|
||||
'rerank_model' as model_type,
|
||||
COUNT(DISTINCT rerank_model_id) as referenced_count
|
||||
FROM knowledge_bases
|
||||
WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
UNION ALL
|
||||
SELECT
|
||||
'vlm_model' as model_type,
|
||||
COUNT(DISTINCT vlm_model_id) as referenced_count
|
||||
FROM knowledge_bases
|
||||
WHERE deleted_at IS NULL AND vlm_model_id != '';
|
||||
|
||||
-- List all referenced models with their reference count
|
||||
WITH model_references AS (
|
||||
SELECT embedding_model_id AS model_id, 'embedding' AS ref_type FROM knowledge_bases WHERE deleted_at IS NULL AND embedding_model_id != ''
|
||||
UNION ALL
|
||||
SELECT summary_model_id, 'summary' FROM knowledge_bases WHERE deleted_at IS NULL AND summary_model_id != ''
|
||||
UNION ALL
|
||||
SELECT rerank_model_id, 'rerank' FROM knowledge_bases WHERE deleted_at IS NULL AND rerank_model_id != ''
|
||||
UNION ALL
|
||||
SELECT vlm_model_id, 'vlm' FROM knowledge_bases WHERE deleted_at IS NULL AND vlm_model_id != ''
|
||||
UNION ALL
|
||||
SELECT embedding_model_id, 'knowledge_embedding' FROM knowledges WHERE deleted_at IS NULL AND embedding_model_id IS NOT NULL AND embedding_model_id != ''
|
||||
)
|
||||
SELECT
|
||||
m.id,
|
||||
m.name,
|
||||
m.type,
|
||||
STRING_AGG(DISTINCT mr.ref_type, ', ' ORDER BY mr.ref_type) AS referenced_as,
|
||||
COUNT(*) AS reference_count
|
||||
FROM models m
|
||||
INNER JOIN model_references mr ON m.id = mr.model_id
|
||||
WHERE m.deleted_at IS NULL
|
||||
GROUP BY m.id, m.name, m.type
|
||||
ORDER BY reference_count DESC, m.name;
|
||||
|
||||
-- ============================================================================
|
||||
-- Rollback Strategy
|
||||
-- ============================================================================
|
||||
-- If you performed a soft delete and need to rollback:
|
||||
-- UPDATE models
|
||||
-- SET deleted_at = NULL
|
||||
-- WHERE deleted_at > 'YOUR_DELETION_TIMESTAMP';
|
||||
-- ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user