diff --git a/server/server.mjs b/server/server.mjs index 6d1646a..2ee7c30 100644 --- a/server/server.mjs +++ b/server/server.mjs @@ -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) {