Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/database/setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import functools

from loguru import logger
from sqlalchemy.engine import URL
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

from config import DatabaseConfiguration, get_config

_user_engine = None
_expdb_engine = None


def _create_engine(db_config: DatabaseConfiguration) -> AsyncEngine:
db_url = URL.create(
Expand All @@ -16,33 +16,36 @@ def _create_engine(db_config: DatabaseConfiguration) -> AsyncEngine:
port=db_config.port,
database=db_config.database,
)

logger.info("Creating database engine for {db_url}", db_url=db_url)
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Reduce database URL exposure in info logs.

Line 20 and Line 42 log connection URLs at info level. Even with password masking, this can leak infrastructure/user metadata into normal production logs. Prefer a redacted identifier (e.g., db name alias) or move full URL logging to debug.

Also applies to: 42-42

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/database/setup.py` at line 20, The info-level logs currently print the
full db_url via logger.info("Creating database engine for {db_url}",
db_url=db_url) (and the similar call around line 42); change these to avoid
exposing the full URL: either log a redacted identifier/alias (e.g., derive a
short db_name or masked string from db_url) at info level, and move the full
db_url logging to logger.debug(...), or simply change the present
logger.info(...) calls to logger.debug(...) and add a new logger.info(...) that
prints only the redacted alias; update both occurrences that reference db_url.

return create_async_engine(
db_url,
echo=db_config.echo,
pool_recycle=3600,
)


@functools.cache
def user_database() -> AsyncEngine:
global _user_engine # noqa: PLW0603
if _user_engine is None:
_user_engine = _create_engine(get_config().openml_database)
return _user_engine
return _create_engine(get_config().openml_database)


@functools.cache
def expdb_database() -> AsyncEngine:
global _expdb_engine # noqa: PLW0603
if _expdb_engine is None:
_expdb_engine = _create_engine(get_config().expdb_database)
return _expdb_engine
return _create_engine(get_config().expdb_database)


async def close_databases() -> None:
"""Close all database connections."""
global _user_engine, _expdb_engine # noqa: PLW0603
if _user_engine is not None:
await _user_engine.dispose()
_user_engine = None
if _expdb_engine is not None:
await _expdb_engine.dispose()
_expdb_engine = None
for db in (user_database, expdb_database):
if db.cache_info().currsize == 1:
engine = db()
logger.info("Disposing of engine connected to {db_url}", db_url=engine.url)
try:
await engine.dispose()
except Exception: # noqa: BLE001
logger.exception(
"Issue disposing of database engine for {db_url}",
db_url=engine.url,
)
db.cache_clear()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Loading