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

6
app.js
View file

@ -25,6 +25,8 @@ import winston from "winston";
import { exec as baseExec } from "child_process";
import { promisify } from "util";
const exec = promisify(baseExec);
// database stuff
import database from "./utils/database.js";
// dbl posting
import { Api } from "@top-gg/sdk";
const dbl = process.env.NODE_ENV === "production" && process.env.DBL ? new Api(process.env.DBL) : null;
@ -148,6 +150,10 @@ if (isMaster) {
error: "red"
});
(async () => {
if (await database.upgrade(logger)) return process.exit(1);
})();
Admiral.on("log", (m) => logger.main(m));
Admiral.on("info", (m) => logger.info(m));
Admiral.on("debug", (m) => logger.debug(m));

View file

@ -11,7 +11,7 @@ lavalink:
twitch: true
vimeo: true
mixer: true
http: true
http: false
local: true
bufferDurationMs: 400
youtubePlaylistLoadLimit: 6 # Number of pages at 100 each
@ -25,6 +25,9 @@ lavalink:
#strategy: "RotateOnBan" # RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch
#searchTriggersFail: true # Whether a search 429 should trigger marking the ip as failing
#retryLimit: -1 # -1 = use default lavaplayer value | 0 = infinity | >0 = retry will happen this numbers times
plugins:
- dependency: "com.github.esmBot:lava-xm-plugin:v0.1.0"
repository: "https://jitpack.io"
metrics:
prometheus:

View file

@ -100,7 +100,6 @@ export default async (client, cluster, worker, ipc, message) => {
try {
await database.addCount(aliases.get(command) ?? command);
const startTime = new Date();
if (message.channel.guild) await database.updateTime(startTime, message.channel.guild.id);
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd(client, cluster, worker, ipc, message, parsed._, message.content.substring(prefix.length).trim().replace(command, "").trim(), (({ _, ...o }) => o)(parsed)); // we also provide the message content as a parameter for cases where we need more accuracy
const result = await commandClass.run();

View file

@ -1,3 +1,5 @@
// wrapper for the database drivers in ./database/
import { config } from "dotenv";
config();
export default await import(`./database/${process.env.DB ? process.env.DB.split("://")[0] : "dummy"}.js`);
export default await import(`./database/${process.env.DB ? process.env.DB.split("://")[0] : "dummy"}.js`);

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);
}