99 lines
3.4 KiB
JavaScript
99 lines
3.4 KiB
JavaScript
const mysql = require('mysql2/promise');
|
||
|
||
// 資料庫配置
|
||
const dbConfig = {
|
||
host: process.env.DB_HOST || 'localhost',
|
||
user: process.env.DB_USER || 'root',
|
||
password: process.env.DB_PASSWORD || '',
|
||
database: process.env.DB_NAME || 'ai_scoring_app',
|
||
timezone: '+08:00',
|
||
};
|
||
|
||
async function updateEvaluationTable() {
|
||
let connection;
|
||
|
||
try {
|
||
console.log('🔗 連接到資料庫...');
|
||
connection = await mysql.createConnection(dbConfig);
|
||
console.log('✅ 資料庫連接成功');
|
||
|
||
// 檢查欄位是否已存在
|
||
console.log('🔍 檢查 evaluations 表結構...');
|
||
const [columns] = await connection.execute(`
|
||
SELECT COLUMN_NAME
|
||
FROM INFORMATION_SCHEMA.COLUMNS
|
||
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'evaluations'
|
||
`, [dbConfig.database]);
|
||
|
||
const existingColumns = columns.map(col => col.COLUMN_NAME);
|
||
console.log('📋 現有欄位:', existingColumns);
|
||
|
||
// 添加新欄位
|
||
const newColumns = [
|
||
{ name: 'performance_status', type: 'varchar(50) DEFAULT NULL COMMENT \'表現狀況\'' },
|
||
{ name: 'recommended_stars', type: 'int(11) DEFAULT NULL COMMENT \'推薦等級(星星數量)\'' },
|
||
{ name: 'excellent_items', type: 'int(11) DEFAULT NULL COMMENT \'優秀項目數量\'' },
|
||
{ name: 'improvement_items', type: 'int(11) DEFAULT NULL COMMENT \'待改進項目數量\'' }
|
||
];
|
||
|
||
for (const column of newColumns) {
|
||
if (!existingColumns.includes(column.name)) {
|
||
console.log(`➕ 添加欄位: ${column.name}`);
|
||
await connection.execute(`
|
||
ALTER TABLE evaluations
|
||
ADD COLUMN \`${column.name}\` ${column.type}
|
||
AFTER \`grade\`
|
||
`);
|
||
console.log(`✅ 欄位 ${column.name} 添加成功`);
|
||
} else {
|
||
console.log(`⚠️ 欄位 ${column.name} 已存在,跳過`);
|
||
}
|
||
}
|
||
|
||
// 驗證更新結果
|
||
console.log('🔍 驗證更新結果...');
|
||
const [updatedColumns] = await connection.execute(`
|
||
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT
|
||
FROM INFORMATION_SCHEMA.COLUMNS
|
||
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'evaluations'
|
||
ORDER BY ORDINAL_POSITION
|
||
`, [dbConfig.database]);
|
||
|
||
console.log('📊 更新後的 evaluations 表結構:');
|
||
updatedColumns.forEach(col => {
|
||
console.log(` - ${col.COLUMN_NAME}: ${col.DATA_TYPE} ${col.IS_NULLABLE === 'YES' ? 'NULL' : 'NOT NULL'} ${col.COLUMN_DEFAULT ? `DEFAULT ${col.COLUMN_DEFAULT}` : ''} ${col.COLUMN_COMMENT ? `(${col.COLUMN_COMMENT})` : ''}`);
|
||
});
|
||
|
||
console.log('\n🎉 evaluations 表更新完成!');
|
||
console.log('📝 新增的欄位:');
|
||
console.log(' - performance_status: 表現狀況');
|
||
console.log(' - recommended_stars: 推薦等級(星星數量)');
|
||
console.log(' - excellent_items: 優秀項目數量');
|
||
console.log(' - improvement_items: 待改進項目數量');
|
||
|
||
} catch (error) {
|
||
console.error('❌ 更新過程中發生錯誤:', error);
|
||
throw error;
|
||
} finally {
|
||
if (connection) {
|
||
await connection.end();
|
||
console.log('🔌 資料庫連接已關閉');
|
||
}
|
||
}
|
||
}
|
||
|
||
// 執行更新
|
||
if (require.main === module) {
|
||
updateEvaluationTable()
|
||
.then(() => {
|
||
console.log('✅ 腳本執行完成');
|
||
process.exit(0);
|
||
})
|
||
.catch((error) => {
|
||
console.error('❌ 腳本執行失敗:', error);
|
||
process.exit(1);
|
||
});
|
||
}
|
||
|
||
module.exports = { updateEvaluationTable };
|