Refactor DB classes and management #5

Merged
dylan merged 7 commits from refactor/db-class into main 2026-03-03 11:17:50 +00:00
2 changed files with 18 additions and 9 deletions
Showing only changes of commit 8b8462fd58 - Show all commits

View File

@@ -1,8 +1,9 @@
import os import os
import psycopg2 import psycopg2
import pandas as pd
from psycopg2.extras import RealDictCursor from psycopg2.extras import RealDictCursor
from psycopg2.extras import execute_batch, Json from psycopg2.extras import execute_batch
from server.exceptions import DatabaseNotConfiguredException
class PostgresConnector: class PostgresConnector:
@@ -11,6 +12,8 @@ class PostgresConnector:
""" """
def __init__(self): def __init__(self):
try:
self.connection = psycopg2.connect( self.connection = psycopg2.connect(
host=os.getenv("POSTGRES_HOST", "localhost"), host=os.getenv("POSTGRES_HOST", "localhost"),
port=os.getenv("POSTGRES_PORT", 5432), port=os.getenv("POSTGRES_PORT", 5432),
@@ -18,6 +21,9 @@ class PostgresConnector:
password=os.getenv("POSTGRES_PASSWORD", "postgres"), password=os.getenv("POSTGRES_PASSWORD", "postgres"),
database=os.getenv("POSTGRES_DB", "postgres"), database=os.getenv("POSTGRES_DB", "postgres"),
) )
except psycopg2.OperationalError as e:
raise DatabaseNotConfiguredException(f"Ensure database is up and running: {e}")
self.connection.autocommit = False self.connection.autocommit = False
def execute(self, query, params=None, fetch=False) -> list: def execute(self, query, params=None, fetch=False) -> list:

View File

@@ -3,3 +3,6 @@ class NotAuthorisedException(Exception):
class NotExistentDatasetException(Exception): class NotExistentDatasetException(Exception):
pass pass
class DatabaseNotConfiguredException(Exception):
pass