Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 2321ff0

Browse files
Merge pull request #66 from encode/database-url-is-public-api
database.url should be public API
2 parents df3280d + 3b14fdc commit 2321ff0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

databases/core.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class Database:
2727
def __init__(
2828
self, url: typing.Union[str, "DatabaseURL"], *, force_rollback: bool = False
2929
):
30-
self._url = DatabaseURL(url)
30+
self.url = DatabaseURL(url)
3131
self._force_rollback = force_rollback
3232
self.is_connected = False
3333

34-
backend_str = self.SUPPORTED_BACKENDS[self._url.dialect]
34+
backend_str = self.SUPPORTED_BACKENDS[self.url.dialect]
3535
backend_cls = import_from_string(backend_str)
3636
assert issubclass(backend_cls, DatabaseBackend)
37-
self._backend = backend_cls(self._url)
37+
self._backend = backend_cls(self.url)
3838

3939
# Connections are stored as task-local state.
4040
self._connection_context = ContextVar("connection_context") # type: ContextVar
@@ -352,3 +352,6 @@ def __repr__(self) -> str:
352352
if self.password:
353353
url = str(self.replace(password="********"))
354354
return f"{self.__class__.__name__}({repr(url)})"
355+
356+
def __eq__(self, other: typing.Any) -> bool:
357+
return str(self) == str(other)

tests/test_databases.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,14 @@ async def test_queries_with_expose_backend_connection(database_url):
632632
# Raw output for the raw request
633633
assert result[1] == "example1"
634634
assert result[2] == True
635+
636+
637+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
638+
@async_adapter
639+
async def test_database_url_interface(database_url):
640+
"""
641+
Test that Database instances expose a `.url` attribute.
642+
"""
643+
async with Database(database_url) as database:
644+
assert isinstance(database.url, DatabaseURL)
645+
assert database.url == database_url

0 commit comments

Comments
 (0)