/** * 验证测试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); });