-- ========================================
-- 数据库迁移验证脚本
-- 全面检查10个Schema和数据完整性
-- ========================================

\echo ''
\echo '========================================'
\echo '📊 数据库迁移验证报告'
\echo '========================================'
\echo ''

-- ========================================
-- 1. Schema检查
-- ========================================
\echo '1️⃣ Schema检查'
\echo '----------------------------------------'

SELECT 
    schema_name,
    pg_catalog.obj_description(oid, 'pg_namespace') AS description,
    (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = s.schema_name) as table_count
FROM pg_namespace s
WHERE schema_name IN (
    'platform_schema', 'aia_schema', 'pkb_schema',
    'asl_schema', 'common_schema', 'dc_schema',
    'rvw_schema', 'admin_schema', 'ssa_schema', 'st_schema'
)
ORDER BY schema_name;

\echo ''

-- ========================================
-- 2. 详细表清单
-- ========================================
\echo '2️⃣ 详细表清单'
\echo '----------------------------------------'

-- Platform Schema
\echo '【Platform Schema】'
SELECT 
    schemaname,
    tablename,
    (SELECT COUNT(*) FROM pg_indexes WHERE schemaname = t.schemaname AND tablename = t.tablename) as index_count
FROM pg_tables t
WHERE schemaname = 'platform_schema'
ORDER BY tablename;

\echo ''

-- AIA Schema
\echo '【AIA Schema】'
SELECT 
    schemaname,
    tablename,
    (SELECT COUNT(*) FROM pg_indexes WHERE schemaname = t.schemaname AND tablename = t.tablename) as index_count
FROM pg_tables t
WHERE schemaname = 'aia_schema'
ORDER BY tablename;

\echo ''

-- PKB Schema
\echo '【PKB Schema】'
SELECT 
    schemaname,
    tablename,
    (SELECT COUNT(*) FROM pg_indexes WHERE schemaname = t.schemaname AND tablename = t.tablename) as index_count
FROM pg_tables t
WHERE schemaname = 'pkb_schema'
ORDER BY tablename;

\echo ''

-- ========================================
-- 3. 数据量统计
-- ========================================
\echo '3️⃣ 数据量统计'
\echo '----------------------------------------'

-- Platform Schema
\echo '【Platform Schema】'
SELECT 'users' AS table_name, COUNT(*) AS row_count FROM platform_schema.users;

\echo ''

-- AIA Schema
\echo '【AIA Schema】'
SELECT 'projects' AS table_name, COUNT(*) AS row_count FROM aia_schema.projects
UNION ALL
SELECT 'conversations', COUNT(*) FROM aia_schema.conversations
UNION ALL
SELECT 'messages', COUNT(*) FROM aia_schema.messages
UNION ALL
SELECT 'general_conversations', COUNT(*) FROM aia_schema.general_conversations
UNION ALL
SELECT 'general_messages', COUNT(*) FROM aia_schema.general_messages;

\echo ''

-- PKB Schema
\echo '【PKB Schema】'
SELECT 'knowledge_bases' AS table_name, COUNT(*) AS row_count FROM pkb_schema.knowledge_bases
UNION ALL
SELECT 'documents', COUNT(*) FROM pkb_schema.documents
UNION ALL
SELECT 'batch_tasks', COUNT(*) FROM pkb_schema.batch_tasks
UNION ALL
SELECT 'batch_results', COUNT(*) FROM pkb_schema.batch_results
UNION ALL
SELECT 'task_templates', COUNT(*) FROM pkb_schema.task_templates;

\echo ''

-- ========================================
-- 4. 数据完整性对比（新Schema vs public）
-- ========================================
\echo '4️⃣ 数据完整性对比'
\echo '----------------------------------------'

SELECT 
    'users' AS table_name,
    (SELECT COUNT(*) FROM public.users) AS public_count,
    (SELECT COUNT(*) FROM platform_schema.users) AS new_schema_count,
    CASE 
        WHEN (SELECT COUNT(*) FROM public.users) = (SELECT COUNT(*) FROM platform_schema.users)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END AS status
    
UNION ALL

SELECT 
    'projects',
    (SELECT COUNT(*) FROM public.projects),
    (SELECT COUNT(*) FROM aia_schema.projects),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.projects) = (SELECT COUNT(*) FROM aia_schema.projects)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END
    
UNION ALL

SELECT 
    'conversations',
    (SELECT COUNT(*) FROM public.conversations),
    (SELECT COUNT(*) FROM aia_schema.conversations),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.conversations) = (SELECT COUNT(*) FROM aia_schema.conversations)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END
    
UNION ALL

SELECT 
    'messages',
    (SELECT COUNT(*) FROM public.messages),
    (SELECT COUNT(*) FROM aia_schema.messages),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.messages) = (SELECT COUNT(*) FROM aia_schema.messages)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END
    
UNION ALL

SELECT 
    'knowledge_bases',
    (SELECT COUNT(*) FROM public.knowledge_bases),
    (SELECT COUNT(*) FROM pkb_schema.knowledge_bases),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.knowledge_bases) = (SELECT COUNT(*) FROM pkb_schema.knowledge_bases)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END
    
UNION ALL

SELECT 
    'documents',
    (SELECT COUNT(*) FROM public.documents),
    (SELECT COUNT(*) FROM pkb_schema.documents),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.documents) = (SELECT COUNT(*) FROM pkb_schema.documents)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END;

\echo ''

-- ========================================
-- 5. 外键约束检查
-- ========================================
\echo '5️⃣ 外键约束检查'
\echo '----------------------------------------'

SELECT 
    tc.table_schema AS schema_name,
    tc.table_name,
    tc.constraint_name,
    kcu.column_name,
    ccu.table_schema AS ref_schema,
    ccu.table_name AS ref_table,
    ccu.column_name AS ref_column
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
    ON tc.constraint_name = kcu.constraint_name
    AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage ccu
    ON ccu.constraint_name = tc.constraint_name
    AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
    AND tc.table_schema IN ('platform_schema', 'aia_schema', 'pkb_schema')
ORDER BY tc.table_schema, tc.table_name;

\echo ''

-- ========================================
-- 6. 索引统计
-- ========================================
\echo '6️⃣ 索引统计'
\echo '----------------------------------------'

SELECT 
    schemaname,
    COUNT(*) as index_count
FROM pg_indexes
WHERE schemaname IN ('platform_schema', 'aia_schema', 'pkb_schema')
GROUP BY schemaname
ORDER BY schemaname;

\echo ''

-- ========================================
-- 7. 数据采样（前3条记录）
-- ========================================
\echo '7️⃣ 数据采样验证'
\echo '----------------------------------------'

\echo '【platform_schema.users】'
SELECT id, email, name, role, created_at 
FROM platform_schema.users 
ORDER BY created_at DESC 
LIMIT 3;

\echo ''
\echo '【aia_schema.projects】'
SELECT id, name, research_type, conversation_count, created_at 
FROM aia_schema.projects 
ORDER BY created_at DESC 
LIMIT 3;

\echo ''
\echo '【pkb_schema.knowledge_bases】'
SELECT id, name, file_count, created_at 
FROM pkb_schema.knowledge_bases 
ORDER BY created_at DESC 
LIMIT 3;

\echo ''

-- ========================================
-- 8. 跨Schema引用验证
-- ========================================
\echo '8️⃣ 跨Schema引用完整性'
\echo '----------------------------------------'

-- 检查aia_schema.projects的user_id是否都存在
SELECT 
    'aia_schema.projects → platform_schema.users' AS reference,
    COUNT(*) AS total_records,
    COUNT(u.id) AS valid_references,
    COUNT(*) - COUNT(u.id) AS invalid_count,
    CASE 
        WHEN COUNT(*) = COUNT(u.id) THEN '✅ 全部有效'
        ELSE '❌ 存在无效引用'
    END AS status
FROM aia_schema.projects p
LEFT JOIN platform_schema.users u ON p.user_id = u.id

UNION ALL

-- 检查pkb_schema.knowledge_bases的user_id
SELECT 
    'pkb_schema.knowledge_bases → platform_schema.users',
    COUNT(*),
    COUNT(u.id),
    COUNT(*) - COUNT(u.id),
    CASE 
        WHEN COUNT(*) = COUNT(u.id) THEN '✅ 全部有效'
        ELSE '❌ 存在无效引用'
    END
FROM pkb_schema.knowledge_bases kb
LEFT JOIN platform_schema.users u ON kb.user_id = u.id

UNION ALL

-- 检查aia_schema.conversations → aia_schema.projects
SELECT 
    'aia_schema.conversations → aia_schema.projects',
    COUNT(*),
    COUNT(p.id),
    COUNT(*) - COUNT(p.id),
    CASE 
        WHEN COUNT(*) = COUNT(p.id) OR COUNT(*) = 0 THEN '✅ 全部有效'
        ELSE '❌ 存在无效引用'
    END
FROM aia_schema.conversations c
LEFT JOIN aia_schema.projects p ON c.project_id = p.id
WHERE c.project_id IS NOT NULL;

\echo ''

-- ========================================
-- 9. 最终总结
-- ========================================
\echo '========================================'
\echo '✅ 验证完成！'
\echo '========================================'
\echo ''















