functional initDB and verified database functions
This commit is contained in:
parent
ab61049de0
commit
dfa75b046a
2 changed files with 41 additions and 15 deletions
|
@ -7,7 +7,8 @@ services:
|
||||||
dockerfile: Dockerfile.bot
|
dockerfile: Dockerfile.bot
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
db:
|
||||||
|
condition: service_healthy
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
|
@ -15,7 +16,6 @@ services:
|
||||||
DB_HOST: db
|
DB_HOST: db
|
||||||
DB_USER: postgres
|
DB_USER: postgres
|
||||||
DB_PASSWORD: example
|
DB_PASSWORD: example
|
||||||
DATABASE: hypixel
|
|
||||||
volumes:
|
volumes:
|
||||||
- /etc/localtime:/etc/localtime:ro
|
- /etc/localtime:/etc/localtime:ro
|
||||||
- /etc/timezone:/etc/timezone:ro
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
@ -27,13 +27,15 @@ services:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile.db
|
dockerfile: Dockerfile.db
|
||||||
restart: always
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_PASSWORD: example # Change this to a secure password. Has to be identical to DB_PASSWORD in the bot service
|
POSTGRES_PASSWORD: example # Change this to a secure password. Has to be identical to DB_PASSWORD in the bot service
|
||||||
POSTGRES_DB: hypixel
|
|
||||||
ports:
|
|
||||||
- 5432:5432
|
|
||||||
volumes:
|
volumes:
|
||||||
- ./db_data:/var/lib/postgres/data
|
- ./db_data:/var/lib/postgres/data
|
||||||
- /etc/localtime:/etc/localtime:ro
|
- /etc/localtime:/etc/localtime:ro
|
||||||
- /etc/timezone:/etc/timezone:ro
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "pg_isready", "-U", "postgres"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 3
|
||||||
|
|
|
@ -4,16 +4,17 @@ const config = loadConfig();
|
||||||
|
|
||||||
class SqlSystem {
|
class SqlSystem {
|
||||||
|
|
||||||
private static pool: Pool = new Pool({
|
private static pool: Pool;
|
||||||
database: process.env.DATABASE,
|
|
||||||
host: process.env.DB_HOST,
|
|
||||||
user: process.env.DB_USER,
|
|
||||||
password: process.env.DB_PASSWORD,
|
|
||||||
max: config.data.worker_count ?? 10
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
public static async InitDB() {
|
public static async InitDB() {
|
||||||
|
this.pool = new Pool({
|
||||||
|
database: 'postgres',
|
||||||
|
host: process.env.DB_HOST,
|
||||||
|
user: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASSWORD,
|
||||||
|
max: config.data.worker_count ?? 10
|
||||||
|
});
|
||||||
let conn: PoolClient | undefined;
|
let conn: PoolClient | undefined;
|
||||||
try {
|
try {
|
||||||
conn = await this.pool.connect();
|
conn = await this.pool.connect();
|
||||||
|
@ -82,6 +83,10 @@ class SqlSystem {
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
EXECUTE PROCEDURE public.fn_remove_lifetime();
|
EXECUTE PROCEDURE public.fn_remove_lifetime();
|
||||||
`);
|
`);
|
||||||
|
await conn.query(`
|
||||||
|
CREATE EXTENSION IF NOT EXISTS pg_cron;
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
`);
|
||||||
//Setup Table sanitation
|
//Setup Table sanitation
|
||||||
await conn.query(`
|
await conn.query(`
|
||||||
CREATE OR REPLACE FUNCTION public.cleanup_old_entries()
|
CREATE OR REPLACE FUNCTION public.cleanup_old_entries()
|
||||||
|
@ -89,7 +94,7 @@ class SqlSystem {
|
||||||
LANGUAGE plpgsql
|
LANGUAGE plpgsql
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
temp_id INT;
|
temp_id UUID;
|
||||||
BEGIN
|
BEGIN
|
||||||
FOR temp_id IN
|
FOR temp_id IN
|
||||||
SELECT id
|
SELECT id
|
||||||
|
@ -104,7 +109,26 @@ class SqlSystem {
|
||||||
RETURN;
|
RETURN;
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
`);
|
`);
|
||||||
|
await conn.query(`
|
||||||
|
DO $outer$
|
||||||
|
DECLARE job_exists boolean := false;
|
||||||
|
BEGIN
|
||||||
|
SELECT (count(*) > 0)
|
||||||
|
INTO job_exists
|
||||||
|
FROM cron.job
|
||||||
|
WHERE jobname = 'cleanup_old_entries_event';
|
||||||
|
|
||||||
|
IF NOT job_exists THEN
|
||||||
|
PERFORM cron.schedule(
|
||||||
|
'cleanup_old_entries_event',
|
||||||
|
'*/2 * * * *',
|
||||||
|
$sql$ SELECT public.cleanup_old_entries(); $sql$
|
||||||
|
);
|
||||||
|
END IF;
|
||||||
|
END;
|
||||||
|
$outer$;
|
||||||
|
`);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error("InitDB Error: ", error);
|
console.error("InitDB Error: ", error);
|
||||||
|
|
Loading…
Reference in a new issue