Optimized db.py for future use

pull/1030/head
kompotkot 2024-03-25 10:58:54 +00:00
rodzic d0cd24dd5f
commit 44b656fb9b
1 zmienionych plików z 103 dodań i 73 usunięć

Wyświetl plik

@ -13,46 +13,49 @@ from sqlalchemy.orm import Session, sessionmaker
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# try: try:
# MOONSTREAM_DB_URI = os.environ.get("MOONSTREAM_DB_URI") MOONSTREAM_DB_URI = os.environ.get("MOONSTREAM_DB_URI")
# if MOONSTREAM_DB_URI is None: if MOONSTREAM_DB_URI is None:
# raise Warning("MOONSTREAM_DB_URI environment variable must be set") raise Warning("MOONSTREAM_DB_URI environment variable must be set")
# MOONSTREAM_DB_URI_READ_ONLY = os.environ.get("MOONSTREAM_DB_URI_READ_ONLY") MOONSTREAM_DB_URI_READ_ONLY = os.environ.get("MOONSTREAM_DB_URI_READ_ONLY")
# if MOONSTREAM_DB_URI_READ_ONLY is None: if MOONSTREAM_DB_URI_READ_ONLY is None:
# raise Warning("MOONSTREAM_DB_URI_READ_ONLY environment variable must be set") raise Warning("MOONSTREAM_DB_URI_READ_ONLY environment variable must be set")
# MOONSTREAM_POOL_SIZE_RAW = os.environ.get("MOONSTREAM_POOL_SIZE") MOONSTREAM_POOL_SIZE_RAW = os.environ.get("MOONSTREAM_POOL_SIZE")
# MOONSTREAM_POOL_SIZE = 1 MOONSTREAM_POOL_SIZE = 1
# try: try:
# if MOONSTREAM_POOL_SIZE_RAW is not None: if MOONSTREAM_POOL_SIZE_RAW is not None:
# MOONSTREAM_POOL_SIZE = int(MOONSTREAM_POOL_SIZE_RAW) MOONSTREAM_POOL_SIZE = int(MOONSTREAM_POOL_SIZE_RAW)
# except: except:
# raise ValueError( raise ValueError(
# f"Could not parse MOONSTREAM_POOL_SIZE as int: {MOONSTREAM_POOL_SIZE_RAW}" f"Could not parse MOONSTREAM_POOL_SIZE as int: {MOONSTREAM_POOL_SIZE_RAW}"
# ) )
# MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW = os.environ.get( MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW = os.environ.get(
# "MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS" "MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS"
# ) )
# MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = 30000 MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = 30000
# try: try:
# if MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW is not None: if MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW is not None:
# MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = int( MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = int(
# MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW
# ) )
# except: except:
# raise ValueError( raise ValueError(
# f"MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS must be an integer: {MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW}" f"MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS must be an integer: {MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW}"
# ) )
# except ValueError as e: except ValueError as e:
# raise ValueError(e) raise ValueError(e)
# except Warning: except Warning:
# logger.warning("Database variables not set") logger.warning("Database variables not set")
def create_moonstream_engine( def create_moonstream_engine(
url: str, pool_size: int, statement_timeout: int, pool_pre_ping: bool = False url: str,
pool_size: int,
statement_timeout: int,
pool_pre_ping: bool = False,
): ):
# Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool # Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
# Statement timeout: https://stackoverflow.com/a/44936982 # Statement timeout: https://stackoverflow.com/a/44936982
@ -63,49 +66,76 @@ def create_moonstream_engine(
connect_args={"options": f"-c statement_timeout={statement_timeout}"}, connect_args={"options": f"-c statement_timeout={statement_timeout}"},
) )
# engine = create_moonstream_engine(
# url=MOONSTREAM_DB_URI, # type: ignore class MoonstreamDBEngine:
# pool_size=MOONSTREAM_POOL_SIZE, def __init__(self) -> None:
# statement_timeout=MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS, self._engine = create_moonstream_engine(
# ) url=MOONSTREAM_DB_URI, # type: ignore
# SessionLocal = sessionmaker(bind=engine) pool_size=MOONSTREAM_POOL_SIZE,
statement_timeout=MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS,
)
self._session_local = sessionmaker(bind=self.engine)
self._yield_db_session_ctx = contextmanager(self.yield_db_session)
@property
def engine(self):
return self._engine
@property
def session_local(self):
return self._session_local
@property
def yield_db_session_ctx(self):
return self._yield_db_session_ctx
def yield_db_session(
self,
) -> Generator[Session, None, None]:
"""
Yields a database connection (created using environment variables).
As per FastAPI docs:
https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency
"""
session = self._session_local()
try:
yield session
finally:
session.close()
# def yield_db_session() -> Generator[Session, None, None]: class MoonstreamDBEngineRO:
# """ def __init__(self) -> None:
# Yields a database connection (created using environment variables). self._RO_engine = create_moonstream_engine(
# As per FastAPI docs: url=MOONSTREAM_DB_URI_READ_ONLY, # type: ignore
# https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency pool_size=MOONSTREAM_POOL_SIZE,
# """ statement_timeout=MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS,
# session = SessionLocal() )
# try: self._RO_session_local = sessionmaker(bind=self.RO_engine)
# yield session
# finally:
# session.close()
self._RO_yield_db_session_ctx = contextmanager(self.yield_db_read_only_session)
# yield_db_session_ctx = contextmanager(yield_db_session) @property
def RO_engine(self):
return self._RO_engine
# # Read only @property
# RO_engine = create_moonstream_engine( def RO_session_local(self):
# url=MOONSTREAM_DB_URI_READ_ONLY, # type: ignore return self._RO_session_local
# pool_size=MOONSTREAM_POOL_SIZE,
# statement_timeout=MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS,
# )
# RO_SessionLocal = sessionmaker(bind=RO_engine)
@property
def RO_yield_db_session_ctx(self):
return self._RO_yield_db_session_ctx
# def yield_db_read_only_session() -> Generator[Session, None, None]: def yield_db_read_only_session(self) -> Generator[Session, None, None]:
# """ """
# Yields a database connection (created using environment variables). Yields read-only database connection (created using environment variables).
# As per FastAPI docs: As per FastAPI docs:
# https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency https://fastapi.tiangolo.com/tutorial/sql-databases/#create-a-dependency
# """ """
# session = RO_SessionLocal() session = self._RO_session_local()
# try: try:
# yield session yield session
# finally: finally:
# session.close() session.close()
# yield_db_read_only_session_ctx = contextmanager(yield_db_read_only_session)