Switch to postgres in docker compose

This commit is contained in:
TheEssem 2021-03-18 11:19:08 -05:00
parent 5d2e2b5274
commit 9f300f763a
4 changed files with 35 additions and 23 deletions

View File

@ -9,13 +9,13 @@ NODE_ENV=development
TOKEN=
# Put database type here (mongo or postgres)
DB_DRIVER=mongo
DB_DRIVER=postgres
# Put the database connection URL here
# Example for PostgreSQL:
# DB=postgresql://esmbot@localhost:5432/esmbot
# Example for MongoDB:
DB=mongodb://localhost:27017/esmBot
# DB=mongodb://localhost:27017/esmBot
# Example for PostgreSQL:
DB=postgresql://esmbot:verycoolpass100@localhost:5432/esmbot
# Put snowflake ID of bot owner here
OWNER=

View File

@ -25,7 +25,7 @@ services:
- api
- chrome
- lavalink
- mongo
- postgres
networks:
esmbot:
@ -64,23 +64,28 @@ services:
esmbot:
ipv4_address: 172.20.0.5
mongo:
container_name: mongo
image: mongo:latest
postgres:
container_name: postgres
image: postgres:13-alpine
restart: unless-stopped
volumes:
- mongo-data:/data/db
- pg-data:/var/lib/postgresql/data
- ./utils/psqlinit.sh:/docker-entrypoint-initdb.d/psqlinit.sh
environment:
POSTGRES_PASSWORD: verycoolpass100
POSTGRES_USER: esmbot
POSTGRES_DB: esmbot
networks:
esmbot:
ipv4_address: 172.20.0.6
mongo-express:
image: mongo-express
adminer:
image: adminer
restart: unless-stopped
depends_on:
- mongo
- postgres
ports:
- 8888:8081
- 8888:8080
networks:
esmbot:
ipv4_address: 172.20.0.7
@ -88,7 +93,8 @@ services:
volumes:
bot-help:
bot-temp:
mongo-data:
#mongo-data:
pg-data:
networks:
esmbot:

View File

@ -1,4 +1,4 @@
require("dotenv").config();
//require("dotenv").config();
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DB
@ -24,15 +24,14 @@ const Global = mongoose.model("Global", globalSchema);
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 {
console.log("Skipping table creation due to error...");
} catch (e) {
console.error(`Skipping table creation due to error: ${e}`);
}
for (const guild of guilds) {
console.log(guild.tagsDisabled);
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, warns = $3, disabled = $4, tags_disabled = $5 WHERE guild_id = $6", [guild.tags, guild.prefix.substring(0, 15), {}, guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled, guild.id]);
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, warns, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5, $6)", [guild.id, guild.tags, guild.prefix.substring(0, 15), {}, guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled]);
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}`);
}
@ -40,8 +39,8 @@ const Global = mongoose.model("Global", globalSchema);
const global = await Global.findOne();
try {
await pool.query("CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL )");
} catch {
console.log("Skipping table creation due to error...");
} catch (e) {
console.error(`Skipping table creation due to error: ${e}`);
}
console.log(global);
for (const [key, value] of global.cmdCounts) {
@ -53,5 +52,5 @@ const Global = mongoose.model("Global", globalSchema);
console.log(`Migrated counts for command ${key}`);
}
console.log("Done!");
return;
return process.exit(0);
})();

7
utils/psqlinit.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
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 );
EOSQL