Refactor: Modularize configuration and implement robust DB bootstrap

This commit is contained in:
Scott Idem
2026-01-15 16:59:18 -05:00
parent 16c79aca39
commit 3f276a42e1
3 changed files with 136 additions and 33 deletions

View File

@@ -67,6 +67,36 @@ try:
except Exception:
log.exception('Could not connect to database.')
def reconnect_db() -> bool:
"""
Re-initializes the global database engine and connection using current settings.
Useful after bootstrapping new credentials from the 'cfg' table.
"""
global engine, db, db_uri
log.info("Refreshing database connection engine...")
try:
if engine:
engine.dispose()
log.info("Disposed of previous database engine.")
db_uri = settings.SQLALCHEMY_DB_URI
engine = create_engine(
url = db_uri,
echo = False,
pool_use_lifo = True,
pool_pre_ping = True,
pool_recycle = settings.DB['pool_recycle'],
isolation_level = 'READ COMMITTED',
connect_args = {'connect_timeout': settings.DB['connect_timeout']}
)
db = engine.connect()
log.info(f"Database connection re-established successfully: {db_uri}")
return True
except Exception:
log.exception("FAILED to refresh database connection!")
return False
# ### BEGIN ### API DB SQL ### sql_connect() ###
@logger_reset
def sql_connect(current_db, log_lvl: int = logging.INFO) -> None|bool|int: