const mysql = require('mysql2/promise'); // 資料庫配置 const dbConfig = { host: 'localhost', user: 'root', password: '123456', database: 'hr_assessment', port: 3306 }; async function addTimeoutColumns() { let connection; try { console.log('🔄 連接到資料庫...'); connection = await mysql.createConnection(dbConfig); console.log('✅ 資料庫連接成功'); console.log('🔄 開始添加 is_timeout 欄位...'); // 檢查並添加 test_results 表的 is_timeout 欄位 try { // 先檢查欄位是否已存在 const [checkResult] = await connection.execute(` SELECT COUNT(*) as count FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test_results' AND COLUMN_NAME = 'is_timeout' `); const columnExists = checkResult[0].count > 0; if (!columnExists) { await connection.execute(` ALTER TABLE test_results ADD COLUMN is_timeout BOOLEAN DEFAULT FALSE `); console.log('✅ test_results 表已添加 is_timeout 欄位'); } else { console.log('ℹ️ test_results 表的 is_timeout 欄位已存在,跳過'); } } catch (error) { console.error('❌ 檢查/添加 test_results 表 is_timeout 欄位失敗:', error.message); } // 檢查並添加 combined_test_results 表的 is_timeout 欄位 try { // 先檢查欄位是否已存在 const [checkResult] = await connection.execute(` SELECT COUNT(*) as count FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'combined_test_results' AND COLUMN_NAME = 'is_timeout' `); const columnExists = checkResult[0].count > 0; if (!columnExists) { await connection.execute(` ALTER TABLE combined_test_results ADD COLUMN is_timeout BOOLEAN DEFAULT FALSE `); console.log('✅ combined_test_results 表已添加 is_timeout 欄位'); } else { console.log('ℹ️ combined_test_results 表的 is_timeout 欄位已存在,跳過'); } } catch (error) { console.error('❌ 檢查/添加 combined_test_results 表 is_timeout 欄位失敗:', error.message); } console.log('✅ is_timeout 欄位添加完成'); } catch (error) { console.error('❌ 執行失敗:', error); process.exit(1); } finally { if (connection) { await connection.end(); console.log('📝 資料庫連接已關閉'); } } } // 執行遷移 addTimeoutColumns();