2021-08-19 14:19:14 +00:00
import { config } from "dotenv" ;
config ( ) ;
import { Pool } from "pg" ;
2021-08-11 01:25:29 +00:00
const pool = new Pool ( {
connectionString : process . env . DB
} ) ;
( async ( ) => {
const guilds = ( await pool . query ( "SELECT * FROM guilds" ) ) . rows ;
2021-08-13 03:28:09 +00:00
console . log ( "Migrating tags..." ) ;
2021-08-11 01:25:29 +00:00
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 } ` ) ;
}
}
2021-08-13 03:28:09 +00:00
console . log ( "Migrating disabled commands..." ) ;
for ( const guild of guilds ) {
await pool . query ( "UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2" , [ guild . tags _disabled ? [ "tags" ] : [ ] , guild . guild _id ] ) ;
console . log ( ` Migrated disabled commands in guild ${ guild . guild _id } ` ) ;
}
2021-08-11 01:25:29 +00:00
console . log ( "Done!" ) ;
return process . exit ( 0 ) ;
} ) ( ) ;