mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
hotfix
This commit is contained in:
1 parent
e966b6ce20
commit
db0ca569ee
9 files changed
+426
-42
No files matched your search
+95
-6
@@ -44,11 +44,18 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
descriptions = {};
|
||||
defaults = {};
|
||||
db = null;
|
||||
/** @type {string|null} Bot user ID — used to isolate rows per-bot in shared databases */
|
||||
botId = null;
|
||||
/** @type {boolean} Whether the bot_id column exists in the settings table */
|
||||
_hasBotIdColumn = false;
|
||||
/** @type {Promise|null} Tracks the in-flight load() so setBotId() can wait for it */
|
||||
_loadPromise = null;
|
||||
// Per-server debounce timers — collapses rapid set() calls into one remoteSave()
|
||||
_debounceTimers = new Map();
|
||||
|
||||
constructor(config, defaultsPath) {
|
||||
constructor(config, defaultsPath, botId = null) {
|
||||
super();
|
||||
this.botId = botId;
|
||||
this.db = mysql.createPool({ connectionLimit: 15, ...config });
|
||||
// Without this listener, any pool-level error (connection drop, timeout, protocol error)
|
||||
// fires an unhandled 'error' event which crashes the entire process with no log output.
|
||||
@@ -56,7 +63,88 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
logger.error("[DB] MySQL pool error:", err.code ?? err.message);
|
||||
});
|
||||
if (defaultsPath) this.loadDefaultsSync(defaultsPath);
|
||||
this.load().catch(err => logger.error("[Settings] Initial load failed:", err?.message ?? err));
|
||||
this._loadPromise = this.load().catch(err => logger.error("[Settings] Initial load failed:", err?.message ?? err));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
async _ensureBotIdColumn() {
|
||||
if (this._hasBotIdColumn) return;
|
||||
|
||||
const res = await this.query(
|
||||
`SELECT COLUMN_NAME FROM information_schema.COLUMNS `
|
||||
+ `WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = 'bot_id'`
|
||||
);
|
||||
|
||||
if (res.error) {
|
||||
logger.error("[Settings] Failed to check bot_id column:", res.error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.results.length > 0) {
|
||||
this._hasBotIdColumn = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Auto-migrate
|
||||
logger.settings("[Settings] Auto-migrating: adding bot_id column to settings table...");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
this._hasBotIdColumn = true;
|
||||
logger.settings("[Settings] Auto-migration complete: bot_id column added.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or update the bot ID used for database row isolation.
|
||||
* Ensures the bot_id column exists (auto-migrates if needed), then reloads.
|
||||
* @param {string} id
|
||||
*/
|
||||
async setBotId(id) {
|
||||
const changed = this.botId !== id;
|
||||
this.botId = id;
|
||||
if (changed) {
|
||||
if (this._loadPromise) await this._loadPromise;
|
||||
await this._ensureBotIdColumn();
|
||||
this.guilds.clear();
|
||||
this._loadPromise = this.load();
|
||||
await this._loadPromise;
|
||||
}
|
||||
}
|
||||
|
||||
_botIdWhere() {
|
||||
if (!this.botId || !this._hasBotIdColumn) return "";
|
||||
return ` AND bot_id = ${mysql.escape(String(this.botId))}`;
|
||||
}
|
||||
|
||||
_botIdInsert() {
|
||||
if (!this.botId || !this._hasBotIdColumn) return { col: "", val: "" };
|
||||
return { col: ", bot_id", val: `, ${mysql.escape(String(this.botId))}` };
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single guild's settings row from the database, filtered by bot_id.
|
||||
* @param {string} guildId
|
||||
* @returns {Promise<{error: object|null, results: Array, fields: Array}>}
|
||||
*/
|
||||
selectGuild(guildId) {
|
||||
const escapedId = mysql.escape(String(guildId));
|
||||
return this.query(`SELECT * FROM settings WHERE id=${escapedId}${this._botIdWhere()}`);
|
||||
}
|
||||
query(query) {
|
||||
return new Promise(res => {
|
||||
@@ -64,7 +152,7 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
});
|
||||
}
|
||||
async load() {
|
||||
const res = await this.query("SELECT * FROM settings");
|
||||
const res = await this.query(`SELECT * FROM settings WHERE 1=1${this._botIdWhere()}`);
|
||||
if (res.error) {
|
||||
this._loadAttempts = (this._loadAttempts || 0) + 1;
|
||||
const delay = Math.min(1000 * Math.pow(2, this._loadAttempts - 1), 30_000);
|
||||
@@ -103,7 +191,7 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
: mysql.escape(String(val));
|
||||
const escapedId = mysql.escape(server.id);
|
||||
const r = await this.query(
|
||||
`UPDATE settings SET data = JSON_SET(data, ${escapedKey}, ${escapedVal}) WHERE id=${escapedId}`
|
||||
`UPDATE settings SET data = JSON_SET(data, ${escapedKey}, ${escapedVal}) WHERE id=${escapedId}${this._botIdWhere()}`
|
||||
);
|
||||
if (r.error) logger.error("[Settings] remoteUpdate error:", r.error);
|
||||
}
|
||||
@@ -111,7 +199,7 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
const escapedData = mysql.escape(JSON.stringify(server.data));
|
||||
const escapedId = mysql.escape(server.id);
|
||||
const r = await this.query(
|
||||
`UPDATE settings SET data = ${escapedData} WHERE id=${escapedId}`
|
||||
`UPDATE settings SET data = ${escapedData} WHERE id=${escapedId}${this._botIdWhere()}`
|
||||
);
|
||||
if (r.error) logger.error("[Settings] remoteSave error:", r.error);
|
||||
}
|
||||
@@ -133,8 +221,9 @@ export class MySqlSettingsManager extends SettingsManager {
|
||||
async create(id, server) {
|
||||
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) VALUES (${escapedId}, ${escapedData})`
|
||||
`INSERT INTO settings (id, data${bi.col}) VALUES (${escapedId}, ${escapedData}${bi.val})`
|
||||
);
|
||||
if (r.error) logger.error("[Settings] create error:", r.error);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
-- Migration: Add bot_id column to the settings table
|
||||
-- This allows multiple bots to share the same MySQL database while keeping
|
||||
-- their settings (prefix, volume, etc.) completely separate.
|
||||
--
|
||||
-- NOTE: The bot now auto-migrates on startup! You do NOT need to run this
|
||||
-- file manually. It is kept here as a reference / manual fallback.
|
||||
--
|
||||
-- The auto-migration happens when setBotId() is called after Discord login:
|
||||
-- 1. Checks if bot_id column exists (via information_schema)
|
||||
-- 2. If not, adds the column and updates the primary key automatically
|
||||
--
|
||||
-- If you prefer to run it manually instead:
|
||||
-- mysql -u <user> -p <database> < settings/migrate-add-bot-id.sql
|
||||
--
|
||||
-- After migration:
|
||||
-- - Existing rows have bot_id = NULL (legacy, works with both old and new code)
|
||||
-- - New rows created by bots will have bot_id set to the bot's user ID
|
||||
-- - The composite primary key (id, bot_id) allows the same guild to have
|
||||
-- separate settings per bot
|
||||
|
||||
-- Step 1: Add bot_id column if it doesn't exist
|
||||
ALTER TABLE settings
|
||||
ADD COLUMN IF NOT EXISTS bot_id VARCHAR(32) DEFAULT NULL;
|
||||
|
||||
-- Step 2: Drop the old primary key (id) and create a composite one (id, bot_id)
|
||||
-- This allows the same guild ID to exist with different bot_id values.
|
||||
-- We use a procedure because MySQL doesn't support IF EXISTS on ALTER TABLE ... DROP PRIMARY KEY
|
||||
-- and we want this migration to be idempotent (safe to run multiple times).
|
||||
|
||||
DELIMITER //
|
||||
|
||||
DROP PROCEDURE IF EXISTS migrate_add_bot_id_pk //
|
||||
|
||||
CREATE PROCEDURE migrate_add_bot_id_pk()
|
||||
BEGIN
|
||||
DECLARE pk_has_bot_id INT DEFAULT 0;
|
||||
|
||||
-- Check if the current primary key already includes bot_id
|
||||
SELECT COUNT(*) INTO pk_has_bot_id
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'settings'
|
||||
AND COLUMN_KEY = 'PRI'
|
||||
AND COLUMN_NAME = 'bot_id';
|
||||
|
||||
IF pk_has_bot_id = 0 THEN
|
||||
-- bot_id is not part of the primary key yet; safe to alter
|
||||
ALTER TABLE settings DROP PRIMARY KEY, ADD PRIMARY KEY (id, bot_id);
|
||||
END IF;
|
||||
END //
|
||||
|
||||
CALL migrate_add_bot_id_pk() //
|
||||
|
||||
DROP PROCEDURE IF EXISTS migrate_add_bot_id_pk //
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
-- Step 3 (Optional): If you already know which bot owns which rows,
|
||||
-- you can set bot_id manually. For example, if the existing rows belong
|
||||
-- to a bot with ID "123456789012345678":
|
||||
-- UPDATE settings SET bot_id = '123456789012345678' WHERE bot_id IS NULL;
|
||||
--
|
||||
-- If you leave bot_id as NULL, those rows are treated as "shared" by the
|
||||
-- legacy code path (no bot_id filter applied). Once each bot starts up
|
||||
-- with the new code, it will create its own rows with bot_id set.
|
||||
@@ -4,11 +4,15 @@ import { logger } from "../src/constants/Logger.mjs";
|
||||
|
||||
const config = JSON.parse(fs.readFileSync(new URL("../config.json", import.meta.url)));
|
||||
|
||||
// Pass bot ID from config if available, so migrations are bot-isolated.
|
||||
// If no botId in config, all rows are migrated (legacy behavior).
|
||||
const botId = config.botId ?? null;
|
||||
|
||||
// Both source and destination use RemoteSettingsManager to ensure the
|
||||
// `guilds` Map is populated from MySQL. The abstract SettingsManager base
|
||||
// class has no implementation (no guilds, no load, no defaults).
|
||||
const sm = new RemoteSettingsManager(config.mysql, "./storage/defaults.json");
|
||||
const rsm = new RemoteSettingsManager(config.mysql, "./storage/defaults.json");
|
||||
const sm = new RemoteSettingsManager(config.mysql, "./storage/defaults.json", botId);
|
||||
const rsm = new RemoteSettingsManager(config.mysql, "./storage/defaults.json", botId);
|
||||
|
||||
// Ensure source manager is fully loaded BEFORE starting migration.
|
||||
// Previously, rsm's "ready" could fire before sm finished loading,
|
||||
|
||||
Reference in new issue
Block a user