From 8a958ed326b5bf7c444d109f96617d61e6ad60f9 Mon Sep 17 00:00:00 2001 From: Tarrgon <61888458+Tarrgon@users.noreply.github.com> Date: Tue, 2 Jun 2026 10:35:45 -0400 Subject: [PATCH] Switch to migrations --- src/migrations/0001-add-appeals.sql | 16 ++++++++++++++++ src/shared/Database.ts | 27 +++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 src/migrations/0001-add-appeals.sql diff --git a/src/migrations/0001-add-appeals.sql b/src/migrations/0001-add-appeals.sql new file mode 100644 index 0000000..cd25dae --- /dev/null +++ b/src/migrations/0001-add-appeals.sql @@ -0,0 +1,16 @@ +-------------------------------------------------------------------------------- +-- Up +-------------------------------------------------------------------------------- +ALTER TABLE settings ADD appeals_channel_id TEXT; + +CREATE TABLE appeals ( + id INTEGER PRIMARY KEY, + message_id TEXT NOT NULL +); + +-------------------------------------------------------------------------------- +-- Down +-------------------------------------------------------------------------------- +ALTER TABLE settings DROP COLUMN appeals_channel_id; + +DROP TABLE appeals; \ No newline at end of file diff --git a/src/shared/Database.ts b/src/shared/Database.ts index 6808d86..0fcb4e3 100644 --- a/src/shared/Database.ts +++ b/src/shared/Database.ts @@ -1,8 +1,9 @@ -import sqlite3 from 'sqlite3'; +import path from 'path'; import { open, Database as SqliteDatabase } from 'sqlite'; -import { serializeMessage, wait } from '../utils'; -import { GuildSettings, LoggedMessage, TicketMessage, TicketPhrase, Note, Ban, GuildArraySetting, GithubUserMapping, KnowledgebaseItem, PrivateHelpTicket, AppealMessage } from '../types'; +import sqlite3 from 'sqlite3'; import { Message } from '../events'; +import { AppealMessage, Ban, GithubUserMapping, GuildArraySetting, GuildSettings, KnowledgebaseItem, LoggedMessage, Note, PrivateHelpTicket, TicketMessage, TicketPhrase } from '../types'; +import { serializeMessage, wait } from '../utils'; const DB_SCHEMA = ` CREATE TABLE IF NOT EXISTS discord_names ( @@ -30,8 +31,7 @@ const DB_SCHEMA = ` link_skip_channels TEXT, github_release_channel TEXT, moderator_channel_id TEXT, - private_help_channel_id TEXT, - appeals_channel_id TEXT + private_help_channel_id TEXT ); CREATE TABLE IF NOT EXISTS messages ( @@ -59,11 +59,6 @@ const DB_SCHEMA = ` phrase TEXT NOT NULL ); - CREATE TABLE IF NOT EXISTS appeals ( - id INTEGER PRIMARY KEY, - message_id TEXT NOT NULL - ); - CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY, user_id TEXT, @@ -134,6 +129,8 @@ export class Database { console.log('SQLite database opened'); await Database.ensure(); + + await Database.migrate(); } private static async ensure() { @@ -141,6 +138,16 @@ export class Database { console.log('SQLite database ensured'); } + private static async migrate() { + console.log('Starting database migrations'); + + await Database.db.migrate({ + migrationsPath: path.join(__dirname, '..', 'migrations') + }); + + console.log('Database migrations ran'); + } + // -- START WHOIS -- static async getE621Ids(discordId: string): Promise {