More setup, DB init script done
This commit is contained in:
12
db/dbClient.ts
Normal file
12
db/dbClient.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import bbLibConfig from '../bbLibConfig.ts';
|
||||
import { Client } from '../deps.ts';
|
||||
|
||||
const dbClient = await new Client().connect({
|
||||
hostname: bbLibConfig.db.host,
|
||||
port: bbLibConfig.db.port,
|
||||
db: bbLibConfig.db.name,
|
||||
username: bbLibConfig.db.username,
|
||||
password: bbLibConfig.db.password,
|
||||
});
|
||||
|
||||
export default dbClient;
|
||||
@@ -1,7 +1,8 @@
|
||||
// This file will create all tables for the groupup schema
|
||||
// This file will create all tables for the bblib schema
|
||||
// DATA WILL BE LOST IF DB ALREADY EXISTS, RUN AT OWN RISK
|
||||
|
||||
import bbLibConfig from '../bbLibConfig.ts';
|
||||
import dbClient from './dbClient.ts';
|
||||
|
||||
console.log('Attempting to create DB');
|
||||
await dbClient.execute(`CREATE SCHEMA IF NOT EXISTS ${bbLibConfig.db.name};`);
|
||||
@@ -12,9 +13,8 @@ console.log('Attempt to drop all tables');
|
||||
await dbClient.execute(`DROP VIEW IF EXISTS db_size;`);
|
||||
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;`);
|
||||
await dbClient.execute(`DROP TABLE IF EXISTS active_events;`);
|
||||
await dbClient.execute(`DROP TABLE IF EXISTS custom_activities;`);
|
||||
await dbClient.execute(`DROP TABLE IF EXISTS banned_guilds`);
|
||||
await dbClient.execute(`DROP TABLE IF EXISTS banned_users`);
|
||||
console.log('Tables dropped');
|
||||
|
||||
console.log('Attempting to create table command_cnt');
|
||||
@@ -35,61 +35,32 @@ await dbClient.execute(`
|
||||
)
|
||||
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;
|
||||
set oldCnt = (SELECT count FROM ${bbLibConfig.db.name}.command_cnt WHERE command = cmd);
|
||||
UPDATE ${bbLibConfig.db.name}.command_cnt SET count = oldCnt + 1 WHERE command = cmd;
|
||||
END
|
||||
`);
|
||||
console.log('Stored Procedure created');
|
||||
|
||||
console.log('Attempting to create table guild_settings');
|
||||
console.log('Attempting to create table banned_guilds');
|
||||
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;
|
||||
`);
|
||||
console.log('Table created');
|
||||
|
||||
console.log('Attempting to create table active_events');
|
||||
await dbClient.execute(`
|
||||
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 datetime 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');
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
console.log('Attempting to create table custom_activities');
|
||||
await dbClient.execute(`
|
||||
CREATE TABLE custom_activities (
|
||||
id int unsigned NOT NULL AUTO_INCREMENT,
|
||||
guildId bigint unsigned NOT NULL,
|
||||
activityTitle char(35) NOT NULL,
|
||||
activitySubtitle char(50) NOT NULL,
|
||||
maxMembers tinyint NOT NULL,
|
||||
CREATE TABLE banned_guilds (
|
||||
id bigint unsigned NOT NULL,
|
||||
soft tinyint(1) NOT NULL,
|
||||
hard tinyint(1) NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY custom_activities_id_UNIQUE (id)
|
||||
UNIQUE KEY banned_guilds_id_UNIQUE (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`);
|
||||
console.log('Table created');
|
||||
|
||||
console.log('Attempting to create table banned_users');
|
||||
await dbClient.execute(`
|
||||
CREATE TABLE banned_users (
|
||||
id bigint unsigned NOT NULL,
|
||||
soft tinyint(1) NOT NULL,
|
||||
hard tinyint(1) NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY banned_users_id_UNIQUE (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`);
|
||||
console.log('Table created');
|
||||
@@ -104,7 +75,7 @@ await dbClient.execute(`
|
||||
table_rows AS "rows"
|
||||
FROM information_schema.TABLES
|
||||
WHERE
|
||||
table_schema = "${config.db.name}"
|
||||
table_schema = "${bbLibConfig.db.name}"
|
||||
AND table_name <> "db_size";
|
||||
`);
|
||||
console.log('View Created');
|
||||
|
||||
22
db/populateDefaults.ts
Normal file
22
db/populateDefaults.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
// This file will populate the tables with default values
|
||||
import bbLibConfig from '../bbLibConfig.ts';
|
||||
import dbClient from './dbClient.ts';
|
||||
|
||||
console.log('Attempting to insert default actions into command_cnt');
|
||||
const actions = [
|
||||
'cmd-ban-guild-soft',
|
||||
'cmd-ban-guild-hard',
|
||||
'cmd-ban-user-soft',
|
||||
'cmd-ban-user-hard',
|
||||
'cmd-unban-guild',
|
||||
'cmd-unban-user',
|
||||
];
|
||||
for (const action of actions) {
|
||||
await dbClient.execute(`INSERT INTO ${bbLibConfig.db.name}.command_cnt(command) values(?)`, [action]).catch((e) => {
|
||||
console.log(`Failed to insert into database`, e);
|
||||
});
|
||||
}
|
||||
console.log('Insertion done');
|
||||
|
||||
await dbClient.close();
|
||||
console.log('Done!');
|
||||
Reference in New Issue
Block a user