2023-04-20 23:45:02 -07:00
|
|
|
// This file will create all tables for the groupup schema
|
2021-05-30 14:04:58 -07:00
|
|
|
// 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';
|
2023-01-28 17:58:24 -08:00
|
|
|
import { dbClient } from '../src/db.ts';
|
2021-05-30 14:04:58 -07:00
|
|
|
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Attempting to create DB');
|
2021-05-30 14:04:58 -07:00
|
|
|
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');
|
2021-05-30 14:04:58 -07:00
|
|
|
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Attempt to drop all tables');
|
2023-04-30 23:56:57 -07:00
|
|
|
await dbClient.execute(`DROP VIEW IF EXISTS db_size;`);
|
2021-05-30 14:04:58 -07:00
|
|
|
await dbClient.execute(`DROP PROCEDURE IF EXISTS INC_CNT;`);
|
|
|
|
await dbClient.execute(`DROP TABLE IF EXISTS command_cnt;`);
|
2023-01-28 17:58:24 -08:00
|
|
|
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;`);
|
2023-04-29 00:57:18 -07:00
|
|
|
await dbClient.execute(`DROP TABLE IF EXISTS custom_activities;`);
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Tables dropped');
|
2021-05-30 14:04:58 -07:00
|
|
|
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Attempting to create table command_cnt');
|
2021-05-30 14:04:58 -07:00
|
|
|
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');
|
2021-05-30 14:04:58 -07:00
|
|
|
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Attempt creating increment Stored Procedure');
|
2021-05-30 14:04:58 -07:00
|
|
|
await dbClient.execute(`
|
|
|
|
CREATE PROCEDURE INC_CNT(
|
|
|
|
IN cmd CHAR(20)
|
|
|
|
)
|
|
|
|
BEGIN
|
2023-01-28 17:58:24 -08:00
|
|
|
declare oldCnt bigint unsigned;
|
|
|
|
set oldCnt = (SELECT count FROM command_cnt WHERE command = cmd);
|
|
|
|
UPDATE command_cnt SET count = oldCnt + 1 WHERE command = cmd;
|
2021-05-30 14:04:58 -07:00
|
|
|
END
|
|
|
|
`);
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Stored Procedure created');
|
2021-05-30 14:04:58 -07:00
|
|
|
|
2023-01-28 17:58:24 -08:00
|
|
|
console.log('Attempting to create table guild_settings');
|
2021-11-11 19:23:36 -08:00
|
|
|
await dbClient.execute(`
|
2023-01-28 17:58:24 -08:00
|
|
|
CREATE TABLE guild_settings (
|
2021-11-11 19:23:36 -08:00
|
|
|
guildId bigint unsigned NOT NULL,
|
2023-01-28 17:58:24 -08:00
|
|
|
lfgChannelId bigint unsigned NOT NULL,
|
|
|
|
managerRoleId bigint unsigned NOT NULL,
|
|
|
|
logChannelId bigint unsigned NOT NULL,
|
|
|
|
PRIMARY KEY (guildId, lfgChannelId)
|
2021-11-11 19:23:36 -08:00
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`);
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Table created');
|
2021-11-11 19:23:36 -08:00
|
|
|
|
2023-04-20 23:41:37 -07:00
|
|
|
console.log('Attempting to create table active_events');
|
2023-04-06 20:23:52 -07:00
|
|
|
await dbClient.execute(`
|
2023-04-20 23:41:37 -07:00
|
|
|
CREATE TABLE active_events (
|
2023-04-06 20:23:52 -07:00
|
|
|
messageId bigint unsigned NOT NULL,
|
|
|
|
channelId bigint unsigned NOT NULL,
|
|
|
|
guildId bigint unsigned NOT NULL,
|
|
|
|
ownerId bigint unsigned NOT NULL,
|
2023-04-29 06:33:43 -07:00
|
|
|
eventTime datetime NOT NULL,
|
2023-04-06 20:23:52 -07:00
|
|
|
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');
|
2023-04-28 20:27:42 -07:00
|
|
|
/**
|
|
|
|
* notifiedFlag
|
|
|
|
* 0 = Not notified
|
|
|
|
* 1 = Notified Successfully
|
|
|
|
* -1 = Failed to notify
|
|
|
|
* lockedFlag
|
|
|
|
* 0 = Not locked
|
|
|
|
* 1 = Locked Successfully
|
|
|
|
* -1 = Failed to lock
|
|
|
|
*
|
|
|
|
* If both are -1, the event failed to delete
|
|
|
|
*/
|
2023-04-06 20:23:52 -07:00
|
|
|
|
2023-04-29 00:57:18 -07:00
|
|
|
console.log('Attempting to create table custom_activities');
|
|
|
|
await dbClient.execute(`
|
|
|
|
CREATE TABLE custom_activities (
|
|
|
|
id int unsigned NOT NULL AUTO_INCREMENT,
|
2023-04-30 23:56:57 -07:00
|
|
|
guildId bigint unsigned NOT NULL,
|
2023-04-29 00:57:18 -07:00
|
|
|
activityTitle char(35) NOT NULL,
|
|
|
|
activitySubtitle char(50) NOT NULL,
|
|
|
|
maxMembers tinyint NOT NULL,
|
|
|
|
PRIMARY KEY (id),
|
|
|
|
UNIQUE KEY custom_activities_id_UNIQUE (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`);
|
|
|
|
console.log('Table created');
|
|
|
|
|
2023-04-29 21:27:15 -07:00
|
|
|
// Database sizes view
|
|
|
|
console.log('Attempting to create view db_size');
|
|
|
|
await dbClient.execute(`
|
|
|
|
CREATE VIEW db_size AS
|
|
|
|
SELECT
|
|
|
|
table_name AS "table",
|
|
|
|
ROUND(((data_length + index_length) / 1024 / 1024), 3) AS "size",
|
|
|
|
table_rows AS "rows"
|
|
|
|
FROM information_schema.TABLES
|
|
|
|
WHERE
|
|
|
|
table_schema = "${config.db.name}"
|
|
|
|
AND table_name <> "db_size";
|
|
|
|
`);
|
|
|
|
console.log('View Created');
|
|
|
|
|
2021-05-30 14:04:58 -07:00
|
|
|
await dbClient.close();
|
2022-07-10 17:48:38 -07:00
|
|
|
console.log('Done!');
|