2021-03-18 16:19:08 +00:00
//require("dotenv").config();
2020-12-18 02:32:19 +00:00
const { Pool } = require ( "pg" ) ;
const pool = new Pool ( {
2020-12-19 00:50:25 +00:00
connectionString : process . env . DB
2020-12-18 02:32:19 +00:00
} ) ;
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 )" ) ;
2021-03-18 16:19:08 +00:00
} catch ( e ) {
console . error ( ` Skipping table creation due to error: ${ e } ` ) ;
2020-12-18 02:32:19 +00:00
}
for ( const guild of guilds ) {
if ( ( await pool . query ( "SELECT * FROM guilds WHERE guild_id = $1" , [ guild . id ] ) ) . rows . length !== 0 ) {
2021-03-18 16:19:08 +00:00
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 ] ) ;
2020-12-18 02:32:19 +00:00
} else {
2021-03-18 16:19:08 +00:00
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 ] ) ;
2020-12-18 02:32:19 +00:00
}
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 )" ) ;
2021-03-18 16:19:08 +00:00
} catch ( e ) {
console . error ( ` Skipping table creation due to error: ${ e } ` ) ;
2020-12-18 02:32:19 +00:00
}
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!" ) ;
2021-03-18 16:19:08 +00:00
return process . exit ( 0 ) ;
2020-12-18 02:32:19 +00:00
} ) ( ) ;