chore: Remove deprecated migration files. This cleanup helps streamline the migration process and maintain a cleaner codebase.
This commit is contained in:
@@ -0,0 +1,139 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
DROP INDEX IF EXISTS idx_knowledges_summary_status;
|
||||||
|
ALTER TABLE knowledges DROP COLUMN IF EXISTS summary_status;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
DROP COLUMN IF EXISTS question_generation_config;
|
||||||
|
|
||||||
|
ALTER TABLE users
|
||||||
|
DROP COLUMN IF EXISTS can_access_all_tenants;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
ADD COLUMN IF NOT EXISTS rerank_model_id VARCHAR(64);
|
||||||
|
|
||||||
|
UPDATE models m
|
||||||
|
JOIN knowledges k ON m.id = k.id
|
||||||
|
SET m.tenant_id = 0
|
||||||
|
WHERE m.tenant_id = k.tenant_id;
|
||||||
|
|
||||||
|
-- Drop index on content_hash
|
||||||
|
DROP INDEX IF EXISTS idx_chunks_content_hash;
|
||||||
|
|
||||||
|
-- Drop content_hash column
|
||||||
|
ALTER TABLE chunks
|
||||||
|
DROP COLUMN IF EXISTS content_hash;
|
||||||
|
|
||||||
|
-- Drop status column
|
||||||
|
ALTER TABLE chunks
|
||||||
|
DROP COLUMN IF EXISTS status;
|
||||||
|
|
||||||
|
DROP INDEX IF EXISTS idx_embeddings_knowledge_base_id;
|
||||||
|
|
||||||
|
-- Drop index
|
||||||
|
DROP INDEX IF EXISTS idx_embeddings_is_enabled;
|
||||||
|
|
||||||
|
-- Remove is_enabled column
|
||||||
|
ALTER TABLE embeddings
|
||||||
|
DROP COLUMN IF EXISTS is_enabled;
|
||||||
|
|
||||||
|
-- Drop indexes and columns referencing tags
|
||||||
|
DROP INDEX IF EXISTS idx_chunks_tag;
|
||||||
|
ALTER TABLE chunks DROP COLUMN IF EXISTS tag_id;
|
||||||
|
|
||||||
|
DROP INDEX IF EXISTS idx_knowledges_tag;
|
||||||
|
ALTER TABLE knowledges DROP COLUMN IF EXISTS tag_id;
|
||||||
|
|
||||||
|
-- Drop tag table
|
||||||
|
DROP INDEX IF EXISTS idx_knowledge_tags_kb_name;
|
||||||
|
DROP INDEX IF EXISTS idx_knowledge_tags_kb;
|
||||||
|
DROP TABLE IF EXISTS knowledge_tags;
|
||||||
|
|
||||||
|
ALTER TABLE chunks
|
||||||
|
DROP COLUMN IF EXISTS metadata;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
DROP COLUMN IF EXISTS faq_config,
|
||||||
|
DROP COLUMN IF EXISTS type;
|
||||||
|
|
||||||
|
-- Drop index
|
||||||
|
DROP INDEX IF EXISTS idx_models_is_builtin;
|
||||||
|
|
||||||
|
-- Remove is_builtin column
|
||||||
|
ALTER TABLE models
|
||||||
|
DROP COLUMN IF EXISTS is_builtin;
|
||||||
|
|
||||||
|
-- Remove check constraint
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
DROP CONSTRAINT IF EXISTS chk_mcp_transport_config;
|
||||||
|
|
||||||
|
-- Make url column required again
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
ALTER COLUMN url SET NOT NULL;
|
||||||
|
|
||||||
|
-- Remove stdio_config and env_vars columns
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
DROP COLUMN IF EXISTS env_vars,
|
||||||
|
DROP COLUMN IF EXISTS stdio_config;
|
||||||
|
|
||||||
|
-- Remove web_search_config column
|
||||||
|
ALTER TABLE tenants
|
||||||
|
DROP COLUMN IF EXISTS web_search_config;
|
||||||
|
|
||||||
|
-- Drop trigger
|
||||||
|
DROP TRIGGER IF EXISTS trigger_mcp_services_updated_at ON mcp_services;
|
||||||
|
|
||||||
|
-- Drop function
|
||||||
|
DROP FUNCTION IF EXISTS update_mcp_services_updated_at();
|
||||||
|
|
||||||
|
-- Drop indexes
|
||||||
|
DROP INDEX IF EXISTS idx_mcp_services_tenant_id;
|
||||||
|
DROP INDEX IF EXISTS idx_mcp_services_enabled;
|
||||||
|
DROP INDEX IF EXISTS idx_mcp_services_deleted_at;
|
||||||
|
|
||||||
|
-- Drop table
|
||||||
|
DROP TABLE IF EXISTS mcp_services;
|
||||||
|
|
||||||
|
-- This migration performs a data cleanup (soft delete) which is not safely reversible.
|
||||||
|
-- Down migration is a no-op.
|
||||||
|
|
||||||
|
-- Drop foreign key constraints first
|
||||||
|
ALTER TABLE auth_tokens DROP CONSTRAINT IF EXISTS fk_auth_tokens_user;
|
||||||
|
ALTER TABLE users DROP CONSTRAINT IF EXISTS fk_users_tenant;
|
||||||
|
|
||||||
|
-- Drop tables
|
||||||
|
DROP TABLE IF EXISTS auth_tokens;
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
|
|
||||||
|
-- Drop is_temporary column from knowledge_bases
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
DROP COLUMN IF EXISTS is_temporary;
|
||||||
|
|
||||||
|
-- Drop context_config column from tenants
|
||||||
|
ALTER TABLE tenants
|
||||||
|
DROP COLUMN IF EXISTS context_config;
|
||||||
|
|
||||||
|
-- Drop conversation_config column from tenants
|
||||||
|
ALTER TABLE tenants
|
||||||
|
DROP COLUMN IF EXISTS conversation_config;
|
||||||
|
|
||||||
|
-- Drop JSONB indexes if they exist
|
||||||
|
DROP INDEX IF EXISTS idx_messages_agent_steps;
|
||||||
|
DROP INDEX IF EXISTS idx_sessions_context_config;
|
||||||
|
DROP INDEX IF EXISTS idx_sessions_agent_config;
|
||||||
|
DROP INDEX IF EXISTS idx_tenants_agent_config;
|
||||||
|
|
||||||
|
-- Drop columns if they exist
|
||||||
|
ALTER TABLE messages
|
||||||
|
DROP COLUMN IF EXISTS agent_steps;
|
||||||
|
|
||||||
|
ALTER TABLE sessions
|
||||||
|
DROP COLUMN IF EXISTS context_config;
|
||||||
|
|
||||||
|
ALTER TABLE sessions
|
||||||
|
DROP COLUMN IF EXISTS agent_config;
|
||||||
|
|
||||||
|
ALTER TABLE tenants
|
||||||
|
DROP COLUMN IF EXISTS agent_config;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,532 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Create users table
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
username VARCHAR(100) NOT NULL UNIQUE,
|
||||||
|
email VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
password_hash VARCHAR(255) NOT NULL,
|
||||||
|
avatar VARCHAR(500),
|
||||||
|
tenant_id INTEGER,
|
||||||
|
is_active BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
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 TABLE users IS 'User accounts in the system';
|
||||||
|
COMMENT ON COLUMN users.id IS 'Unique identifier of the user';
|
||||||
|
COMMENT ON COLUMN users.username IS 'Username of the user';
|
||||||
|
COMMENT ON COLUMN users.email IS 'Email address of the user';
|
||||||
|
COMMENT ON COLUMN users.password_hash IS 'Hashed password of the user';
|
||||||
|
COMMENT ON COLUMN users.avatar IS 'Avatar URL of the user';
|
||||||
|
COMMENT ON COLUMN users.tenant_id IS 'Tenant ID that the user belongs to';
|
||||||
|
COMMENT ON COLUMN users.is_active IS 'Whether the user is active';
|
||||||
|
|
||||||
|
-- Add indexes for users
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_deleted_at ON users(deleted_at);
|
||||||
|
|
||||||
|
-- Add foreign key constraint for tenant_id (only if not exists)
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_users_tenant'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE users
|
||||||
|
ADD CONSTRAINT fk_users_tenant
|
||||||
|
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE SET NULL;
|
||||||
|
RAISE NOTICE 'Added foreign key constraint fk_users_tenant';
|
||||||
|
ELSE
|
||||||
|
RAISE NOTICE 'Foreign key constraint fk_users_tenant already exists';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Create auth_tokens table
|
||||||
|
CREATE TABLE IF NOT EXISTS auth_tokens (
|
||||||
|
id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
user_id VARCHAR(36) NOT NULL,
|
||||||
|
token TEXT NOT NULL,
|
||||||
|
token_type VARCHAR(50) NOT NULL,
|
||||||
|
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
|
is_revoked BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMENT ON TABLE auth_tokens IS 'Authentication tokens for users';
|
||||||
|
COMMENT ON COLUMN auth_tokens.id IS 'Unique identifier of the token';
|
||||||
|
COMMENT ON COLUMN auth_tokens.user_id IS 'User ID that owns this token';
|
||||||
|
COMMENT ON COLUMN auth_tokens.token IS 'Token value (JWT or other format)';
|
||||||
|
COMMENT ON COLUMN auth_tokens.token_type IS 'Token type (access_token, refresh_token)';
|
||||||
|
COMMENT ON COLUMN auth_tokens.expires_at IS 'Token expiration time';
|
||||||
|
COMMENT ON COLUMN auth_tokens.is_revoked IS 'Whether the token is revoked';
|
||||||
|
|
||||||
|
-- Add indexes for auth_tokens
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_auth_tokens_user_id ON auth_tokens(user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_auth_tokens_token ON auth_tokens(token);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_auth_tokens_token_type ON auth_tokens(token_type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_auth_tokens_expires_at ON auth_tokens(expires_at);
|
||||||
|
|
||||||
|
-- Add foreign key constraint (only if not exists)
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'fk_auth_tokens_user'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE auth_tokens
|
||||||
|
ADD CONSTRAINT fk_auth_tokens_user
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||||
|
RAISE NOTICE 'Added foreign key constraint fk_auth_tokens_user';
|
||||||
|
ELSE
|
||||||
|
RAISE NOTICE 'Foreign key constraint fk_auth_tokens_user already exists';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add is_temporary column to knowledge_bases
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
ADD COLUMN IF NOT EXISTS is_temporary BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN knowledge_bases.is_temporary IS 'Whether this knowledge base is temporary (ephemeral) and should be hidden from UI';
|
||||||
|
|
||||||
|
-- Add context_config column to tenants
|
||||||
|
ALTER TABLE tenants
|
||||||
|
ADD COLUMN IF NOT EXISTS context_config JSONB;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN tenants.context_config IS 'Global Context configuration for this tenant (default for all sessions)';
|
||||||
|
|
||||||
|
-- Add conversation_config column to tenants
|
||||||
|
ALTER TABLE tenants
|
||||||
|
ADD COLUMN IF NOT EXISTS conversation_config JSONB;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN tenants.conversation_config IS 'Global Conversation configuration for this tenant (default for normal mode sessions)';
|
||||||
|
|
||||||
|
-- For tenants table: add 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
|
||||||
|
-- If field exists but type is JSON, convert to JSONB
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'tenants'
|
||||||
|
AND column_name = 'agent_config'
|
||||||
|
AND data_type = 'json'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE tenants
|
||||||
|
ALTER COLUMN agent_config TYPE JSONB USING agent_config::jsonb;
|
||||||
|
|
||||||
|
RAISE NOTICE 'Converted tenants.agent_config from JSON to JSONB';
|
||||||
|
ELSE
|
||||||
|
RAISE NOTICE 'agent_config column already exists in tenants table';
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- For sessions table: add 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 $$;
|
||||||
|
|
||||||
|
-- For sessions table: add 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 $$;
|
||||||
|
|
||||||
|
-- For messages table: add 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 $$;
|
||||||
|
|
||||||
|
-- Add GIN indexes for JSON fields
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
-- For tenants.agent_config
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_indexes
|
||||||
|
WHERE tablename = 'tenants'
|
||||||
|
AND indexname = 'idx_tenants_agent_config'
|
||||||
|
) THEN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'tenants'
|
||||||
|
AND column_name = 'agent_config'
|
||||||
|
AND data_type = 'jsonb'
|
||||||
|
) THEN
|
||||||
|
CREATE INDEX idx_tenants_agent_config ON tenants USING GIN (agent_config);
|
||||||
|
RAISE NOTICE 'Created index idx_tenants_agent_config';
|
||||||
|
ELSE
|
||||||
|
RAISE NOTICE 'Skipped index creation for tenants.agent_config (not JSONB type)';
|
||||||
|
END IF;
|
||||||
|
ELSE
|
||||||
|
RAISE NOTICE 'Index idx_tenants_agent_config already exists';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
-- For 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;
|
||||||
|
|
||||||
|
-- For 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;
|
||||||
|
|
||||||
|
-- For 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 $$;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
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_config ->> 'model_id'
|
||||||
|
FROM knowledge_bases
|
||||||
|
WHERE deleted_at IS NULL
|
||||||
|
AND vlm_config ->> 'model_id' IS NOT NULL
|
||||||
|
AND vlm_config ->> '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.tenant_id != 0
|
||||||
|
AND m.id NOT IN (SELECT model_id FROM referenced_models);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Create mcp_services table
|
||||||
|
CREATE TABLE IF NOT EXISTS mcp_services (
|
||||||
|
id VARCHAR(36) PRIMARY KEY,
|
||||||
|
tenant_id INTEGER NOT NULL,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
enabled BOOLEAN DEFAULT true,
|
||||||
|
transport_type VARCHAR(50) NOT NULL,
|
||||||
|
url VARCHAR(512) NOT NULL,
|
||||||
|
headers JSONB,
|
||||||
|
auth_config JSONB,
|
||||||
|
advanced_config JSONB,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
deleted_at TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create indexes
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_mcp_services_tenant_id ON mcp_services(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_mcp_services_enabled ON mcp_services(enabled);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_mcp_services_deleted_at ON mcp_services(deleted_at);
|
||||||
|
|
||||||
|
-- Add comment to table
|
||||||
|
COMMENT ON TABLE mcp_services IS 'MCP service configurations';
|
||||||
|
|
||||||
|
-- Create trigger for updated_at
|
||||||
|
CREATE OR REPLACE FUNCTION update_mcp_services_updated_at()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER trigger_mcp_services_updated_at
|
||||||
|
BEFORE UPDATE ON mcp_services
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION update_mcp_services_updated_at();
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Add web_search_config column to tenants table
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'tenants' AND column_name = 'web_search_config'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE tenants
|
||||||
|
ADD COLUMN web_search_config JSONB DEFAULT NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add or update comment
|
||||||
|
COMMENT ON COLUMN tenants.web_search_config IS 'Web search configuration for the tenant';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Add stdio_config and env_vars columns
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
ADD COLUMN IF NOT EXISTS stdio_config JSONB,
|
||||||
|
ADD COLUMN IF NOT EXISTS env_vars JSONB;
|
||||||
|
|
||||||
|
-- Make url column optional
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
ALTER COLUMN url DROP NOT NULL;
|
||||||
|
|
||||||
|
-- Add check constraint
|
||||||
|
ALTER TABLE mcp_services
|
||||||
|
ADD CONSTRAINT chk_mcp_transport_config CHECK (
|
||||||
|
(transport_type = 'stdio' AND stdio_config IS NOT NULL) OR
|
||||||
|
(transport_type != 'stdio' AND url IS NOT NULL)
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Add is_builtin column to models table
|
||||||
|
ALTER TABLE models
|
||||||
|
ADD COLUMN IF NOT EXISTS is_builtin BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
|
||||||
|
-- Add index for is_builtin field
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_models_is_builtin ON models(is_builtin);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
ADD COLUMN IF NOT EXISTS type VARCHAR(32) NOT NULL DEFAULT 'document',
|
||||||
|
ADD COLUMN IF NOT EXISTS faq_config JSONB;
|
||||||
|
|
||||||
|
UPDATE knowledge_bases
|
||||||
|
SET type = 'document'
|
||||||
|
WHERE type IS NULL OR type = '';
|
||||||
|
|
||||||
|
ALTER TABLE chunks
|
||||||
|
ADD COLUMN IF NOT EXISTS metadata JSONB;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Tag table (per knowledge base)
|
||||||
|
CREATE TABLE IF NOT EXISTS knowledge_tags (
|
||||||
|
id VARCHAR(36) PRIMARY KEY,
|
||||||
|
tenant_id INTEGER NOT NULL,
|
||||||
|
knowledge_base_id VARCHAR(36) NOT NULL,
|
||||||
|
name VARCHAR(128) NOT NULL,
|
||||||
|
color VARCHAR(32),
|
||||||
|
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
deleted_at TIMESTAMPTZ
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_tags_kb_name
|
||||||
|
ON knowledge_tags(tenant_id, knowledge_base_id, name);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_knowledge_tags_kb
|
||||||
|
ON knowledge_tags(tenant_id, knowledge_base_id);
|
||||||
|
|
||||||
|
-- Tag reference on knowledges
|
||||||
|
ALTER TABLE knowledges
|
||||||
|
ADD COLUMN IF NOT EXISTS tag_id VARCHAR(36);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_knowledges_tag
|
||||||
|
ON knowledges(tag_id);
|
||||||
|
|
||||||
|
-- Tag reference on chunks
|
||||||
|
ALTER TABLE chunks
|
||||||
|
ADD COLUMN IF NOT EXISTS tag_id VARCHAR(36);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_chunks_tag
|
||||||
|
ON chunks(tag_id);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Add is_enabled column to embeddings table
|
||||||
|
ALTER TABLE embeddings
|
||||||
|
ADD COLUMN IF NOT EXISTS is_enabled BOOLEAN DEFAULT TRUE;
|
||||||
|
|
||||||
|
-- Create index for is_enabled column
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_embeddings_is_enabled
|
||||||
|
ON embeddings(is_enabled);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Create index for knowledge_base_id
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_embeddings_knowledge_base_id
|
||||||
|
ON embeddings(knowledge_base_id);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
-- Add status field to track chunk processing state
|
||||||
|
ALTER TABLE chunks
|
||||||
|
ADD COLUMN IF NOT EXISTS status INT NOT NULL DEFAULT 0;
|
||||||
|
|
||||||
|
-- Add content_hash field for quick content matching
|
||||||
|
ALTER TABLE chunks
|
||||||
|
ADD COLUMN IF NOT EXISTS content_hash VARCHAR(64);
|
||||||
|
|
||||||
|
-- Create index on content_hash
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_chunks_content_hash
|
||||||
|
ON chunks(content_hash);
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
WITH tenant_source AS (
|
||||||
|
SELECT
|
||||||
|
kb.embedding_model_id AS model_id,
|
||||||
|
kb.tenant_id
|
||||||
|
FROM knowledge_bases kb
|
||||||
|
WHERE kb.tenant_id IS NOT NULL
|
||||||
|
AND kb.embedding_model_id IS NOT NULL
|
||||||
|
AND kb.embedding_model_id <> ''
|
||||||
|
|
||||||
|
UNION
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
kb.summary_model_id AS model_id,
|
||||||
|
kb.tenant_id
|
||||||
|
FROM knowledge_bases kb
|
||||||
|
WHERE kb.tenant_id IS NOT NULL
|
||||||
|
AND kb.summary_model_id IS NOT NULL
|
||||||
|
AND kb.summary_model_id <> ''
|
||||||
|
|
||||||
|
UNION
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
kb.rerank_model_id AS model_id,
|
||||||
|
kb.tenant_id
|
||||||
|
FROM knowledge_bases kb
|
||||||
|
WHERE kb.tenant_id IS NOT NULL
|
||||||
|
AND kb.rerank_model_id IS NOT NULL
|
||||||
|
AND kb.rerank_model_id <> ''
|
||||||
|
)
|
||||||
|
UPDATE models m
|
||||||
|
SET tenant_id = ts.tenant_id
|
||||||
|
FROM tenant_source ts
|
||||||
|
WHERE m.id = ts.model_id
|
||||||
|
AND m.tenant_id = 0;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
DROP COLUMN IF EXISTS rerank_model_id;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE users
|
||||||
|
ADD COLUMN IF NOT EXISTS can_access_all_tenants BOOLEAN NOT NULL DEFAULT FALSE;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
||||||
|
ALTER TABLE knowledge_bases
|
||||||
|
ADD COLUMN IF NOT EXISTS question_generation_config JSONB NULL ;
|
||||||
|
|
||||||
|
ALTER TABLE knowledges ADD COLUMN IF NOT EXISTS summary_status VARCHAR(32) DEFAULT 'none';
|
||||||
|
|
||||||
|
-- Add index for efficient querying
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_knowledges_summary_status ON knowledges(summary_status);
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
-- 000001_create_users_and_auth_tokens.down.sql
|
|
||||||
-- Drop users and auth_tokens tables
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop foreign key constraints first
|
|
||||||
ALTER TABLE auth_tokens DROP CONSTRAINT IF EXISTS fk_auth_tokens_user;
|
|
||||||
ALTER TABLE users DROP CONSTRAINT IF EXISTS fk_users_tenant;
|
|
||||||
|
|
||||||
-- Drop tables
|
|
||||||
DROP TABLE IF EXISTS auth_tokens;
|
|
||||||
DROP TABLE IF EXISTS users;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
-- 000001_create_users_and_auth_tokens.up.sql
|
|
||||||
-- Create users and auth_tokens tables
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Create users table
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
username VARCHAR(100) NOT NULL UNIQUE,
|
|
||||||
email VARCHAR(255) NOT NULL UNIQUE,
|
|
||||||
password_hash VARCHAR(255) NOT NULL,
|
|
||||||
avatar VARCHAR(500),
|
|
||||||
tenant_id INTEGER,
|
|
||||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
|
||||||
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 TABLE users IS 'User accounts in the system';
|
|
||||||
COMMENT ON COLUMN users.id IS 'Unique identifier of the user';
|
|
||||||
COMMENT ON COLUMN users.username IS 'Username of the user';
|
|
||||||
COMMENT ON COLUMN users.email IS 'Email address of the user';
|
|
||||||
COMMENT ON COLUMN users.password_hash IS 'Hashed password of the user';
|
|
||||||
COMMENT ON COLUMN users.avatar IS 'Avatar URL of the user';
|
|
||||||
COMMENT ON COLUMN users.tenant_id IS 'Tenant ID that the user belongs to';
|
|
||||||
COMMENT ON COLUMN users.is_active IS 'Whether the user is active';
|
|
||||||
|
|
||||||
-- Add indexes for users
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_users_deleted_at ON users(deleted_at);
|
|
||||||
|
|
||||||
-- Add foreign key constraint for tenant_id
|
|
||||||
ALTER TABLE users
|
|
||||||
ADD CONSTRAINT fk_users_tenant
|
|
||||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE SET NULL;
|
|
||||||
|
|
||||||
-- Create auth_tokens table
|
|
||||||
CREATE TABLE IF NOT EXISTS auth_tokens (
|
|
||||||
id VARCHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
user_id VARCHAR(36) NOT NULL,
|
|
||||||
token TEXT NOT NULL,
|
|
||||||
token_type VARCHAR(50) NOT NULL,
|
|
||||||
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
|
||||||
is_revoked BOOLEAN NOT NULL DEFAULT false,
|
|
||||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMENT ON TABLE auth_tokens IS 'Authentication tokens for users';
|
|
||||||
COMMENT ON COLUMN auth_tokens.id IS 'Unique identifier of the token';
|
|
||||||
COMMENT ON COLUMN auth_tokens.user_id IS 'User ID that owns this token';
|
|
||||||
COMMENT ON COLUMN auth_tokens.token IS 'Token value (JWT or other format)';
|
|
||||||
COMMENT ON COLUMN auth_tokens.token_type IS 'Token type (access_token, refresh_token)';
|
|
||||||
COMMENT ON COLUMN auth_tokens.expires_at IS 'Token expiration time';
|
|
||||||
COMMENT ON COLUMN auth_tokens.is_revoked IS 'Whether the token is revoked';
|
|
||||||
|
|
||||||
-- Add indexes for auth_tokens
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_auth_tokens_user_id ON auth_tokens(user_id);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_auth_tokens_token ON auth_tokens(token);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_auth_tokens_token_type ON auth_tokens(token_type);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_auth_tokens_expires_at ON auth_tokens(expires_at);
|
|
||||||
|
|
||||||
-- Add foreign key constraint
|
|
||||||
ALTER TABLE auth_tokens
|
|
||||||
ADD CONSTRAINT fk_auth_tokens_user
|
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
-- 000016_add_agent_config_if_missing.down.sql
|
|
||||||
-- Rollback for agent_config / context_config / agent_steps columns and indexes
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop JSONB indexes if they exist
|
|
||||||
DROP INDEX IF EXISTS idx_messages_agent_steps;
|
|
||||||
DROP INDEX IF EXISTS idx_sessions_context_config;
|
|
||||||
DROP INDEX IF EXISTS idx_sessions_agent_config;
|
|
||||||
DROP INDEX IF EXISTS idx_tenants_agent_config;
|
|
||||||
|
|
||||||
-- Drop columns if they exist
|
|
||||||
ALTER TABLE messages
|
|
||||||
DROP COLUMN IF EXISTS agent_steps;
|
|
||||||
|
|
||||||
ALTER TABLE sessions
|
|
||||||
DROP COLUMN IF EXISTS context_config;
|
|
||||||
|
|
||||||
ALTER TABLE sessions
|
|
||||||
DROP COLUMN IF EXISTS agent_config;
|
|
||||||
|
|
||||||
ALTER TABLE tenants
|
|
||||||
DROP COLUMN IF EXISTS agent_config;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
-- 000016_add_agent_config_if_missing.up.sql
|
|
||||||
-- Incremental migration: add agent_config / context_config / agent_steps columns and indexes if missing
|
|
||||||
-- Ported from migrations/paradedb/02-add-agent-config-if-missing.sql
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- ============================================
|
|
||||||
-- 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
|
|
||||||
-- 如果字段已存在但类型是 JSON,转换为 JSONB
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM information_schema.columns
|
|
||||||
WHERE table_name = 'tenants'
|
|
||||||
AND column_name = 'agent_config'
|
|
||||||
AND data_type = 'json'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE tenants
|
|
||||||
ALTER COLUMN agent_config TYPE JSONB USING agent_config::jsonb;
|
|
||||||
|
|
||||||
RAISE NOTICE 'Converted tenants.agent_config from JSON to JSONB';
|
|
||||||
ELSE
|
|
||||||
RAISE NOTICE 'agent_config column already exists in tenants table';
|
|
||||||
END IF;
|
|
||||||
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 添加索引(仅当字段类型为 JSONB 时)
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM pg_indexes
|
|
||||||
WHERE tablename = 'tenants'
|
|
||||||
AND indexname = 'idx_tenants_agent_config'
|
|
||||||
) THEN
|
|
||||||
-- 检查字段类型是否为 JSONB
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM information_schema.columns
|
|
||||||
WHERE table_name = 'tenants'
|
|
||||||
AND column_name = 'agent_config'
|
|
||||||
AND data_type = 'jsonb'
|
|
||||||
) THEN
|
|
||||||
CREATE INDEX idx_tenants_agent_config ON tenants USING GIN (agent_config);
|
|
||||||
RAISE NOTICE 'Created index idx_tenants_agent_config';
|
|
||||||
ELSE
|
|
||||||
RAISE NOTICE 'Skipped index creation for tenants.agent_config (not JSONB type)';
|
|
||||||
END IF;
|
|
||||||
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 $$;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
-- 000017_cleanup_unreferenced_models.down.sql
|
|
||||||
-- This migration performs a data cleanup (soft delete) which is not safely reversible.
|
|
||||||
-- Down migration is a no-op.
|
|
||||||
|
|
||||||
SELECT 1;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
-- 000017_cleanup_unreferenced_models.up.sql
|
|
||||||
-- Cleanup unreferenced models by soft-deleting them (set deleted_at)
|
|
||||||
-- Ported from migrations/paradedb/03-cleanup-unreferenced-models.sql
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
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_config ->> 'model_id'
|
|
||||||
FROM knowledge_bases
|
|
||||||
WHERE deleted_at IS NULL
|
|
||||||
AND vlm_config ->> 'model_id' IS NOT NULL
|
|
||||||
AND vlm_config ->> '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.tenant_id != 0
|
|
||||||
AND m.id NOT IN (SELECT model_id FROM referenced_models);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
-- Rollback: Remove MCP services table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop trigger
|
|
||||||
DROP TRIGGER IF EXISTS trigger_mcp_services_updated_at ON mcp_services;
|
|
||||||
|
|
||||||
-- Drop function
|
|
||||||
DROP FUNCTION IF EXISTS update_mcp_services_updated_at();
|
|
||||||
|
|
||||||
-- Drop indexes
|
|
||||||
DROP INDEX IF EXISTS idx_mcp_services_tenant_id;
|
|
||||||
DROP INDEX IF EXISTS idx_mcp_services_enabled;
|
|
||||||
DROP INDEX IF EXISTS idx_mcp_services_deleted_at;
|
|
||||||
|
|
||||||
-- Drop table
|
|
||||||
DROP TABLE IF EXISTS mcp_services;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
-- 04-add-mcp-services.sql
|
|
||||||
-- Create MCP services table for managing external MCP (Model Context Protocol) services
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Create mcp_services table
|
|
||||||
CREATE TABLE IF NOT EXISTS mcp_services (
|
|
||||||
id VARCHAR(36) PRIMARY KEY,
|
|
||||||
tenant_id INTEGER NOT NULL,
|
|
||||||
name VARCHAR(255) NOT NULL,
|
|
||||||
description TEXT,
|
|
||||||
enabled BOOLEAN DEFAULT true,
|
|
||||||
transport_type VARCHAR(50) NOT NULL, -- Transport type: sse or http-streamable
|
|
||||||
url VARCHAR(512) NOT NULL,
|
|
||||||
headers JSONB, -- HTTP headers as JSON object
|
|
||||||
auth_config JSONB, -- Authentication configuration (API key, token, custom headers)
|
|
||||||
advanced_config JSONB, -- Advanced configuration (timeout, retry settings)
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
deleted_at TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create indexes
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_mcp_services_tenant_id ON mcp_services(tenant_id);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_mcp_services_enabled ON mcp_services(enabled);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_mcp_services_deleted_at ON mcp_services(deleted_at);
|
|
||||||
|
|
||||||
-- Add comment to table
|
|
||||||
COMMENT ON TABLE mcp_services IS 'MCP service configurations';
|
|
||||||
|
|
||||||
-- Create trigger for updated_at
|
|
||||||
CREATE OR REPLACE FUNCTION update_mcp_services_updated_at()
|
|
||||||
RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
CREATE TRIGGER trigger_mcp_services_updated_at
|
|
||||||
BEFORE UPDATE ON mcp_services
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION update_mcp_services_updated_at();
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
-- 000005_add_web_search_config.down.sql
|
|
||||||
-- Remove web_search_config column from tenants table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Remove web_search_config column
|
|
||||||
ALTER TABLE tenants
|
|
||||||
DROP COLUMN IF EXISTS web_search_config;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
-- 000005_add_web_search_config.up.sql
|
|
||||||
-- Add web_search_config column to tenants table for web search configuration
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add web_search_config column to tenants table (if not exists)
|
|
||||||
-- Note: PostgreSQL 9.6+ supports IF NOT EXISTS
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM information_schema.columns
|
|
||||||
WHERE table_name = 'tenants' AND column_name = 'web_search_config'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE tenants
|
|
||||||
ADD COLUMN web_search_config JSONB DEFAULT NULL;
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
-- Add or update comment
|
|
||||||
COMMENT ON COLUMN tenants.web_search_config IS 'Web search configuration for the tenant';
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
-- 06-add-mcp-stdio-support.sql
|
|
||||||
-- Rollback stdio transport support for MCP services
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Remove check constraint
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
DROP CONSTRAINT IF EXISTS chk_mcp_transport_config;
|
|
||||||
|
|
||||||
-- Make url column required again
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
ALTER COLUMN url SET NOT NULL;
|
|
||||||
|
|
||||||
-- Remove stdio_config and env_vars columns
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
DROP COLUMN IF EXISTS env_vars,
|
|
||||||
DROP COLUMN IF EXISTS stdio_config;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
-- 06-add-mcp-stdio-support.sql
|
|
||||||
-- Add stdio transport support for MCP services
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add stdio_config and env_vars columns
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
ADD COLUMN IF NOT EXISTS stdio_config JSONB, -- Stdio configuration: {command: "uvx"/"npx", args: [...]}
|
|
||||||
ADD COLUMN IF NOT EXISTS env_vars JSONB; -- Environment variables for stdio transport
|
|
||||||
|
|
||||||
-- Make url column optional (remove NOT NULL constraint)
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
ALTER COLUMN url DROP NOT NULL;
|
|
||||||
|
|
||||||
-- Add check constraint: stdio transport requires stdio_config, others require url
|
|
||||||
ALTER TABLE mcp_services
|
|
||||||
ADD CONSTRAINT chk_mcp_transport_config CHECK (
|
|
||||||
(transport_type = 'stdio' AND stdio_config IS NOT NULL) OR
|
|
||||||
(transport_type != 'stdio' AND url IS NOT NULL)
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
-- 07-add-builtin-models.sql (rollback)
|
|
||||||
-- Remove is_builtin field from models table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop index
|
|
||||||
DROP INDEX IF EXISTS idx_models_is_builtin;
|
|
||||||
|
|
||||||
-- Remove is_builtin column
|
|
||||||
ALTER TABLE models
|
|
||||||
DROP COLUMN IF EXISTS is_builtin;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
-- 07-add-builtin-models.sql
|
|
||||||
-- Add is_builtin field to models table to support builtin models visible to all tenants
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add is_builtin column to models table
|
|
||||||
ALTER TABLE models
|
|
||||||
ADD COLUMN IF NOT EXISTS is_builtin BOOLEAN NOT NULL DEFAULT false;
|
|
||||||
|
|
||||||
-- Add index for is_builtin field to improve query performance
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_models_is_builtin ON models(is_builtin);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
-- 08-add-faq-support rollback
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE chunks
|
|
||||||
DROP COLUMN IF EXISTS metadata;
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
DROP COLUMN IF EXISTS faq_config,
|
|
||||||
DROP COLUMN IF EXISTS type;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
-- 08-add-faq-support.sql
|
|
||||||
-- Introduce FAQ knowledge base type and chunk metadata storage
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
ADD COLUMN IF NOT EXISTS type VARCHAR(32) NOT NULL DEFAULT 'document',
|
|
||||||
ADD COLUMN IF NOT EXISTS faq_config JSONB;
|
|
||||||
|
|
||||||
UPDATE knowledge_bases
|
|
||||||
SET type = 'document'
|
|
||||||
WHERE type IS NULL OR type = '';
|
|
||||||
|
|
||||||
ALTER TABLE chunks
|
|
||||||
ADD COLUMN IF NOT EXISTS metadata JSONB;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
-- 000009_add_kb_tags.down.sql
|
|
||||||
-- Drop knowledge base scoped tags and tag_id fields
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop indexes and columns referencing tags
|
|
||||||
DROP INDEX IF EXISTS idx_chunks_tag;
|
|
||||||
ALTER TABLE chunks DROP COLUMN IF EXISTS tag_id;
|
|
||||||
|
|
||||||
DROP INDEX IF EXISTS idx_knowledges_tag;
|
|
||||||
ALTER TABLE knowledges DROP COLUMN IF EXISTS tag_id;
|
|
||||||
|
|
||||||
-- Drop tag table
|
|
||||||
DROP INDEX IF EXISTS idx_knowledge_tags_kb_name;
|
|
||||||
DROP INDEX IF EXISTS idx_knowledge_tags_kb;
|
|
||||||
DROP TABLE IF EXISTS knowledge_tags;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
-- 000009_add_kb_tags.up.sql
|
|
||||||
-- Add knowledge base scoped tags and tag_id fields
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Tag table (per knowledge base)
|
|
||||||
CREATE TABLE IF NOT EXISTS knowledge_tags (
|
|
||||||
id VARCHAR(36) PRIMARY KEY,
|
|
||||||
tenant_id INTEGER NOT NULL,
|
|
||||||
knowledge_base_id VARCHAR(36) NOT NULL,
|
|
||||||
name VARCHAR(128) NOT NULL,
|
|
||||||
color VARCHAR(32),
|
|
||||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
|
||||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
deleted_at TIMESTAMPTZ
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_tags_kb_name
|
|
||||||
ON knowledge_tags(tenant_id, knowledge_base_id, name);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_knowledge_tags_kb
|
|
||||||
ON knowledge_tags(tenant_id, knowledge_base_id);
|
|
||||||
|
|
||||||
-- Tag reference on knowledges
|
|
||||||
ALTER TABLE knowledges
|
|
||||||
ADD COLUMN IF NOT EXISTS tag_id VARCHAR(36);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_knowledges_tag
|
|
||||||
ON knowledges(tag_id);
|
|
||||||
|
|
||||||
-- Tag reference on chunks
|
|
||||||
ALTER TABLE chunks
|
|
||||||
ADD COLUMN IF NOT EXISTS tag_id VARCHAR(36);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_chunks_tag
|
|
||||||
ON chunks(tag_id);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
-- 000010_add_embeddings_is_enabled.down.sql
|
|
||||||
-- Remove is_enabled column from embeddings table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop index
|
|
||||||
DROP INDEX IF EXISTS idx_embeddings_is_enabled;
|
|
||||||
|
|
||||||
-- Remove is_enabled column
|
|
||||||
ALTER TABLE embeddings
|
|
||||||
DROP COLUMN IF EXISTS is_enabled;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
-- 000010_add_embeddings_is_enabled.up.sql
|
|
||||||
-- Add is_enabled column to embeddings table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add is_enabled column to embeddings table
|
|
||||||
ALTER TABLE embeddings
|
|
||||||
ADD COLUMN IF NOT EXISTS is_enabled BOOLEAN DEFAULT TRUE;
|
|
||||||
|
|
||||||
-- Create index for is_enabled column
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_embeddings_is_enabled
|
|
||||||
ON embeddings(is_enabled);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
-- 000012_add_embeddings_kb_id_index.down.sql
|
|
||||||
-- Remove B-tree index on knowledge_base_id from embeddings table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
DROP INDEX IF EXISTS idx_embeddings_knowledge_base_id;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
-- 000012_add_embeddings_kb_id_index.up.sql
|
|
||||||
-- Add B-tree index on knowledge_base_id for embeddings table to improve query performance
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Create index for knowledge_base_id to optimize filtering queries
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_embeddings_knowledge_base_id
|
|
||||||
ON embeddings(knowledge_base_id);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
-- 000013_add_chunk_status_and_hash.down.sql
|
|
||||||
-- Rollback: Remove status and content_hash fields from chunks table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop index on content_hash
|
|
||||||
DROP INDEX IF EXISTS idx_chunks_content_hash;
|
|
||||||
|
|
||||||
-- Drop content_hash column
|
|
||||||
ALTER TABLE chunks
|
|
||||||
DROP COLUMN IF EXISTS content_hash;
|
|
||||||
|
|
||||||
-- Drop status column
|
|
||||||
ALTER TABLE chunks
|
|
||||||
DROP COLUMN IF EXISTS status;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
-- 000013_add_chunk_status_and_hash.up.sql
|
|
||||||
-- Add status and content_hash fields to chunks table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add status field to track chunk processing state
|
|
||||||
ALTER TABLE chunks
|
|
||||||
ADD COLUMN IF NOT EXISTS status INT NOT NULL DEFAULT 0;
|
|
||||||
|
|
||||||
-- Add content_hash field for quick content matching (primarily for FAQ)
|
|
||||||
ALTER TABLE chunks
|
|
||||||
ADD COLUMN IF NOT EXISTS content_hash VARCHAR(64);
|
|
||||||
|
|
||||||
-- Create index on content_hash for efficient lookup
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_chunks_content_hash
|
|
||||||
ON chunks(content_hash);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
-- 14-backfill-models-tenant-id.down.sql
|
|
||||||
-- Reset tenant_id in models back to 0 for records tied to knowledges entries
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
UPDATE models m
|
|
||||||
JOIN knowledges k ON m.id = k.id
|
|
||||||
SET m.tenant_id = 0
|
|
||||||
WHERE m.tenant_id = k.tenant_id;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
-- 14-backfill-models-tenant-id.up.sql
|
|
||||||
-- Backfill tenant_id in models table using knowledges records that share the same id
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
WITH tenant_source AS (
|
|
||||||
-- 从 knowledge_bases 中取出各模型 ID(embedding / summary)及其 tenant_id
|
|
||||||
SELECT
|
|
||||||
kb.embedding_model_id AS model_id,
|
|
||||||
kb.tenant_id
|
|
||||||
FROM knowledge_bases kb
|
|
||||||
WHERE kb.tenant_id IS NOT NULL
|
|
||||||
AND kb.embedding_model_id IS NOT NULL
|
|
||||||
AND kb.embedding_model_id <> ''
|
|
||||||
|
|
||||||
UNION
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
kb.summary_model_id AS model_id,
|
|
||||||
kb.tenant_id
|
|
||||||
FROM knowledge_bases kb
|
|
||||||
WHERE kb.tenant_id IS NOT NULL
|
|
||||||
AND kb.summary_model_id IS NOT NULL
|
|
||||||
AND kb.summary_model_id <> ''
|
|
||||||
|
|
||||||
UNION
|
|
||||||
|
|
||||||
-- rerank 模型
|
|
||||||
SELECT
|
|
||||||
kb.rerank_model_id AS model_id,
|
|
||||||
kb.tenant_id
|
|
||||||
FROM knowledge_bases kb
|
|
||||||
WHERE kb.tenant_id IS NOT NULL
|
|
||||||
AND kb.rerank_model_id IS NOT NULL
|
|
||||||
AND kb.rerank_model_id <> ''
|
|
||||||
)
|
|
||||||
UPDATE models m
|
|
||||||
SET tenant_id = ts.tenant_id
|
|
||||||
FROM tenant_source ts
|
|
||||||
WHERE m.id = ts.model_id
|
|
||||||
AND m.tenant_id = 0;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
-- 000015_remove_kb_rerank_model.down.sql
|
|
||||||
-- Reintroduce rerank_model_id column to knowledge_bases table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
ADD COLUMN IF NOT EXISTS rerank_model_id VARCHAR(64);
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
-- 000015_remove_kb_rerank_model.up.sql
|
|
||||||
-- Remove rerank_model_id column from knowledge_bases table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
DROP COLUMN IF EXISTS rerank_model_id;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
-- 000016_add_can_access_all_tenants.down.sql
|
|
||||||
-- Remove can_access_all_tenants column from users table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE users
|
|
||||||
DROP COLUMN IF EXISTS can_access_all_tenants;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
-- 000016_add_can_access_all_tenants.up.sql
|
|
||||||
-- Add can_access_all_tenants column to users table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
ALTER TABLE users
|
|
||||||
ADD COLUMN IF NOT EXISTS can_access_all_tenants BOOLEAN NOT NULL DEFAULT FALSE;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
-- Remove question_generation_config column from knowledge_bases table
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
DROP COLUMN IF EXISTS question_generation_config;
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
-- Add question_generation_config column to knowledge_bases table
|
|
||||||
-- This column stores configuration for AI question generation feature
|
|
||||||
-- When enabled, the system generates questions for document chunks to improve recall
|
|
||||||
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
ADD COLUMN IF NOT EXISTS question_generation_config JSONB NULL ;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
-- Remove summary_status column from knowledges table
|
|
||||||
|
|
||||||
DROP INDEX IF EXISTS idx_knowledges_summary_status;
|
|
||||||
ALTER TABLE knowledges DROP COLUMN IF EXISTS summary_status;
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
-- Add summary_status column to knowledges table
|
|
||||||
-- This column tracks the status of async summary generation task
|
|
||||||
-- Values: none, pending, processing, completed, failed
|
|
||||||
|
|
||||||
ALTER TABLE knowledges ADD COLUMN IF NOT EXISTS summary_status VARCHAR(32) DEFAULT 'none';
|
|
||||||
|
|
||||||
-- Add index for efficient querying
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_knowledges_summary_status ON knowledges(summary_status);
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
-- 000019_add_is_temporary_to_knowledge_bases.down.sql
|
|
||||||
-- Remove is_temporary column from knowledge_bases table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Drop is_temporary column
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
DROP COLUMN IF EXISTS is_temporary;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
-- 000019_add_is_temporary_to_knowledge_bases.up.sql
|
|
||||||
-- Add is_temporary column to knowledge_bases table
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- Add is_temporary column
|
|
||||||
ALTER TABLE knowledge_bases
|
|
||||||
ADD COLUMN IF NOT EXISTS is_temporary BOOLEAN NOT NULL DEFAULT false;
|
|
||||||
|
|
||||||
COMMENT ON COLUMN knowledge_bases.is_temporary IS 'Whether this knowledge base is temporary (ephemeral) and should be hidden from UI';
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
Reference in New Issue
Block a user