Features: - Migrate 10 agent prompts from hardcoded to database - Add grayscale preview support (DRAFT/ACTIVE distribution) - Implement 3-tier fallback (DB -> Cache -> Hardcoded) - Add version management and rollback capability Files changed: - backend/scripts/migrate-aia-prompts.ts (new migration script) - backend/src/common/prompt/prompt.fallbacks.ts (add AIA fallbacks) - backend/src/modules/aia/services/agentService.ts (integrate PromptService) - backend/src/modules/aia/services/conversationService.ts (pass userId) - backend/src/modules/aia/types/index.ts (fix AgentStage type) Documentation: - docs/03-业务模块/AIA-AI智能问答/06-开发记录/2026-01-18-Prompt管理系统集成.md - docs/02-通用能力层/00-通用能力层清单.md (add FileCard, Prompt management) - docs/00-系统总体设计/00-系统当前状态与开发指南.md (update to v3.6) Prompt codes: - AIA_SCIENTIFIC_QUESTION, AIA_PICO_ANALYSIS, AIA_TOPIC_EVALUATION - AIA_OUTCOME_DESIGN, AIA_CRF_DESIGN, AIA_SAMPLE_SIZE - AIA_PROTOCOL_WRITING, AIA_METHODOLOGY_REVIEW - AIA_PAPER_POLISH, AIA_PAPER_TRANSLATE Tested: Migration script executed, all 10 prompts inserted successfully
262 lines
6.8 KiB
JavaScript
262 lines
6.8 KiB
JavaScript
/**
|
||
* DC模块数据库表检查脚本(使用Prisma)
|
||
*
|
||
* 验证dc_schema和4个表是否已创建
|
||
*/
|
||
|
||
import { PrismaClient } from '@prisma/client';
|
||
|
||
const prisma = new PrismaClient();
|
||
|
||
async function checkDCTables() {
|
||
try {
|
||
console.log('');
|
||
console.log('============================================================');
|
||
console.log('[DC模块] 数据库表检查');
|
||
console.log('============================================================');
|
||
console.log('');
|
||
console.log('✅ Prisma连接初始化成功');
|
||
console.log('');
|
||
|
||
// 1. 检查dc_schema是否存在
|
||
console.log('📋 检查1: dc_schema是否存在?');
|
||
const schemaResult = await prisma.$queryRawUnsafe(`
|
||
SELECT schema_name
|
||
FROM information_schema.schemata
|
||
WHERE schema_name = 'dc_schema'
|
||
`);
|
||
|
||
if (schemaResult.length === 0) {
|
||
console.log('❌ dc_schema 不存在!');
|
||
console.log('');
|
||
console.log('💡 解决方案:');
|
||
console.log(' cd backend');
|
||
console.log(' npx prisma db push');
|
||
console.log('');
|
||
await prisma.$disconnect();
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log('✅ dc_schema 存在');
|
||
console.log('');
|
||
|
||
// 2. 检查4个表是否存在
|
||
console.log('📋 检查2: DC模块的4个表是否存在?');
|
||
console.log('');
|
||
|
||
const tables = [
|
||
{ name: 'dc_health_checks', display: '健康检查表', model: 'dCHealthCheck' },
|
||
{ name: 'dc_templates', display: '预设模板表', model: 'dCTemplate' },
|
||
{ name: 'dc_extraction_tasks', display: '提取任务表', model: 'dCExtractionTask' },
|
||
{ name: 'dc_extraction_items', display: '提取明细表', model: 'dCExtractionItem' },
|
||
];
|
||
|
||
let allTablesExist = true;
|
||
const missingTables = [];
|
||
const tableCounts = {};
|
||
|
||
for (const table of tables) {
|
||
try {
|
||
const tableResult = await prisma.$queryRawUnsafe(`
|
||
SELECT table_name
|
||
FROM information_schema.tables
|
||
WHERE table_schema = 'dc_schema'
|
||
AND table_name = '${table.name}'
|
||
`);
|
||
|
||
if (tableResult.length > 0) {
|
||
// 获取行数
|
||
const countResult = await prisma.$queryRawUnsafe(`
|
||
SELECT COUNT(*) as count FROM dc_schema.${table.name}
|
||
`);
|
||
const count = Number(countResult[0].count);
|
||
tableCounts[table.name] = count;
|
||
|
||
console.log(` ✅ ${table.display} (${table.name})`);
|
||
console.log(` 记录数: ${count} 条`);
|
||
} else {
|
||
console.log(` ❌ ${table.display} (${table.name}): 不存在`);
|
||
allTablesExist = false;
|
||
missingTables.push(table.name);
|
||
}
|
||
} catch (error) {
|
||
console.log(` ❌ ${table.display} (${table.name}): 检查失败 - ${error.message}`);
|
||
allTablesExist = false;
|
||
missingTables.push(table.name);
|
||
}
|
||
}
|
||
|
||
console.log('');
|
||
|
||
// 3. 检查dc_templates是否有预设数据
|
||
if (allTablesExist) {
|
||
console.log('📋 检查3: dc_templates预设模板是否存在?');
|
||
const templateCount = tableCounts['dc_templates'];
|
||
|
||
if (templateCount === 0) {
|
||
console.log('⚠️ dc_templates表为空(没有预设模板)');
|
||
console.log('');
|
||
console.log('💡 需要启动后端服务初始化预设模板:');
|
||
console.log(' cd backend');
|
||
console.log(' npm run dev');
|
||
console.log(' (启动时会自动seed 3个预设模板)');
|
||
} else {
|
||
console.log(`✅ dc_templates已有 ${templateCount} 个预设模板`);
|
||
|
||
// 列出模板
|
||
try {
|
||
const templates = await prisma.$queryRawUnsafe(`
|
||
SELECT disease_type, report_type, display_name
|
||
FROM dc_schema.dc_templates
|
||
ORDER BY created_at
|
||
`);
|
||
|
||
if (templates.length > 0) {
|
||
console.log('');
|
||
console.log(' 预设模板列表:');
|
||
templates.forEach((t, i) => {
|
||
console.log(` ${i + 1}. ${t.display_name} (${t.disease_type}/${t.report_type})`);
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.log(' ⚠️ 无法获取模板详情');
|
||
}
|
||
}
|
||
console.log('');
|
||
}
|
||
|
||
// 4. 总结
|
||
console.log('============================================================');
|
||
console.log('[总结]');
|
||
console.log('============================================================');
|
||
console.log('');
|
||
|
||
if (allTablesExist) {
|
||
console.log('🎉 恭喜!DC模块数据库表已全部创建!');
|
||
console.log('');
|
||
console.log('✅ dc_schema: 存在');
|
||
console.log('✅ 4个数据表: 全部存在');
|
||
console.log('');
|
||
console.log('📊 数据统计:');
|
||
console.log(` - dc_health_checks: ${tableCounts['dc_health_checks']} 条`);
|
||
console.log(` - dc_templates: ${tableCounts['dc_templates']} 条`);
|
||
console.log(` - dc_extraction_tasks: ${tableCounts['dc_extraction_tasks']} 条`);
|
||
console.log(` - dc_extraction_items: ${tableCounts['dc_extraction_items']} 条`);
|
||
console.log('');
|
||
console.log('📌 下一步:');
|
||
if (tableCounts['dc_templates'] === 0) {
|
||
console.log(' 1. ⚠️ 启动后端初始化预设模板(npm run dev)');
|
||
console.log(' 2. 然后可以开始前端开发!');
|
||
} else {
|
||
console.log(' ✅ 可以开始前端开发了!');
|
||
}
|
||
console.log('');
|
||
|
||
await prisma.$disconnect();
|
||
process.exit(0);
|
||
} else {
|
||
console.log('⚠️ DC模块数据库表未完全创建');
|
||
console.log('');
|
||
console.log('❌ 缺少以下表:');
|
||
missingTables.forEach(t => console.log(` - ${t}`));
|
||
console.log('');
|
||
console.log('💡 解决方案:');
|
||
console.log(' cd backend');
|
||
console.log(' npx prisma db push');
|
||
console.log('');
|
||
|
||
await prisma.$disconnect();
|
||
process.exit(1);
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('');
|
||
console.error('❌ 检查失败:', error.message);
|
||
console.error('');
|
||
|
||
if (error.message.includes('connect') || error.message.includes('ECONNREFUSED')) {
|
||
console.error('💡 数据库连接失败,请确认:');
|
||
console.error(' 1. PostgreSQL服务是否已启动?');
|
||
console.error(' 2. DATABASE_URL环境变量是否正确?');
|
||
console.error(' 当前: ' + (process.env.DATABASE_URL || '未设置'));
|
||
console.error('');
|
||
console.error(' 期望格式:');
|
||
console.error(' postgresql://postgres:postgres123@localhost:5432/ai_clinical_research');
|
||
}
|
||
|
||
console.error('');
|
||
console.error('详细错误:');
|
||
console.error(error);
|
||
console.error('');
|
||
|
||
await prisma.$disconnect().catch(() => {});
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 执行检查
|
||
checkDCTables();
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|