Switch: mariadb -> postgres

Addendum: MariaDB sucks dick with teeth
This commit is contained in:
Ulysia 2025-01-06 18:04:47 +01:00
parent c916075754
commit 5c106de4c1
3 changed files with 45 additions and 46 deletions

View file

@ -11,7 +11,7 @@ services:
environment: environment:
NODE_ENV: "production" NODE_ENV: "production"
DB_HOST: db DB_HOST: db
DB_USER: root DB_USER: postgres
DB_PASSWORD: example DB_PASSWORD: example
DATABASE: hypixel DATABASE: hypixel
volumes: volumes:
@ -21,21 +21,12 @@ services:
db: db:
image: mariadb:11.7.1-noble-rc image: postgres:17.2
restart: always restart: always
environment: environment:
MARIADB_ROOT_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
MARIADB_DATABASE: hypixel POSTGRES_DB: hypixel
volumes: volumes:
- ./db_data:/var/lib/mysql - ./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
## optional service for quick management of the database.
# adminer:
# image: adminer
# restart: always
# depends_on:
# - db
# ports:
# - 8080:8080

View file

@ -12,14 +12,15 @@
}, },
"dependencies": { "dependencies": {
"@types/node": "^22.10.2", "@types/node": "^22.10.2",
"@types/pg": "^8.11.10",
"@types/toastify-js": "^1.12.3", "@types/toastify-js": "^1.12.3",
"axios": "^0.24.0", "axios": "^0.24.0",
"copy-paste": "^1.5.3", "copy-paste": "^1.5.3",
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"express": "^4.21.2", "express": "^4.21.2",
"mariadb": "^3.4.0",
"node-notifier": "^10.0.1", "node-notifier": "^10.0.1",
"open": "^8.4.2", "open": "^8.4.2",
"pg": "^8.13.1",
"prismarine-nbt": "^2.7.0", "prismarine-nbt": "^2.7.0",
"socket.io": "^4.8.1", "socket.io": "^4.8.1",
"toastify-js": "^1.12.0", "toastify-js": "^1.12.0",

View file

@ -1,24 +1,31 @@
import mariadb from 'mariadb'; import { Pool, PoolClient } from 'pg';
import { loadConfig } from './configLoader'; import { loadConfig } from './configLoader';
const config = loadConfig(); const config = loadConfig();
class SqlSystem { class SqlSystem {
private static pool: mariadb.Pool = mariadb.createPool({ database: process.env.DATABASE, host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, connectionLimit: config.data.worker_count ?? 10 }) 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
})
public static async InitDB() { public static async InitDB() {
let conn: mariadb.PoolConnection | undefined; let conn: PoolClient | undefined;
try { try {
conn = await this.pool.getConnection(); conn = await this.pool.connect();
//Setup tables for auctions and lifetimes //Setup tables for auctions and lifetimes
await conn.query(` await conn.query(`
CREATE TABLE if NOT EXISTS auctions ( CREATE TABLE if NOT EXISTS auctions (
id INT AUTO_INCREMENT PRIMARY KEY, id UUID PRIMARY KEY,
auctionid VARCHAR(255) NOT NULL, auctionid VARCHAR(255) NOT NULL,
lbin DECIMAL(65,5) SIGNED, lbin DECIMAL(65,5),
UNIQUE INDEX 'auctionid' ('auctionid') UNIQUE (auctionid)
); );
CREATE TABLE if NOT EXISTS lifetimes (id INT PRIMARY KEY, insertedon DATETIME(2)); CREATE TABLE if NOT EXISTS lifetimes (id UUID PRIMARY KEY, insertedon DATETIME(2));
`); `);
//Setup lifetime Trigger //Setup lifetime Trigger
await conn.query(` await conn.query(`
@ -88,10 +95,10 @@ class SqlSystem {
} }
//INSERT ELEMENT IN AUCTIONS TABLE OR UPDATE IF IT ALREADY EXISTS //INSERT ELEMENT IN AUCTIONS TABLE OR UPDATE IF IT ALREADY EXISTS
public static async Upsert(auctionid: string|string[], lbin: number|number[]) { public static async Upsert(auctionid: string|string[], lbin: number|number[]) {
let conn: mariadb.PoolConnection | undefined; let conn: PoolClient | undefined;
try { try {
conn = await this.pool.getConnection(); conn = await this.pool.connect();
await conn.beginTransaction(); await conn.query('BEGIN');
if(Array.isArray(auctionid) && Array.isArray(lbin)) if(Array.isArray(auctionid) && Array.isArray(lbin))
if(auctionid.length === lbin.length) { if(auctionid.length === lbin.length) {
await conn.query(` await conn.query(`
@ -111,12 +118,12 @@ class SqlSystem {
LBin = VALUES(LBin); LBin = VALUES(LBin);
`, [auctionid, lbin]); `, [auctionid, lbin]);
} }
await conn.commit(); await conn.query('COMMIT');
} }
catch(error) catch(error)
{ {
console.error("message_before_error: ", error); console.error("message_before_error: ", error);
if(conn) await conn.rollback(); if (conn) await conn.query('ROLLBACK');
throw error; throw error;
} }
finally finally
@ -126,18 +133,18 @@ class SqlSystem {
} }
//REMOVE ELEMENT IN AUCTIONS TABLE //REMOVE ELEMENT IN AUCTIONS TABLE
public static async Remove(auctionid: string|string[]) { public static async Remove(auctionid: string|string[]) {
let conn: mariadb.PoolConnection | undefined; let conn: PoolClient | undefined;
try { try {
conn = await this.pool.getConnection(); conn = await this.pool.connect();
await conn.beginTransaction(); await conn.query('BEGIN');
await conn.query(` await conn.query(`
DELETE FROM auctions WHERE auctionid = ? DELETE FROM auctions WHERE auctionid = ?
`,[auctionid]); `,[auctionid]);
await conn.commit(); await conn.query('COMMIT');
} }
catch (error) { catch (error) {
console.error("InitDB Error: ", error); console.error("InitDB Error: ", error);
if(conn) await conn.rollback(); if (conn) await conn.query('ROLLBACK');
throw error; throw error;
} }
finally { finally {
@ -146,16 +153,16 @@ class SqlSystem {
} }
public static async Query(query:string,use_transaction:boolean = true) { public static async Query(query:string,use_transaction:boolean = true) {
let conn: mariadb.PoolConnection | undefined; let conn: PoolClient | undefined;
try{ try{
conn = await this.pool.getConnection() conn = await this.pool.connect()
if(use_transaction) await conn.beginTransaction(); if (use_transaction) await conn.query('BEGIN');
await conn.query(query); await conn.query(query);
if(use_transaction) await conn.commit(); if (use_transaction) await conn.query('COMMIT');
} }
catch(error) { catch(error) {
console.error("InitDB Error: ", error); console.error("InitDB Error: ", error);
if(use_transaction && conn) await conn.rollback(); if (use_transaction && conn) await conn.query('ROLLBACK');
throw error; throw error;
} }
finally finally
@ -165,10 +172,10 @@ class SqlSystem {
} }
//MATCH PROVIDED ELEMENTS IN AUCTIONS TABLE - returns true/false //MATCH PROVIDED ELEMENTS IN AUCTIONS TABLE - returns true/false
public static async Match(auctionid:string|string[],lbin:number|number[]): Promise<boolean|any> { public static async Match(auctionid:string|string[],lbin:number|number[]): Promise<boolean|any> {
let conn: mariadb.PoolConnection | undefined; let conn: PoolClient | undefined;
let result; let result;
try{ try{
conn = await this.pool.getConnection(); conn = await this.pool.connect();
if(Array.isArray(auctionid) && Array.isArray(lbin)) if(Array.isArray(auctionid) && Array.isArray(lbin))
if(auctionid.length === lbin.length) { if(auctionid.length === lbin.length) {
result = await conn.query(` result = await conn.query(`
@ -198,7 +205,7 @@ class SqlSystem {
END AS user_exists END AS user_exists
FROM auctions WHERE auctionID = ?; FROM auctions WHERE auctionID = ?;
`,[auctionid]); `,[auctionid]);
return Boolean(result[0].result); return Boolean(result.rows.length);
} }
} }
catch(error) catch(error)
@ -215,19 +222,19 @@ class SqlSystem {
//EXAMPLE BLOCK OF CODE FOR ADDING DATABASE FUNCTIONS //EXAMPLE BLOCK OF CODE FOR ADDING DATABASE FUNCTIONS
/* /*
public static async example_name() { public static async example_name() {
let conn:mariadb.PoolConnection|undefined; let conn:PoolClient|undefined;
try{ try{
conn = await this.pool.getConnection(); conn = await this.pool.connect();
await conn.beginTransaction(); await conn.query('BEGIN');
//CODE HERE// //CODE HERE//
await conn.commit(); await conn.query('COMMIT');
} }
catch(error) catch(error)
{ {
console.error("message_before_error: ", error); console.error("message_before_error: ", error);
if(conn) await conn.rollback(); if(conn) await conn.query('ROLLBACK');
throw error; throw error;
} }
finally finally