feat(knowledge): add multi-space platform schema

This commit is contained in:
2026-07-16 10:21:35 +08:00
parent 5a131b57ef
commit 735e9a78c1
3 changed files with 281 additions and 6 deletions
@@ -0,0 +1,154 @@
-- AIHR 多租户多知识空间统一问答平台迁移(MySQL 8.x)。
-- 只补结构和 legacy 编码,不移动既有附件、片段或 OSS 数据;可重复执行。
SET @aihr_space_code_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_info' AND COLUMN_NAME = 'code'
);
SET @aihr_space_code_ddl := IF(
@aihr_space_code_exists = 0,
'ALTER TABLE aihr_knowledge_info ADD COLUMN `code` varchar(100) DEFAULT NULL COMMENT ''租户内稳定空间编码'' AFTER `name`',
'SELECT 1'
);
PREPARE aihr_space_code_stmt FROM @aihr_space_code_ddl;
EXECUTE aihr_space_code_stmt;
DEALLOCATE PREPARE aihr_space_code_stmt;
UPDATE aihr_knowledge_info SET code = CONCAT('legacy_', id) WHERE code IS NULL OR code = '';
ALTER TABLE aihr_knowledge_info MODIFY COLUMN `code` varchar(100) NOT NULL COMMENT '租户内稳定空间编码';
SET @aihr_space_type_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_info' AND COLUMN_NAME = 'space_type'
);
SET @aihr_space_type_ddl := IF(
@aihr_space_type_exists = 0,
'ALTER TABLE aihr_knowledge_info ADD COLUMN `space_type` varchar(30) NOT NULL DEFAULT ''BUSINESS'' COMMENT ''PUBLIC/BUSINESS/MANAGEMENT/EXTERNAL'' AFTER `code`',
'SELECT 1'
);
PREPARE aihr_space_type_stmt FROM @aihr_space_type_ddl;
EXECUTE aihr_space_type_stmt;
DEALLOCATE PREPARE aihr_space_type_stmt;
SET @aihr_space_sensitivity_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_info' AND COLUMN_NAME = 'sensitivity_level'
);
SET @aihr_space_sensitivity_ddl := IF(
@aihr_space_sensitivity_exists = 0,
'ALTER TABLE aihr_knowledge_info ADD COLUMN `sensitivity_level` varchar(30) NOT NULL DEFAULT ''INTERNAL'' COMMENT ''PUBLIC/INTERNAL/CONFIDENTIAL'' AFTER `space_type`',
'SELECT 1'
);
PREPARE aihr_space_sensitivity_stmt FROM @aihr_space_sensitivity_ddl;
EXECUTE aihr_space_sensitivity_stmt;
DEALLOCATE PREPARE aihr_space_sensitivity_stmt;
SET @aihr_space_status_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_info' AND COLUMN_NAME = 'status'
);
SET @aihr_space_status_ddl := IF(
@aihr_space_status_exists = 0,
'ALTER TABLE aihr_knowledge_info ADD COLUMN `status` varchar(20) NOT NULL DEFAULT ''ACTIVE'' COMMENT ''DRAFT/ACTIVE/DISABLED'' AFTER `sensitivity_level`',
'SELECT 1'
);
PREPARE aihr_space_status_stmt FROM @aihr_space_status_ddl;
EXECUTE aihr_space_status_stmt;
DEALLOCATE PREPARE aihr_space_status_stmt;
SET @aihr_space_code_index_exists := (
SELECT COUNT(*) FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_info' AND INDEX_NAME = 'uk_aihr_knowledge_info_code'
);
SET @aihr_space_code_index_ddl := IF(
@aihr_space_code_index_exists = 0,
'ALTER TABLE aihr_knowledge_info ADD UNIQUE KEY `uk_aihr_knowledge_info_code` (`tenant_id`, `code`)',
'SELECT 1'
);
PREPARE aihr_space_code_index_stmt FROM @aihr_space_code_index_ddl;
EXECUTE aihr_space_code_index_stmt;
DEALLOCATE PREPARE aihr_space_code_index_stmt;
SET @aihr_upload_spaces_exists := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'aihr_knowledge_upload_item' AND COLUMN_NAME = 'space_codes_json'
);
SET @aihr_upload_spaces_ddl := IF(
@aihr_upload_spaces_exists = 0,
'ALTER TABLE aihr_knowledge_upload_item ADD COLUMN `space_codes_json` json DEFAULT NULL COMMENT ''目标知识空间编码数组'' AFTER `category`',
'SELECT 1'
);
PREPARE aihr_upload_spaces_stmt FROM @aihr_upload_spaces_ddl;
EXECUTE aihr_upload_spaces_stmt;
DEALLOCATE PREPARE aihr_upload_spaces_stmt;
CREATE TABLE IF NOT EXISTS `aihr_knowledge_space_grant` (
`id` bigint NOT NULL AUTO_INCREMENT,
`tenant_id` varchar(20) NOT NULL,
`knowledge_id` bigint NOT NULL,
`principal_type` varchar(20) NOT NULL,
`principal_value` varchar(100) NOT NULL,
`permission` varchar(20) NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'ACTIVE',
`create_by` bigint DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_by` bigint DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_aihr_space_grant` (`tenant_id`, `knowledge_id`, `principal_type`, `principal_value`, `permission`),
KEY `idx_aihr_space_grant_principal` (`tenant_id`, `principal_type`, `principal_value`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='知识空间主体授权';
CREATE TABLE IF NOT EXISTS `aihr_knowledge_app` (
`id` bigint NOT NULL AUTO_INCREMENT,
`tenant_id` varchar(20) NOT NULL,
`app_code` varchar(64) NOT NULL,
`app_name` varchar(100) NOT NULL,
`auth_type` varchar(20) NOT NULL,
`internal_client_key` varchar(64) DEFAULT NULL,
`token_hash` varchar(64) DEFAULT NULL,
`status` varchar(20) NOT NULL DEFAULT 'ACTIVE',
`rate_limit_per_minute` int NOT NULL DEFAULT 60,
`expires_time` datetime DEFAULT NULL,
`last_used_time` datetime DEFAULT NULL,
`create_by` bigint DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_by` bigint DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_aihr_knowledge_app_code` (`app_code`),
UNIQUE KEY `uk_aihr_knowledge_app_client` (`tenant_id`, `auth_type`, `internal_client_key`),
KEY `idx_aihr_knowledge_app_tenant_status` (`tenant_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='知识问答调用应用';
CREATE TABLE IF NOT EXISTS `aihr_knowledge_app_space` (
`id` bigint NOT NULL AUTO_INCREMENT,
`tenant_id` varchar(20) NOT NULL,
`app_id` bigint NOT NULL,
`knowledge_id` bigint NOT NULL,
`create_by` bigint DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_aihr_app_space` (`tenant_id`, `app_id`, `knowledge_id`),
KEY `idx_aihr_app_space_knowledge` (`tenant_id`, `knowledge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='调用应用与知识空间授权';
CREATE TABLE IF NOT EXISTS `aihr_knowledge_query_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`request_id` varchar(64) NOT NULL,
`tenant_id` varchar(20) NOT NULL,
`app_id` bigint NOT NULL,
`user_id` bigint DEFAULT NULL,
`question_hash` varchar(64) NOT NULL,
`used_space_codes_json` json DEFAULT NULL,
`source_types_json` json DEFAULT NULL,
`status` varchar(30) NOT NULL,
`latency_ms` bigint NOT NULL DEFAULT 0,
`model_name` varchar(100) DEFAULT NULL,
`prompt_version` varchar(100) DEFAULT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_aihr_query_request` (`request_id`),
KEY `idx_aihr_query_tenant_app` (`tenant_id`, `app_id`, `create_time`),
KEY `idx_aihr_query_status` (`tenant_id`, `status`, `create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='知识问答最小化审计日志';