mrmBot-Matrix/utils/convertdb.js

56 lines
No EOL
2.5 KiB
JavaScript

//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);
})();