Move tags to separate table

This commit is contained in:
Essem 2021-08-10 20:25:29 -05:00
parent 81277f95c0
commit a75ceb41f2
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
7 changed files with 62 additions and 73 deletions

View file

@ -1,56 +0,0 @@
//require("dotenv").config();
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DB
});
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO, { poolSize: 10, bufferMaxEntries: 0, useNewUrlParser: true, useUnifiedTopology: true });
const guildSchema = new mongoose.Schema({
id: String,
tags: Map,
prefix: String,
disabled: [String],
tagsDisabled: Boolean
});
const Guild = mongoose.model("Guild", guildSchema);
const globalSchema = new mongoose.Schema({
cmdCounts: Map
});
const Global = mongoose.model("Global", globalSchema);
(async () => {
console.log("Migrating guilds...");
const guilds = await Guild.find();
try {
await pool.query("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, tags json NOT NULL, prefix VARCHAR(15) NOT NULL, warns json NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL )");
} catch (e) {
console.error(`Skipping table creation due to error: ${e}`);
}
for (const guild of guilds) {
if ((await pool.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id])).rows.length !== 0) {
await pool.query("UPDATE guilds SET tags = $1, prefix = $2, disabled = $3, tags_disabled = $4 WHERE guild_id = $5", [guild.tags, guild.prefix.substring(0, 15), guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled, guild.id]);
} else {
await pool.query("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, guild.tags, guild.prefix.substring(0, 15), guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled]);
}
console.log(`Migrated guild with ID ${guild.id}`);
}
console.log("Migrating command counts...");
const global = await Global.findOne();
try {
await pool.query("CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL )");
} catch (e) {
console.error(`Skipping table creation due to error: ${e}`);
}
console.log(global);
for (const [key, value] of global.cmdCounts) {
if ((await pool.query("SELECT * FROM counts WHERE command = $1", [key])).rows.length !== 0) {
await pool.query("UPDATE counts SET count = $1 WHERE command = $2", [value, key]);
} else {
await pool.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [key, value]);
}
console.log(`Migrated counts for command ${key}`);
}
console.log("Done!");
return process.exit(0);
})();

27
utils/converttags.js Normal file
View file

@ -0,0 +1,27 @@
require("dotenv").config();
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DB
});
(async () => {
console.log("Migrating tags...");
const guilds = (await pool.query("SELECT * FROM guilds")).rows;
try {
await pool.query("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) )");
} catch (e) {
console.error(`Skipping table creation due to error: ${e}`);
}
for (const guild of guilds) {
for (const [name, value] of Object.entries(guild.tags)) {
if ((await pool.query("SELECT * FROM tags WHERE guild_id = $1 AND name = $2", [guild.guild_id, name])).rows.length !== 0) {
await pool.query("UPDATE tags SET content = $1, author = $2 WHERE guild_id = $3 AND name = $4", [value.content, value.author, guild.guild_id, name]);
} else {
await pool.query("INSERT INTO tags (guild_id, name, content, author) VALUES ($1, $2, $3, $4)", [guild.guild_id, name, value.content, value.author]);
}
console.log(`Migrated tag ${name} in guild ${guild.guild_id}`);
}
}
console.log("Done!");
return process.exit(0);
})();

View file

@ -14,8 +14,10 @@ exports.getCounts = async () => {
exports.disableChannel = async () => {};
exports.enableChannel = async () => {};
exports.toggleTags = async () => {};
exports.getTags = async () => {};
exports.setTag = async () => {};
exports.removeTag = async () => {};
exports.editTag = async () => {};
exports.setPrefix = async () => {};
exports.addGuild = async (guild) => {
return {

View file

@ -1,6 +1,5 @@
const collections = require("../collections.js");
const logger = require("../logger.js");
const misc = require("../misc.js");
const { Pool } = require("pg");
const connection = new Pool({
@ -17,16 +16,25 @@ exports.setPrefix = async (prefix, guild) => {
collections.prefixCache.set(guild.id, prefix);
};
exports.getTags = async (guild) => {
const tagArray = (await connection.query("SELECT * FROM tags WHERE guild_id = $1", [guild])).rows;
const tags = {};
for (const tag of tagArray) {
tags[tag.name] = { content: tag.content, author: tag.author };
}
return tags;
};
exports.setTag = async (name, content, guild) => {
const guildDB = await this.getGuild(guild.id);
guildDB.tags[name] = content;
await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
await connection.query("INSERT INTO tags (guild_id, name, content, author) VALUES ($1, $2, $3, $4)", [guild.id, name, content.content, content.author]);
};
exports.editTag = async (name, content, guild) => {
await connection.query("UPDATE tags SET content = $1, author = $2 WHERE guild_id = $3 AND name = $4", [content.content, content.author, guild.id, name]);
};
exports.removeTag = async (name, guild) => {
const guildDB = await this.getGuild(guild.id);
delete guildDB.tags[name];
await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
await connection.query("DELETE FROM tags WHERE guild_id = $1 AND name = $2", [guild.id, name]);
};
exports.toggleTags = async (guild) => {
@ -71,7 +79,7 @@ exports.addCount = async (command) => {
exports.addGuild = async (guild) => {
const query = await this.getGuild(guild);
if (query) return query;
await connection.query("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, misc.tagDefaults, process.env.PREFIX, [], false]);
await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], false]);
return await this.getGuild(guild.id);
};

View file

@ -85,6 +85,11 @@ exports.toggleTags = async (guild) => {
connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
};
exports.getTags = async (name, content, guild) => {
const guildDB = await this.getGuild(guild.id);
return JSON.parse(guildDB.tags);
};
exports.setTag = async (name, content, guild) => {
const guildDB = await this.getGuild(guild.id);
const tags = JSON.parse(guildDB.tags);
@ -99,6 +104,8 @@ exports.removeTag = async (name, guild) => {
connection.prepare("UPDATE guilds SET tags = ? WHERE guild_id = ?").run(JSON.stringify(tags), guild.id);
};
exports.editTag = this.setTag;
exports.setPrefix = async (prefix, guild) => {
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
collections.prefixCache.set(guild.id, prefix);

View file

@ -2,6 +2,7 @@
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, tags json NOT NULL, prefix VARCHAR(15) NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL );
CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL );
CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL );
CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL );
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) );
EOSQL