Skip to content

Commit 1e4b60e

Browse files
Merge branch 'main' of github.com:GraysonBellamy/nidaqlib
2 parents 50a609e + 19f2ca1 commit 1e4b60e

3 files changed

Lines changed: 139 additions & 94 deletions

File tree

src/nidaqlib/sinks/postgres.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import re
4242
from dataclasses import dataclass
4343
from typing import TYPE_CHECKING, Any, Self
44-
from urllib.parse import urlparse
44+
from urllib.parse import urlparse, urlunparse
4545

4646
from nidaqlib._logging import get_logger
4747
from nidaqlib.errors import (
@@ -219,17 +219,19 @@ def __post_init__(self) -> None:
219219
)
220220

221221
def target(self) -> str:
222-
"""Return a log-safe description: ``host:port/db.schema``."""
222+
"""Return a log-safe URI describing the connection target."""
223223
if self.dsn is not None:
224224
parsed = urlparse(self.dsn)
225225
host = parsed.hostname or "?"
226226
port = parsed.port or self.port
227227
db = (parsed.path or "/?").lstrip("/") or "?"
228+
scheme = parsed.scheme or "postgres"
228229
else:
229230
host = self.host or "?"
230231
port = self.port
231232
db = self.database or "?"
232-
return f"{host}:{port}/{db}.{self.schema}"
233+
scheme = "postgres"
234+
return urlunparse((scheme, f"{host}:{port}", f"/{db}.{self.schema}", "", "", ""))
233235

234236

235237
def _load_asyncpg() -> Any:

tests/unit/test_postgres_sink.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from __future__ import annotations
1010

11+
from urllib.parse import urlparse
12+
1113
import pytest
1214

1315
from nidaqlib.sinks import PostgresConfig, PostgresSink
@@ -38,8 +40,10 @@ def test_target_does_not_leak_password(self) -> None:
3840
config = PostgresConfig(
3941
dsn="postgres://user:hunter2@db.example.com:5433/prod",
4042
)
41-
assert "hunter2" not in config.target()
42-
assert "db.example.com" in config.target()
43+
target = config.target()
44+
parsed = urlparse(target)
45+
assert "hunter2" not in target
46+
assert parsed.hostname == "db.example.com"
4347

4448

4549
class TestPostgresSinkConstruction:

0 commit comments

Comments
 (0)