Files
AIclinicalresearch/docs/09-架构实施/migration-scripts/002-migrate-platform.sql
HaHafeng 31d555f7bb docs: Update architecture docs with platform infrastructure details
- Add platform infrastructure chapter to frontend-backend architecture design
- Update system architecture document with 6 new infrastructure modules
- Update AI onboarding guide with infrastructure overview
- Link to backend/src/common/README.md for detailed usage guide

Key Updates:
- Storage service (LocalAdapter + OSSAdapter)
- Logging system (Winston + JSON format)
- Cache service (Memory + Redis)
- Async job queue (Memory + Database)
- Health check endpoints
- Monitoring metrics
- Database connection pool
- Environment config management

All modules support zero-code switching between local and cloud environments.

Related: #Platform-Infrastructure
2025-11-17 08:36:10 +08:00

148 lines
4.6 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ========================================
-- 002-migrate-platform.sql
-- ========================================
-- 目的迁移platform_schema用户表
-- 迁移表1个users
-- 预计时间15分钟
-- 作者AI助手
-- 日期2025-11-09
-- ========================================
-- 前置条件:
-- 1. 已执行 001-create-all-10-schemas.sql
-- 2. public.users 表存在且有数据
BEGIN;
-- ========================================
-- 第一步创建platform_schema.users表
-- ========================================
CREATE TABLE IF NOT EXISTS platform_schema.users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
name VARCHAR(255),
avatar_url VARCHAR(500),
role VARCHAR(50) NOT NULL DEFAULT 'user',
status VARCHAR(50) DEFAULT 'active',
kb_quota INT DEFAULT 3,
kb_used INT DEFAULT 0,
trial_ends_at TIMESTAMP,
is_trial BOOLEAN DEFAULT true,
last_login_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- ========================================
-- 第二步:创建索引
-- ========================================
CREATE INDEX IF NOT EXISTS idx_platform_users_email ON platform_schema.users(email);
CREATE INDEX IF NOT EXISTS idx_platform_users_role ON platform_schema.users(role);
CREATE INDEX IF NOT EXISTS idx_platform_users_status ON platform_schema.users(status);
CREATE INDEX IF NOT EXISTS idx_platform_users_created_at ON platform_schema.users(created_at);
-- ========================================
-- 第三步:迁移数据
-- ========================================
-- 从public.users迁移数据到platform_schema.users
INSERT INTO platform_schema.users (
id, email, password, name, avatar_url,
role, status, kb_quota, kb_used,
trial_ends_at, is_trial, last_login_at,
created_at, updated_at
)
SELECT
id, email, password, name, avatar_url,
role, status, kb_quota, kb_used,
trial_ends_at, is_trial, last_login_at,
created_at, updated_at
FROM public.users
ON CONFLICT (id) DO NOTHING; -- 如果已存在则跳过(支持重复执行)
-- ========================================
-- 第四步:数据验证
-- ========================================
DO $$
DECLARE
public_count INTEGER;
platform_count INTEGER;
BEGIN
-- 统计原表数据量
SELECT COUNT(*) INTO public_count FROM public.users;
-- 统计新表数据量
SELECT COUNT(*) INTO platform_count FROM platform_schema.users;
RAISE NOTICE '原表 public.users 数据量:%', public_count;
RAISE NOTICE '新表 platform_schema.users 数据量:%', platform_count;
-- 验证数据一致性
IF public_count = platform_count THEN
RAISE NOTICE '✅ 数据迁移成功:数据量完全一致';
ELSE
RAISE WARNING '⚠️ 警告:数据量不一致!预期 %,实际 %', public_count, platform_count;
END IF;
-- 验证email唯一性
IF (SELECT COUNT(DISTINCT email) FROM platform_schema.users) = platform_count THEN
RAISE NOTICE '✅ Email唯一性校验通过';
ELSE
RAISE WARNING '⚠️ 警告Email存在重复';
END IF;
END $$;
-- ========================================
-- 第五步:对比验证(抽样检查)
-- ========================================
-- 对比前5条数据
SELECT 'public.users' AS source, id, email, name, role, created_at
FROM public.users
ORDER BY created_at DESC
LIMIT 5;
SELECT 'platform_schema.users' AS source, id, email, name, role, created_at
FROM platform_schema.users
ORDER BY created_at DESC
LIMIT 5;
COMMIT;
-- ========================================
-- 执行结果统计(可单独运行)
-- ========================================
SELECT
'platform_schema.users' AS table_name,
COUNT(*) AS total_count,
COUNT(DISTINCT email) AS unique_emails,
COUNT(CASE WHEN role = 'admin' THEN 1 END) AS admin_count,
COUNT(CASE WHEN role = 'user' THEN 1 END) AS user_count,
COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count,
MIN(created_at) AS first_user_date,
MAX(created_at) AS last_user_date
FROM platform_schema.users;
-- ========================================
-- 后续步骤说明
-- ========================================
-- 注意public.users表暂时保留不删除
-- 原因:
-- 1. 其他Schema的表aia, pkb会引用platform_schema.users
-- 2. 所有迁移完成并验证后再决定是否删除public.users
-- 3. 删除前需确保所有外键已更新
-- ========================================