functional initDB and verified database functions

This commit is contained in:
Ulysia 2025-01-06 20:54:32 +01:00
parent ab61049de0
commit dfa75b046a
2 changed files with 41 additions and 15 deletions

View file

@ -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

View file

@ -4,16 +4,17 @@ const config = loadConfig();
class SqlSystem {
private static pool: Pool = new Pool({
database: process.env.DATABASE,
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
})
public static async InitDB() {
});
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
@ -105,6 +110,25 @@ class SqlSystem {
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);