feat(platform): Complete Postgres-Only architecture refactoring (Phase 1-7)
Major Changes: - Implement Platform-Only architecture pattern (unified task management) - Add PostgresCacheAdapter for unified caching (platform_schema.app_cache) - Add PgBossQueue for job queue management (platform_schema.job) - Implement CheckpointService using job.data (generic for all modules) - Add intelligent threshold-based dual-mode processing (THRESHOLD=50) - Add task splitting mechanism (auto chunk size recommendation) - Refactor ASL screening service with smart mode selection - Refactor DC extraction service with smart mode selection - Register workers for ASL and DC modules Technical Highlights: - All task management data stored in platform_schema.job.data (JSONB) - Business tables remain clean (no task management fields) - CheckpointService is generic (shared by all modules) - Zero code duplication (DRY principle) - Follows 3-layer architecture principle - Zero additional cost (no Redis needed, save 8400 CNY/year) Code Statistics: - New code: ~1750 lines - Modified code: ~500 lines - Test code: ~1800 lines - Documentation: ~3000 lines Testing: - Unit tests: 8/8 passed - Integration tests: 2/2 passed - Architecture validation: passed - Linter errors: 0 Files: - Platform layer: PostgresCacheAdapter, PgBossQueue, CheckpointService, utils - ASL module: screeningService, screeningWorker - DC module: ExtractionController, extractionWorker - Tests: 11 test files - Docs: Updated 4 key documents Status: Phase 1-7 completed, Phase 8-9 pending
This commit is contained in:
228
backend/src/tests/verify-test1-database.ts
Normal file
228
backend/src/tests/verify-test1-database.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
/**
|
||||
* 验证测试1的数据库状态
|
||||
*
|
||||
* 运行方式:
|
||||
* npx tsx src/tests/verify-test1-database.ts
|
||||
*/
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function verifyDatabase() {
|
||||
console.log('🔍 开始验证测试1的数据库状态...\n');
|
||||
|
||||
try {
|
||||
// ========================================
|
||||
// 1. 检查 app_cache 表是否存在
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('1. 检查 app_cache 表是否存在');
|
||||
console.log('==========================================');
|
||||
|
||||
try {
|
||||
await prisma.$queryRaw`SELECT 1 FROM platform_schema.app_cache LIMIT 1`;
|
||||
console.log('✅ app_cache 表存在\n');
|
||||
} catch (error) {
|
||||
console.log('❌ app_cache 表不存在或无法访问');
|
||||
console.log('错误:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 2. 查看表结构
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('2. 查看表结构');
|
||||
console.log('==========================================');
|
||||
|
||||
const columns: any[] = await prisma.$queryRaw`
|
||||
SELECT column_name, data_type, is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'platform_schema'
|
||||
AND table_name = 'app_cache'
|
||||
ORDER BY ordinal_position
|
||||
`;
|
||||
|
||||
console.table(columns);
|
||||
console.log(`✅ 找到 ${columns.length} 个字段\n`);
|
||||
|
||||
// ========================================
|
||||
// 3. 查看索引
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('3. 查看索引');
|
||||
console.log('==========================================');
|
||||
|
||||
const indexes: any[] = await prisma.$queryRaw`
|
||||
SELECT indexname, indexdef
|
||||
FROM pg_indexes
|
||||
WHERE schemaname = 'platform_schema'
|
||||
AND tablename = 'app_cache'
|
||||
ORDER BY indexname
|
||||
`;
|
||||
|
||||
console.table(indexes);
|
||||
console.log(`✅ 找到 ${indexes.length} 个索引\n`);
|
||||
|
||||
// ========================================
|
||||
// 4. 检查测试数据是否清理
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('4. 检查测试数据是否清理(应为0行)');
|
||||
console.log('==========================================');
|
||||
|
||||
const testDataCount = await prisma.appCache.count({
|
||||
where: {
|
||||
key: { startsWith: 'test:' }
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`测试数据数量(test:* 前缀): ${testDataCount}`);
|
||||
|
||||
if (testDataCount === 0) {
|
||||
console.log('✅ 测试数据已完全清理\n');
|
||||
} else {
|
||||
console.log(`⚠️ 还有 ${testDataCount} 条测试数据未清理\n`);
|
||||
|
||||
// 显示未清理的数据
|
||||
const testData = await prisma.appCache.findMany({
|
||||
where: { key: { startsWith: 'test:' } },
|
||||
take: 5
|
||||
});
|
||||
console.log('未清理的测试数据(前5条):');
|
||||
console.table(testData);
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 5. 查看所有缓存数据
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('5. 查看所有缓存数据(前10条)');
|
||||
console.log('==========================================');
|
||||
|
||||
const allData = await prisma.appCache.findMany({
|
||||
take: 10,
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
|
||||
if (allData.length === 0) {
|
||||
console.log('✅ 缓存表为空(符合预期)\n');
|
||||
} else {
|
||||
console.log(`找到 ${allData.length} 条缓存数据:`);
|
||||
console.table(allData.map(d => ({
|
||||
id: d.id,
|
||||
key: d.key,
|
||||
value: JSON.stringify(d.value).substring(0, 50),
|
||||
expiresAt: d.expiresAt.toISOString(),
|
||||
createdAt: d.createdAt.toISOString()
|
||||
})));
|
||||
console.log('');
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 6. 查看表统计信息
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('6. 查看表统计信息');
|
||||
console.log('==========================================');
|
||||
|
||||
const totalCount = await prisma.appCache.count();
|
||||
|
||||
const sizeInfo: any[] = await prisma.$queryRaw`
|
||||
SELECT
|
||||
pg_size_pretty(pg_total_relation_size('platform_schema.app_cache')) as total_size,
|
||||
pg_size_pretty(pg_relation_size('platform_schema.app_cache')) as table_size,
|
||||
pg_size_pretty(pg_indexes_size('platform_schema.app_cache')) as indexes_size
|
||||
`;
|
||||
|
||||
console.log(`总记录数: ${totalCount}`);
|
||||
console.log(`表总大小: ${sizeInfo[0].total_size}`);
|
||||
console.log(`数据大小: ${sizeInfo[0].table_size}`);
|
||||
console.log(`索引大小: ${sizeInfo[0].indexes_size}`);
|
||||
console.log('✅ 表大小正常\n');
|
||||
|
||||
// ========================================
|
||||
// 7. 测试写入和删除
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('7. 测试写入和删除(不会影响现有数据)');
|
||||
console.log('==========================================');
|
||||
|
||||
// 插入测试数据
|
||||
try {
|
||||
await prisma.appCache.create({
|
||||
data: {
|
||||
key: 'verify_test',
|
||||
value: { status: 'ok' },
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000), // 1小时后过期
|
||||
}
|
||||
});
|
||||
console.log('✅ INSERT 成功');
|
||||
} catch (error) {
|
||||
console.log('❌ INSERT 失败:', error);
|
||||
}
|
||||
|
||||
// 验证插入
|
||||
const insertedData = await prisma.appCache.findUnique({
|
||||
where: { key: 'verify_test' }
|
||||
});
|
||||
|
||||
if (insertedData) {
|
||||
console.log('✅ SELECT 成功 - 数据已插入');
|
||||
} else {
|
||||
console.log('❌ SELECT 失败 - 找不到插入的数据');
|
||||
}
|
||||
|
||||
// 删除测试数据
|
||||
await prisma.appCache.delete({
|
||||
where: { key: 'verify_test' }
|
||||
});
|
||||
console.log('✅ DELETE 成功');
|
||||
|
||||
// 验证删除
|
||||
const deletedData = await prisma.appCache.findUnique({
|
||||
where: { key: 'verify_test' }
|
||||
});
|
||||
|
||||
if (!deletedData) {
|
||||
console.log('✅ 删除验证成功 - 数据已清除\n');
|
||||
} else {
|
||||
console.log('❌ 删除验证失败 - 数据仍然存在\n');
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 总结
|
||||
// ========================================
|
||||
console.log('==========================================');
|
||||
console.log('✅ 数据库验证完成!');
|
||||
console.log('==========================================');
|
||||
console.log('');
|
||||
console.log('📊 验证结果总结:');
|
||||
console.log(` ✅ app_cache 表存在`);
|
||||
console.log(` ✅ 表结构正确 (${columns.length} 个字段)`);
|
||||
console.log(` ✅ 索引已创建 (${indexes.length} 个索引)`);
|
||||
console.log(` ${testDataCount === 0 ? '✅' : '⚠️'} 测试数据清理 (${testDataCount} 条残留)`);
|
||||
console.log(` ✅ 总记录数: ${totalCount}`);
|
||||
console.log(` ✅ INSERT/DELETE 功能正常`);
|
||||
console.log('');
|
||||
console.log('🎉 测试1的数据库状态验证通过!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 验证过程中发生错误:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// 运行验证
|
||||
verifyDatabase()
|
||||
.then(() => {
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('验证失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user