Skip to content

Commit 24a7320

Browse files
olivermeyerclaude
andcommitted
fix(database): env var for db_name is NAME not DB_NAME
Rename field `db_name` to `name` in `DatabaseSettings` so pydantic-settings constructs the env var as `{PREFIX}NAME` (e.g. `MYAPP_DB_NAME`) instead of the confusing double-prefix `{PREFIX}DB_NAME`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ddcbb44 commit 24a7320

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

ATTRIBUTIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ SOFTWARE.
360360

361361
```
362362

363-
## aignostics-foundry-core (0.5.0) - MIT License
363+
## aignostics-foundry-core (0.6.1) - MIT License
364364

365365
🏭 Foundational infrastructure for Foundry components.
366366

src/aignostics_foundry_core/database.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DatabaseSettings(OpaqueSettings):
4848
pool_size: int = 10
4949
pool_max_overflow: int = 10
5050
pool_timeout: float = 30.0
51-
db_name: str | None = None
51+
name: str | None = None
5252

5353
def __init__(self, _env_prefix: str | None = None, **kwargs: Any) -> None: # noqa: ANN401
5454
"""Initialise settings, deriving env prefix from the active FoundryContext when not given.
@@ -68,17 +68,17 @@ def __init__(self, _env_prefix: str | None = None, **kwargs: Any) -> None: # no
6868
def get_url(self) -> str:
6969
"""Return the database URL string, optionally substituting the database name.
7070
71-
When :attr:`db_name` is set, the path component of the URL is replaced with
72-
``/{db_name}``, leaving the scheme, host, port, query, and fragment unchanged.
71+
When :attr:`name` is set, the path component of the URL is replaced with
72+
``/{name}``, leaving the scheme, host, port, query, and fragment unchanged.
7373
7474
Returns:
7575
The database URL as a plain string.
7676
"""
7777
raw = self.url.get_secret_value()
78-
if self.db_name is None:
78+
if self.name is None:
7979
return raw
8080
parsed = urllib.parse.urlparse(raw)
81-
return urllib.parse.urlunparse(parsed._replace(path=f"/{self.db_name}"))
81+
return urllib.parse.urlunparse(parsed._replace(path=f"/{self.name}"))
8282

8383

8484
# Global engine and session maker - initialized once per process and kept open

tests/aignostics_foundry_core/database_settings_test.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
OVERRIDE_POOL_SIZE = 5
2020
OVERRIDE_POOL_MAX_OVERFLOW = 20
2121
OVERRIDE_POOL_TIMEOUT = 60
22+
TEST_DB_PREFIX = "TEST_DB_"
23+
TEST_DB_NAME_ENV = "TEST_DB_NAME"
2224

2325

2426
@pytest.fixture(autouse=True)
@@ -43,17 +45,17 @@ def test_get_url_returns_plain_url_when_db_name_not_set() -> None:
4345

4446
@pytest.mark.unit
4547
def test_get_url_replaces_db_name_in_path() -> None:
46-
"""get_url() substitutes the path component when db_name is set."""
47-
settings = DatabaseSettings(_env_prefix="TEST_DB_", url=POSTGRES_URL, db_name="mydb")
48+
"""get_url() substitutes the path component when name is set."""
49+
settings = DatabaseSettings(_env_prefix="TEST_DB_", url=POSTGRES_URL, name="mydb")
4850
result = settings.get_url()
4951
assert result.endswith("/mydb")
5052
assert "postgres" not in result.split("/")[-1]
5153

5254

5355
@pytest.mark.unit
5456
def test_get_url_preserves_scheme_and_host() -> None:
55-
"""Scheme, host, and port are intact after db_name substitution."""
56-
settings = DatabaseSettings(_env_prefix="TEST_DB_", url=POSTGRES_URL, db_name="mydb")
57+
"""Scheme, host, and port are intact after name substitution."""
58+
settings = DatabaseSettings(_env_prefix="TEST_DB_", url=POSTGRES_URL, name="mydb")
5759
result = settings.get_url()
5860
assert result.startswith("postgresql+asyncpg://")
5961
assert "localhost:5432" in result
@@ -120,6 +122,18 @@ def test_pool_overrides_from_env(monkeypatch: pytest.MonkeyPatch) -> None:
120122
# ---------------------------------------------------------------------------
121123

122124

125+
@pytest.mark.unit
126+
def test_db_name_reads_from_name_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
127+
"""Setting {PREFIX}NAME populates db_name and get_url() substitutes the database name."""
128+
monkeypatch.setenv("TEST_DB_URL", POSTGRES_URL)
129+
monkeypatch.setenv(TEST_DB_NAME_ENV, "mydb")
130+
131+
settings = DatabaseSettings(_env_prefix=TEST_DB_PREFIX)
132+
133+
assert settings.name == "mydb"
134+
assert settings.get_url().endswith("/mydb")
135+
136+
123137
@pytest.mark.unit
124138
def test_url_is_masked_in_repr() -> None:
125139
"""repr(settings) / str(settings) does not expose the raw URL."""

0 commit comments

Comments
 (0)