Controllers added updated implementations and tests awaits
This commit is contained in:
41
Controllers/Postgres/config.py
Normal file
41
Controllers/Postgres/config.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
"""
|
||||
pool_pre_ping=True, # Verify connection before using
|
||||
pool_size=20, # Maximum number of permanent connections
|
||||
max_overflow=10, # Maximum number of additional connections
|
||||
pool_recycle=600, # Recycle connections after 1 hour
|
||||
pool_timeout=30, # Wait up to 30 seconds for a connection
|
||||
echo=True, # Set to True for debugging SQL queries
|
||||
"""
|
||||
|
||||
|
||||
class Configs(BaseSettings):
|
||||
"""
|
||||
Postgresql configuration settings.
|
||||
"""
|
||||
|
||||
DB: str = ""
|
||||
USER: str = ""
|
||||
PASSWORD: str = ""
|
||||
HOST: str = ""
|
||||
PORT: str = 0
|
||||
ENGINE: str = ""
|
||||
POOL_PRE_PING: bool = True
|
||||
POOL_SIZE: int = 20
|
||||
MAX_OVERFLOW: int = 10
|
||||
POOL_RECYCLE: int = 600
|
||||
POOL_TIMEOUT: int = 30
|
||||
ECHO: bool = True
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
"""Generate the database URL."""
|
||||
return f"{self.ENGINE}://{self.USER}:{self.PASSWORD}@{self.HOST}:{self.PORT}/{self.DB}"
|
||||
|
||||
model_config = SettingsConfigDict(env_prefix="POSTGRES_")
|
||||
|
||||
|
||||
postgres_configs = (
|
||||
Configs()
|
||||
) # singleton instance of the POSTGRESQL configuration settings
|
||||
61
Controllers/Postgres/database.py
Normal file
61
Controllers/Postgres/database.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from contextlib import contextmanager
|
||||
from functools import lru_cache
|
||||
from typing import Generator
|
||||
from config import postgres_configs
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker, scoped_session, Session
|
||||
|
||||
# Configure the database engine with proper pooling
|
||||
engine = create_engine(
|
||||
postgres_configs.url,
|
||||
pool_pre_ping=True, # Verify connection before using
|
||||
pool_size=20, # Maximum number of permanent connections
|
||||
max_overflow=10, # Maximum number of additional connections
|
||||
pool_recycle=600, # Recycle connections after 1 hour
|
||||
pool_timeout=30, # Wait up to 30 seconds for a connection
|
||||
echo=True, # Set to True for debugging SQL queries
|
||||
)
|
||||
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
# Create a cached session factory
|
||||
@lru_cache()
|
||||
def get_session_factory() -> scoped_session:
|
||||
"""Create a thread-safe session factory."""
|
||||
session_local = sessionmaker(
|
||||
bind=engine,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
expire_on_commit=True, # Prevent expired object issues
|
||||
)
|
||||
return scoped_session(session_local)
|
||||
|
||||
|
||||
# Get database session with proper connection management
|
||||
@contextmanager
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
"""Get database session with proper connection management.
|
||||
|
||||
This context manager ensures:
|
||||
- Proper connection pooling
|
||||
- Session cleanup
|
||||
- Connection return to pool
|
||||
- Thread safety
|
||||
|
||||
Yields:
|
||||
Session: SQLAlchemy session object
|
||||
"""
|
||||
session_factory = get_session_factory()
|
||||
session = session_factory()
|
||||
try:
|
||||
yield session
|
||||
session.commit()
|
||||
except Exception:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
session_factory.remove() # Clean up the session from the registry
|
||||
0
Controllers/Postgres/implementations.py
Normal file
0
Controllers/Postgres/implementations.py
Normal file
Reference in New Issue
Block a user