自動放寬舊資料表過小的欄位,修正上傳失敗
根本原因: badminton 資料表是舊系統先建立的,battlecombination 欄位型別比 TEXT 小;ensureTable 的 CREATE TABLE IF NOT EXISTS 對已存在的資料表不會生效,跨區平衡後 6 隊 × 3 輪的 JSON 超過欄位長度,寫入時噴出「Data too long for column 'battlecombination'」。 影響: - server 啟動後第一次資料庫操作會自動把 personnel 與 battlecombination 放寬成 TEXT,不需手動下 SQL - ensureTable 加上 tableReady 旗標,每個 process 只檢查一次,不再每個 request 都跑 CREATE TABLE 修法: 新增 widenLegacyColumns 查詢 information_schema,欄位型別不是 TEXT 系列時執行 ALTER TABLE MODIFY 放寬,並以模組層旗標記憶檢查結果。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -240,7 +240,13 @@ app.listen(port, () => {
|
||||
}
|
||||
})
|
||||
|
||||
let tableReady = false
|
||||
|
||||
async function ensureTable(poolInstance, currentTableName) {
|
||||
if (tableReady) {
|
||||
return
|
||||
}
|
||||
|
||||
await poolInstance.execute(`
|
||||
CREATE TABLE IF NOT EXISTS \`${currentTableName}\` (
|
||||
time INT(11) NOT NULL,
|
||||
@@ -249,6 +255,34 @@ async function ensureTable(poolInstance, currentTableName) {
|
||||
PRIMARY KEY (time)
|
||||
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
|
||||
`)
|
||||
|
||||
await widenLegacyColumns(poolInstance, currentTableName)
|
||||
tableReady = true
|
||||
}
|
||||
|
||||
async function widenLegacyColumns(poolInstance, currentTableName) {
|
||||
const [columns] = await poolInstance.execute(
|
||||
`
|
||||
SELECT COLUMN_NAME AS name, DATA_TYPE AS type
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
AND COLUMN_NAME IN ('personnel', 'battlecombination')
|
||||
`,
|
||||
[currentTableName],
|
||||
)
|
||||
|
||||
for (const column of columns) {
|
||||
if (['text', 'mediumtext', 'longtext'].includes(String(column.type).toLowerCase())) {
|
||||
continue
|
||||
}
|
||||
|
||||
const nullability = column.name === 'personnel' ? 'NOT NULL' : 'DEFAULT NULL'
|
||||
await poolInstance.execute(
|
||||
`ALTER TABLE \`${currentTableName}\` MODIFY \`${column.name}\` TEXT ${nullability}`,
|
||||
)
|
||||
console.log(`widened column ${column.name} to TEXT on \`${currentTableName}\``)
|
||||
}
|
||||
}
|
||||
|
||||
function buildLineFlexMessage(time, rounds) {
|
||||
|
||||
Reference in New Issue
Block a user