Files
hr-assessment-system/lib/database/migrations/add-timeout-columns.ts

72 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { executeQuery } from '../connection'
// 添加 is_timeout 欄位到現有資料庫表
export async function addTimeoutColumns(): Promise<void> {
try {
console.log('🔄 開始添加 is_timeout 欄位...')
// 檢查並添加 test_results 表的 is_timeout 欄位
try {
// 先檢查欄位是否已存在
const checkColumnQuery = `
SELECT COUNT(*) as count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'test_results'
AND COLUMN_NAME = 'is_timeout'
`
const result = await executeQuery(checkColumnQuery)
const columnExists = result[0]?.count > 0
if (!columnExists) {
await executeQuery(`
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: any) {
console.error('❌ 檢查/添加 test_results 表 is_timeout 欄位失敗:', error.message)
}
// 檢查並添加 combined_test_results 表的 is_timeout 欄位
try {
// 先檢查欄位是否已存在
const checkColumnQuery = `
SELECT COUNT(*) as count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'combined_test_results'
AND COLUMN_NAME = 'is_timeout'
`
const result = await executeQuery(checkColumnQuery)
const columnExists = result[0]?.count > 0
if (!columnExists) {
await executeQuery(`
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: any) {
console.error('❌ 檢查/添加 combined_test_results 表 is_timeout 欄位失敗:', error.message)
}
console.log('✅ is_timeout 欄位添加完成')
} catch (error) {
console.error('❌ 添加 is_timeout 欄位失敗:', error)
throw error
}
}
// 執行遷移
if (typeof window === 'undefined') {
// 只在伺服器端執行
addTimeoutColumns().catch(console.error)
}