Files
romm/backend/decorators/database.py
Michael Manganiello a85c84a7d4 misc: Use single SQLAlchemy engine and session maker
As recommended by SQLAlchemy [1], this change makes a single
instantiation of the database engine and session maker, instead of one
entity per handler.

It also uses the provided `URL` constructor to better define the
database URL structure.

[1] https://docs.sqlalchemy.org/en/20/core/connections.html#basic-usage
2024-08-21 09:56:28 -03:00

26 lines
732 B
Python

import functools
from fastapi import HTTPException, status
from handler.database.base_handler import sync_session
from logger.logger import log
from sqlalchemy.exc import ProgrammingError
def begin_session(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if hasattr(kwargs, "session"):
return func(*args, **kwargs)
try:
with sync_session.begin() as s:
kwargs["session"] = s
return func(*args, **kwargs)
except ProgrammingError as exc:
log.critical(str(exc))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc
return wrapper