This commit is contained in:
NoLogicAlan committed 2026-05-16 23:32:23 +03:00
1 parent db0ca569ee
commit 685c8a9215
4 files changed
+254 -53

No files matched your search

+92 -20
View File
@@ -66,15 +66,23 @@ export class RemoteSettingsManager extends EventEmitter {
}
/**
* Check whether the `bot_id` column exists in the settings table.
* If it doesn't, auto-migrate by adding the column and updating the primary key.
* This ensures old databases work seamlessly with the new multi-bot code.
* Check whether the `bot_id` column exists in the settings table AND is
* part of the primary key. If the column is missing, auto-migrate by
* adding it (NOT NULL DEFAULT '') and updating the primary key to (id, bot_id).
* If the column exists but isn't in the PK (previous failed migration),
* fix the NULL values and retry the PK update.
*
* Key detail: MySQL primary key columns CANNOT be NULL. The previous version
* used DEFAULT NULL which caused ALTER TABLE ... ADD PRIMARY KEY to fail
* silently, leaving the PK as just (id) and producing ER_DUP_ENTRY errors
* when two bots tried to INSERT rows for the same guild.
*/
async _ensureBotIdColumn() {
if (this._hasBotIdColumn) return; // already checked, column exists
if (this._hasBotIdColumn) return; // already verified column + PK
// Check if bot_id column exists and its properties
const res = await this.query(
`SELECT COLUMN_NAME FROM information_schema.COLUMNS `
`SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_KEY FROM information_schema.COLUMNS `
+ `WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = 'bot_id'`
);
@@ -83,35 +91,82 @@ export class RemoteSettingsManager extends EventEmitter {
return;
}
if (res.results.length > 0) {
// Column already exists
if (res.results.length === 0) {
// ── Column doesn't exist: add it and update PK ─────────────────────
logger.settings("[Settings] Auto-migrating: adding bot_id column to settings table...");
// NOT NULL DEFAULT '' — primary key columns can't be NULL in MySQL.
// Existing rows get bot_id = '' which we'll claim for the current bot.
const alterRes = await this.query(
`ALTER TABLE settings ADD COLUMN bot_id VARCHAR(32) NOT NULL DEFAULT ''`
);
if (alterRes.error) {
logger.error("[Settings] Failed to add bot_id column:", alterRes.error);
return;
}
// Claim all existing rows for the current bot before updating PK.
// This preserves existing settings (prefix, volume, etc.) instead of
// leaving them orphaned with bot_id = ''.
if (this.botId) {
await this.query(
`UPDATE settings SET bot_id = ${mysql.escape(String(this.botId))} WHERE bot_id = ''`
);
}
// Update primary key from (id) to (id, bot_id)
const pkRes = await this.query(
`ALTER TABLE settings DROP PRIMARY KEY, ADD PRIMARY KEY (id, bot_id)`
);
if (pkRes.error) {
logger.error("[Settings] Failed to update primary key:", pkRes.error);
return; // Don't set flag — bot_id features can't work without composite PK
}
this._hasBotIdColumn = true;
logger.settings("[Settings] Auto-migration complete: bot_id column added.");
return;
}
// ── Column already exists: verify it's in the primary key ─────────────
const colInfo = res.results[0];
const isNullable = colInfo.IS_NULLABLE === 'YES';
const isPrimaryKey = colInfo.COLUMN_KEY === 'PRI';
if (isPrimaryKey) {
// Column exists and is part of composite PK — all good
this._hasBotIdColumn = true;
return;
}
// Auto-migrate: add bot_id column and update primary key
logger.settings("[Settings] Auto-migrating: adding bot_id column to settings table...");
// Column exists but is NOT in PK — the ALTER TABLE PK update must have
// failed previously (likely because the column had NULL values).
logger.settings("[Settings] Fixing bot_id column: adding to primary key...");
const alterRes = await this.query(
`ALTER TABLE settings ADD COLUMN bot_id VARCHAR(32) DEFAULT NULL`
);
if (alterRes.error) {
logger.error("[Settings] Failed to add bot_id column:", alterRes.error);
return;
// Step 1: Replace NULLs with empty string (PK columns can't be NULL)
if (isNullable) {
await this.query(`UPDATE settings SET bot_id = '' WHERE bot_id IS NULL`);
await this.query(`ALTER TABLE settings MODIFY COLUMN bot_id VARCHAR(32) NOT NULL DEFAULT ''`);
}
// Update primary key from (id) to (id, bot_id)
// Step 2: Claim unclaimed rows (bot_id = '') for the current bot
if (this.botId) {
await this.query(
`UPDATE settings SET bot_id = ${mysql.escape(String(this.botId))} WHERE bot_id = ''`
);
}
// Step 3: Update primary key
const pkRes = await this.query(
`ALTER TABLE settings DROP PRIMARY KEY, ADD PRIMARY KEY (id, bot_id)`
);
if (pkRes.error) {
logger.error("[Settings] Failed to update primary key:", pkRes.error);
// Column was added but PK update failed — still mark as available
// since the column exists and queries will work
return; // Don't set flag — can't use bot_id without composite PK
}
this._hasBotIdColumn = true;
logger.settings("[Settings] Auto-migration complete: bot_id column added.");
logger.settings("[Settings] Fix complete: bot_id column added to primary key.");
}
/**
@@ -129,6 +184,19 @@ export class RemoteSettingsManager extends EventEmitter {
if (this._loadPromise) await this._loadPromise;
// Ensure the DB schema supports bot_id before querying with it
await this._ensureBotIdColumn();
// Claim any unclaimed legacy rows (bot_id = '') for this bot.
// This handles the case where the column was already properly set up
// (e.g. by another bot's migration) but legacy rows weren't claimed yet.
if (this._hasBotIdColumn && this.botId) {
const claimRes = await this.query(
`UPDATE settings SET bot_id = ${mysql.escape(String(this.botId))} WHERE bot_id = ''`
);
if (claimRes.results?.affectedRows > 0) {
logger.settings(`[Settings] Claimed ${claimRes.results.affectedRows} legacy row(s) for bot ${this.botId}`);
}
}
// Reload with the new botId so we pick up this bot's rows
this.guilds.clear();
this._loadPromise = this.load();
@@ -248,7 +316,11 @@ export class RemoteSettingsManager extends EventEmitter {
const escapedId = mysql.escape(id);
const escapedData = mysql.escape(JSON.stringify(server.data));
const bi = this._botIdInsert();
const r = await this.query(`INSERT INTO settings (id, data${bi.col}) VALUES (${escapedId}, ${escapedData}${bi.val})`);
// INSERT IGNORE: if the row already exists (race condition or failed
// migration), silently skip instead of crashing with ER_DUP_ENTRY.
const r = await this.query(
`INSERT IGNORE INTO settings (id, data${bi.col}) VALUES (${escapedId}, ${escapedData}${bi.val})`
);
if (r.error) logger.error("[Settings] create error:", r.error);
}