Files
AIclinicalresearch/docs/09-架构实施/migration-scripts/002-migrate-platform.sql
HaHafeng e3e7e028e8 feat(platform): Complete platform infrastructure implementation and verification
Platform Infrastructure - 8 Core Modules Completed:
- Storage Service (LocalAdapter + OSSAdapter stub)
- Logging System (Winston + JSON format)
- Cache Service (MemoryCache + Redis stub)
- Async Job Queue (MemoryQueue + DatabaseQueue stub)
- Health Check Endpoints (liveness/readiness/detailed)
- Database Connection Pool (with Serverless optimization)
- Environment Configuration Management
- Monitoring Metrics (DB connections/memory/API)

Key Features:
- Adapter Pattern for zero-code environment switching
- Full backward compatibility with legacy modules
- 100% test coverage (all 8 modules verified)
- Complete documentation (11 docs updated)

Technical Improvements:
- Fixed duplicate /health route registration issue
- Fixed TypeScript interface export (export type)
- Installed winston dependency
- Added structured logging with context support
- Implemented graceful shutdown for Serverless
- Added connection pool optimization for SAE

Documentation Updates:
- Platform infrastructure planning (04-骞冲彴鍩虹璁炬柦瑙勫垝.md)
- Implementation report (2025-11-17-骞冲彴鍩虹璁炬柦瀹炴柦瀹屾垚鎶ュ憡.md)
- Verification report (2025-11-17-骞冲彴鍩虹璁炬柦楠岃瘉鎶ュ憡.md)
- Git commit guidelines (06-Git鎻愪氦瑙勮寖.md) - Added commit frequency rules
- Updated 3 core architecture documents

Code Statistics:
- New code: 2,532 lines
- New files: 22
- Updated files: 130+
- Test pass rate: 100% (8/8 modules)

Deployment Readiness:
- Local environment: 鉁?Ready
- Cloud environment: 馃攧 Needs OSS/Redis dependencies

Next Steps:
- Ready to start ASL module development
- Can directly use storage/logger/cache/jobQueue

Tested: Local verification 100% passed
Related: #Platform-Infrastructure
2025-11-18 08:00:41 +08:00

149 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. 删除前需确保所有外键已更新
-- ========================================