Skip to content

Commit 1da5779

Browse files
author
Francisco
committed
fix(db): isolate SPECIAL_DB_URL from container runtime
SPECIAL_DB_URL was previously loaded via env_file into every container and then silently overridden by the running_in_docker() guard. Moved to a host-shell-only .env.migrations file. Added a warning log in resolve_special_db_runtime_url so any future leak into a container is visible instead of silent.
1 parent 0560bd7 commit 1da5779

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

.env.migrations

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Host-shell only. Never load this into containers.
2+
# Use when running alembic migrations from the host:
3+
# source .env.migrations && alembic upgrade head
4+
# (PowerShell: Get-Content .env.migrations | ForEach-Object { ... })
5+
SPECIAL_DB_URL=mysql+pymysql://api_user:458b6b76b2b70e67357c75ade0ab75d28f00ec5fb9859401fe762421a5881bfc@localhost:3307/entities_db

.gitignore

20 Bytes
Binary file not shown.

src/api/entities_api/db/database.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
logging_utility = UtilsInterface.LoggingUtility()
1111

1212
# --- ALL ENGINE AND SESSION LOGIC IS NOW CENTRALIZED HERE ---
13-
1413
DATABASE_URL = os.getenv("DATABASE_URL")
1514
SPECIAL_DB_URL = os.getenv("SPECIAL_DB_URL")
1615

@@ -21,7 +20,24 @@ def running_in_docker() -> bool:
2120

2221

2322
def resolve_special_db_runtime_url(special_raw: str | None) -> str | None:
23+
"""
24+
Resolve the effective SPECIAL_DB_URL for the current runtime.
25+
26+
SPECIAL_DB_URL is intended for host-shell use only (see .env.migrations).
27+
Its value typically points at localhost:3307, which is meaningless inside
28+
containers. If it leaks into container env via .env / env_file, we ignore
29+
it and fall back to DATABASE_URL — and log a warning so the leak is
30+
visible to the operator.
31+
"""
2432
if running_in_docker():
33+
if special_raw:
34+
logging_utility.warning(
35+
"SPECIAL_DB_URL is set inside a container — ignoring and "
36+
"falling back to DATABASE_URL. SPECIAL_DB_URL is host-shell-only; "
37+
"it should live in .env.migrations, not .env. "
38+
"See: https://github.com/project-david-ai/projectdavid-core "
39+
"(container hygiene section)."
40+
)
2541
return DATABASE_URL
2642
return special_raw or None
2743

@@ -71,8 +87,6 @@ def get_db():
7187

7288
# Optional: You can also move the wait logic here to keep all DB startup
7389
# code together, which makes app.py even cleaner.
74-
75-
7690
def _wait_for_engine(engine_to_check, db_name, logger, retries=30, delay=3):
7791
if not engine_to_check:
7892
return

0 commit comments

Comments
 (0)