Skip to content

Commit

Permalink
Merge pull request #86 from lsst-sqre/tickets/DM-33627
Browse files Browse the repository at this point in the history
DM-33627: Retain port setting in safir.database
  • Loading branch information
jonathansick authored Feb 24, 2022
2 parents 3e080de + 1731ba1 commit 7bbc252
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Change log
.. Headline template:
X.Y.Z (YYYY-MM-DD)
3.0.1 (2022-02-24)
==================

- ``safir.database`` retains the port in the database URL, if provided.

3.0.0 (2022-02-23)
==================

Expand Down
2 changes: 1 addition & 1 deletion docs/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ For applications using `Click`_ (the recommended way to implement a command-line
"--reset", is_flag=True, help="Delete all existing database data."
)
@run_with_asyncio
async def init() -> None:
async def init(reset: bool) -> None:
logger = structlog.get_logger(config.logger_name)
engine = create_database_engine(
config.database_url, config.database_password
Expand Down
2 changes: 2 additions & 0 deletions src/safir/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def _build_database_url(
parsed_url = parsed_url._replace(scheme="postgresql+asyncpg")
if password:
netloc = f"{parsed_url.username}:{password}@{parsed_url.hostname}"
if parsed_url.port:
netloc = f"{netloc}:{parsed_url.port}"
parsed_url = parsed_url._replace(netloc=netloc)
url = parsed_url.geturl()
return url
Expand Down
10 changes: 10 additions & 0 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ def test_build_database_url() -> None:
)
assert url == "postgresql+asyncpg://[email protected]/foo"

url = _build_database_url(
"postgresql://[email protected]:5432/foo", None, is_async=True
)
assert url == "postgresql+asyncpg://[email protected]:5432/foo"

url = _build_database_url(
"postgresql://[email protected]/foo", "otherpass", is_async=True
)
assert url == "postgresql+asyncpg://foo:[email protected]/foo"

url = _build_database_url(
"postgresql://[email protected]:5433/foo", "otherpass", is_async=True
)
assert url == "postgresql+asyncpg://foo:[email protected]:5433/foo"


@pytest.mark.asyncio
async def test_create_async_session() -> None:
Expand Down

0 comments on commit 7bbc252

Please sign in to comment.