Move database migration, removed guild modified times, added XM plugin to lavalink config

This commit is contained in:
Essem 2022-02-21 18:55:25 -06:00
parent 4ef2ce7526
commit bc9eb12b5a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
6 changed files with 59 additions and 62 deletions

View file

@ -9,35 +9,11 @@ const connection = new Postgres.Pool({
const psqlUpdates = [
"", // reserved
"CREATE TABLE settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;"
"CREATE TABLE settings ( id smallint PRIMARY KEY, version integer NOT NULL, CHECK(id = 1) );\nALTER TABLE guilds ADD COLUMN accessed timestamp;",
"ALTER TABLE guilds DROP COLUMN accessed"
];
// INSERT INTO settings (id, version) VALUES(1, $1)
export async function setup(ipc) {
let version;
try {
version = (await connection.query("SELECT version FROM settings WHERE id = 1")).rows[0].version;
} catch {
version = 0;
}
if (version < (psqlUpdates.length - 1)) {
logger.warn(`Migrating PostgreSQL database, which is currently at version ${version}...`);
await connection.query("BEGIN");
try {
while (version < (psqlUpdates.length - 1)) {
version++;
logger.warn(`Running version ${version} update script (${psqlUpdates[version]})...`);
await connection.query(psqlUpdates[version]);
}
await connection.query("COMMIT");
await connection.query("INSERT INTO settings (id, version) VALUES (1, $1) ON CONFLICT (id) DO NOTHING", [psqlUpdates.length - 1]);
} catch (e) {
logger.error(`PostgreSQL migration failed: ${e}`);
await connection.query("ROLLBACK");
logger.error("Unable to start the bot, quitting now.");
throw ipc.totalShutdown();
}
}
export async function setup() {
let counts;
try {
counts = await connection.query("SELECT * FROM counts");
@ -67,12 +43,35 @@ export async function setup(ipc) {
}
}
export async function getGuild(query) {
return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
export async function upgrade(logger) {
let version;
try {
version = (await connection.query("SELECT version FROM settings WHERE id = 1")).rows[0].version;
} catch {
version = 0;
}
if (version < (psqlUpdates.length - 1)) {
logger.warn(`Migrating PostgreSQL database, which is currently at version ${version}...`);
await connection.query("BEGIN");
try {
while (version < (psqlUpdates.length - 1)) {
version++;
logger.warn(`Running version ${version} update script (${psqlUpdates[version]})...`);
await connection.query(psqlUpdates[version]);
}
await connection.query("COMMIT");
await connection.query("INSERT INTO settings (id, version) VALUES (1, $1) ON CONFLICT (id) DO UPDATE SET version = $1", [psqlUpdates.length - 1]);
} catch (e) {
logger.error(`PostgreSQL migration failed: ${e}`);
await connection.query("ROLLBACK");
logger.error("Unable to start the bot, quitting now.");
return 1;
}
}
}
export async function updateTime(time, guild) {
await connection.query("UPDATE guilds SET accessed = $1 WHERE guild_id = $2", [time, guild]);
export async function getGuild(query) {
return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
}
export async function setPrefix(prefix, guild) {

View file

@ -6,26 +6,12 @@ const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
const sqliteUpdates = [
"", // reserved
"ALTER TABLE guilds ADD COLUMN accessed int" // CREATE TABLE settings ( version int );\n
"ALTER TABLE guilds ADD COLUMN accessed int",
"ALTER TABLE guilds DROP COLUMN accessed"
];
export async function setup(ipc) {
connection.prepare("CREATE TABLE IF NOT EXISTS guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, disabled_commands text NOT NULL )").run();
let counts;
try {
counts = connection.prepare("SELECT * FROM counts").all();
} catch {
connection.prepare("CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL )").run();
counts = [];
}
try {
connection.prepare("SELECT * FROM tags").all();
} catch {
connection.prepare("CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) )").run();
}
export async function setup() {
const counts = connection.prepare("SELECT * FROM counts").all();
if (!counts) {
for (const command of collections.commands.keys()) {
connection.prepare("INSERT INTO counts (command, count) VALUES (?, ?)").run(command, 0);
@ -46,7 +32,17 @@ export async function setup(ipc) {
}
}
}
}
export async function stop() {
connection.close();
}
export async function upgrade(logger) {
connection.prepare("CREATE TABLE IF NOT EXISTS guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, disabled_commands text NOT NULL )").run();
connection.prepare("CREATE TABLE IF NOT EXISTS counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL )").run();
connection.prepare("CREATE TABLE IF NOT EXISTS tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) )").run();
let version = connection.pragma("user_version", { simple: true });
if (version < (sqliteUpdates.length - 1)) {
logger.warn(`Migrating SQLite database at ${process.env.DB}, which is currently at version ${version}...`);
@ -63,15 +59,11 @@ export async function setup(ipc) {
logger.error(`SQLite migration failed: ${e}`);
connection.prepare("ROLLBACK").run();
logger.error("Unable to start the bot, quitting now.");
throw ipc.totalShutdown();
return 1;
}
}
}
export async function stop() {
connection.close();
}
export async function fixGuild(guild) {
let guildDB;
try {
@ -85,10 +77,6 @@ export async function fixGuild(guild) {
}
}
export async function updateTime(time, guild) {
connection.prepare("UPDATE guilds SET accessed = ? WHERE guild_id = ?").run(Math.floor(time / 1000), guild);
}
export async function addCount(command) {
connection.prepare("UPDATE counts SET count = count + 1 WHERE command = ?").run(command);
}