From dfa75b046af4551f338aeff6c01d51d73c30e191 Mon Sep 17 00:00:00 2001 From: ulysia Date: Mon, 6 Jan 2025 20:54:32 +0100 Subject: [PATCH] functional initDB and verified database functions --- docker-compose.yml | 14 ++++++++------ src/sqlFunctions.ts | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index daceae5..8535b13 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,8 @@ services: dockerfile: Dockerfile.bot restart: unless-stopped depends_on: - - db + db: + condition: service_healthy env_file: - .env environment: @@ -15,7 +16,6 @@ services: DB_HOST: db DB_USER: postgres DB_PASSWORD: example - DATABASE: hypixel volumes: - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro @@ -27,13 +27,15 @@ services: build: context: . dockerfile: Dockerfile.db - restart: always + restart: unless-stopped environment: 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: - ./db_data:/var/lib/postgres/data - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro + healthcheck: + test: ["CMD", "pg_isready", "-U", "postgres"] + interval: 10s + timeout: 5s + retries: 3 diff --git a/src/sqlFunctions.ts b/src/sqlFunctions.ts index cf41a62..d9ce4b1 100644 --- a/src/sqlFunctions.ts +++ b/src/sqlFunctions.ts @@ -4,16 +4,17 @@ const config = loadConfig(); class SqlSystem { - private static pool: Pool = new 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 - }) + private static pool: Pool; 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; try { conn = await this.pool.connect(); @@ -82,6 +83,10 @@ class SqlSystem { FOR EACH ROW 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 await conn.query(` CREATE OR REPLACE FUNCTION public.cleanup_old_entries() @@ -89,7 +94,7 @@ class SqlSystem { LANGUAGE plpgsql AS $$ DECLARE - temp_id INT; + temp_id UUID; BEGIN FOR temp_id IN SELECT id @@ -104,7 +109,26 @@ class SqlSystem { RETURN; 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) { console.error("InitDB Error: ", error);