This repository was archived by the owner on Aug 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -246,6 +246,11 @@ async def rollback(self) -> None:
246
246
await self ._connection .__aexit__ ()
247
247
248
248
249
+ class _EmptyNetloc (str ):
250
+ def __bool__ (self ):
251
+ return True
252
+
253
+
249
254
class DatabaseURL :
250
255
def __init__ (self , url : typing .Union [str , "DatabaseURL" ]):
251
256
self ._url = str (url )
@@ -282,6 +287,10 @@ def hostname(self) -> typing.Optional[str]:
282
287
def port (self ) -> typing .Optional [int ]:
283
288
return self .components .port
284
289
290
+ @property
291
+ def netloc (self ) -> typing .Optional [str ]:
292
+ return self .components .netloc
293
+
285
294
@property
286
295
def database (self ) -> str :
287
296
return self .components .path .lstrip ("/" )
@@ -317,6 +326,11 @@ def replace(self, **kwargs: typing.Any) -> "DatabaseURL":
317
326
driver = kwargs .pop ("driver" , self .driver )
318
327
kwargs ["scheme" ] = f"{ dialect } +{ driver } " if driver else dialect
319
328
329
+ if not kwargs .get ("netloc" , self .netloc ):
330
+ # Using an empty string that evaluates as True means we end
331
+ # up with URLs like `sqlite:///database` instead of `sqlite:/database`
332
+ kwargs ["netloc" ] = _EmptyNetloc ()
333
+
320
334
components = self .components ._replace (** kwargs )
321
335
return self .__class__ (components .geturl ())
322
336
Original file line number Diff line number Diff line change @@ -40,3 +40,9 @@ def test_replace_database_url_components():
40
40
new = u .replace (port = 123 )
41
41
assert new .port == 123
42
42
assert str (new ) == "postgresql://localhost:123/mydatabase"
43
+
44
+ u = DatabaseURL ("sqlite:///mydatabase" )
45
+ assert u .database == "mydatabase"
46
+ new = u .replace (database = "test_" + u .database )
47
+ assert new .database == "test_mydatabase"
48
+ assert str (new ) == "sqlite:///test_mydatabase"
You can’t perform that action at this time.
0 commit comments