Skip to content

Commit

Permalink
Allow setting min_size and max_size in postgres DSN
Browse files Browse the repository at this point in the history
Overtakes encode#129
Closes encode#78
  • Loading branch information
vmarkovtsev committed May 27, 2020
1 parent 45519d7 commit 00bb442
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion databases/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ def _get_connection_kwargs(self) -> dict:
async def connect(self) -> None:
assert self._pool is None, "DatabaseBackend is already running"
kwargs = self._get_connection_kwargs()
self._pool = await asyncpg.create_pool(str(self._database_url), **kwargs)
self._pool = await asyncpg.create_pool(
host=self._database_url.hostname,
port=self._database_url.port,
user=self._database_url.username,
password=self._database_url.password,
database=self._database_url.database,
**kwargs,
)

async def disconnect(self) -> None:
assert self._pool is not None, "DatabaseBackend is not running"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_connection_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def test_postgres_pool_size():
assert kwargs == {"min_size": 1, "max_size": 20}


def test_postgres_pool_size_connect():
backend = PostgresBackend("postgres://localhost/database?min_size=1&max_size=20")
await backend.connect()
await backend.disconnect()


def test_postgres_explicit_pool_size():
backend = PostgresBackend("postgres://localhost/database", min_size=1, max_size=20)
kwargs = backend._get_connection_kwargs()
Expand Down

0 comments on commit 00bb442

Please sign in to comment.