Files
ExecuBoard/scripts/test-user-update.js
2025-08-01 01:21:37 +08:00

61 lines
1.8 KiB
JavaScript
Raw Permalink 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.

require('dotenv').config({ path: '.env.local' })
const mysql = require('mysql2/promise')
async function testUserUpdate() {
console.log('🧪 測試用戶更新功能...')
// 連接資料庫
const dbConfig = {
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
database: process.env.DB_DATABASE,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
}
console.log('🔗 連接到資料庫...')
const db = mysql.createPool(dbConfig)
try {
// 1. 檢查用戶是否存在
console.log('1⃣ 檢查用戶 user_001 是否存在...')
const [users] = await db.execute('SELECT * FROM users WHERE id = ?', ['user_001'])
if (users.length === 0) {
console.log('❌ 用戶 user_001 不存在')
return
}
console.log('✅ 用戶存在:', users[0])
// 2. 測試更新
console.log('2⃣ 測試更新用戶...')
const updateData = {
name: '陳雅雯測試更新',
email: 'chen@company.com',
role: 'manager'
}
const now = new Date().toISOString()
const fields = Object.keys(updateData).map(key => `${key} = ?`)
const values = Object.values(updateData)
console.log('SQL:', `UPDATE users SET ${fields.join(', ')}, updated_at = ? WHERE id = ?`)
console.log('參數:', [...values, now, 'user_001'])
await db.execute(
`UPDATE users SET ${fields.join(', ')}, updated_at = ? WHERE id = ?`,
[...values, now, 'user_001']
)
// 3. 驗證更新結果
console.log('3⃣ 驗證更新結果...')
const [updatedUsers] = await db.execute('SELECT * FROM users WHERE id = ?', ['user_001'])
console.log('✅ 更新成功:', updatedUsers[0])
} catch (error) {
console.error('❌ 測試失敗:', error)
} finally {
await db.end()
}
}
testUserUpdate()