// ===================================================== // 遷移到動態評分系統的腳本 // ===================================================== const mysql = require('mysql2/promise'); const fs = require('fs'); async function migrateToDynamicScoring() { console.log('🚀 開始遷移到動態評分系統...\n'); let connection; try { // 連接數據庫 connection = await mysql.createConnection({ host: 'mysql.theaken.com', port: 33306, user: 'AI_Platform', password: 'Aa123456', database: 'db_AI_Platform' }); console.log('✅ 數據庫連接成功'); // 讀取 SQL 腳本 const sqlScript = fs.readFileSync('scripts/redesign-scoring-database.sql', 'utf8'); // 分割 SQL 語句 const statements = sqlScript .split(';') .map(stmt => stmt.trim()) .filter(stmt => stmt.length > 0 && !stmt.startsWith('--')); console.log(`📝 準備執行 ${statements.length} 個 SQL 語句...`); // 逐個執行 SQL 語句 for (let i = 0; i < statements.length; i++) { const statement = statements[i]; console.log(`\n🔄 執行語句 ${i + 1}/${statements.length}:`); console.log(statement.substring(0, 100) + (statement.length > 100 ? '...' : '')); try { await connection.execute(statement); console.log('✅ 執行成功'); } catch (error) { console.log('⚠️ 執行警告:', error.message); // 繼續執行其他語句 } } console.log('\n🎉 數據庫遷移完成!'); console.log('\n📊 新表結構:'); console.log('- judge_scores: 主評分記錄表'); console.log('- judge_score_details: 評分項目詳情表'); console.log('- app_judge_scores: 向後兼容視圖'); } catch (error) { console.error('❌ 遷移失敗:', error.message); } finally { if (connection) { await connection.end(); console.log('\n✅ 數據庫連接已關閉'); } } } // 執行遷移 migrateToDynamicScoring();