GroupUp/db/initialize.ts

72 lines
2.3 KiB
TypeScript
Raw Normal View History

// This file will create all tables for the groupup schema
// DATA WILL BE LOST IF DB ALREADY EXISTS, RUN AT OWN RISK
2022-07-10 17:48:38 -07:00
import config from '../config.ts';
import { dbClient } from '../src/db.ts';
2022-07-10 17:48:38 -07:00
console.log('Attempting to create DB');
await dbClient.execute(`CREATE SCHEMA IF NOT EXISTS ${config.db.name};`);
await dbClient.execute(`USE ${config.db.name}`);
2022-07-10 17:48:38 -07:00
console.log('DB created');
2022-07-10 17:48:38 -07:00
console.log('Attempt to drop all tables');
await dbClient.execute(`DROP PROCEDURE IF EXISTS INC_CNT;`);
await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`);
await dbClient.execute(`DROP TABLE IF EXISTS guild_settings;`);
2023-04-20 23:41:37 -07:00
await dbClient.execute(`DROP TABLE IF EXISTS active_events;`);
2022-07-10 17:48:38 -07:00
console.log('Tables dropped');
2022-07-10 17:48:38 -07:00
console.log('Attempting to create table command_cnt');
await dbClient.execute(`
CREATE TABLE command_cnt (
command char(20) NOT NULL,
count bigint unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (command),
UNIQUE KEY command_cnt_command_UNIQUE (command)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`);
2022-07-10 17:48:38 -07:00
console.log('Table created');
2022-07-10 17:48:38 -07:00
console.log('Attempt creating increment Stored Procedure');
await dbClient.execute(`
CREATE PROCEDURE INC_CNT(
IN cmd CHAR(20)
)
BEGIN
declare oldCnt bigint unsigned;
set oldCnt = (SELECT count FROM command_cnt WHERE command = cmd);
UPDATE command_cnt SET count = oldCnt + 1 WHERE command = cmd;
END
`);
2022-07-10 17:48:38 -07:00
console.log('Stored Procedure created');
console.log('Attempting to create table guild_settings');
await dbClient.execute(`
CREATE TABLE guild_settings (
guildId bigint unsigned NOT NULL,
lfgChannelId bigint unsigned NOT NULL,
managerRoleId bigint unsigned NOT NULL,
logChannelId bigint unsigned NOT NULL,
PRIMARY KEY (guildId, lfgChannelId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`);
2022-07-10 17:48:38 -07:00
console.log('Table created');
2023-04-20 23:41:37 -07:00
console.log('Attempting to create table active_events');
await dbClient.execute(`
2023-04-20 23:41:37 -07:00
CREATE TABLE active_events (
messageId bigint unsigned NOT NULL,
channelId bigint unsigned NOT NULL,
guildId bigint unsigned NOT NULL,
ownerId bigint unsigned NOT NULL,
eventTime timestamp NOT NULL,
notifiedFlag tinyint(1) NOT NULL DEFAULT 0,
lockedFlag tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (messageId, channelId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`);
console.log('Table created');
await dbClient.close();
2022-07-10 17:48:38 -07:00
console.log('Done!');