up stuff
This commit is contained in:
parent
656c3fd414
commit
1c17b57d8a
37 changed files with 2818 additions and 469 deletions
38
oedb/utils/db.py
Normal file
38
oedb/utils/db.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
"""
|
||||
Database utility functions for the OpenEventDatabase.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
from oedb.utils.logging import logger
|
||||
|
||||
def db_connect():
|
||||
"""
|
||||
Connect to the PostgreSQL database using environment variables.
|
||||
|
||||
Returns:
|
||||
psycopg2.connection: A connection to the database.
|
||||
"""
|
||||
return psycopg2.connect(
|
||||
dbname=os.getenv("DB_NAME", "oedb"),
|
||||
host=os.getenv("DB_HOST", ""),
|
||||
password=os.getenv("POSTGRES_PASSWORD", None),
|
||||
user=os.getenv("DB_USER", ""))
|
||||
|
||||
def check_db_connection():
|
||||
"""
|
||||
Check if the database is accessible.
|
||||
|
||||
Returns:
|
||||
bool: True if the database is accessible, False otherwise.
|
||||
"""
|
||||
try:
|
||||
conn = db_connect()
|
||||
conn.close()
|
||||
logger.success("Successfully connected to PostgreSQL database")
|
||||
return True
|
||||
except psycopg2.OperationalError as e:
|
||||
logger.error(f"Failed to connect to PostgreSQL database: {e}")
|
||||
return False
|
Loading…
Add table
Add a link
Reference in a new issue