Skip to content

Commit

Permalink
Allow setting min_size and max_size in postgres DSN (#210)
Browse files Browse the repository at this point in the history
Overtakes #129
Closes #78
  • Loading branch information
vmarkovtsev authored Aug 10, 2020
1 parent d822465 commit 074c051
Show file tree
Hide file tree
Showing 2 changed files with 20 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
12 changes: 12 additions & 0 deletions tests/test_connection_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from databases.backends.aiopg import AiopgBackend
from databases.backends.mysql import MySQLBackend
from databases.backends.postgres import PostgresBackend
from databases.core import DatabaseURL
from tests.test_databases import DATABASE_URLS, async_adapter


def test_postgres_pool_size():
Expand All @@ -13,6 +15,16 @@ def test_postgres_pool_size():
assert kwargs == {"min_size": 1, "max_size": 20}


@async_adapter
async def test_postgres_pool_size_connect():
for url in DATABASE_URLS:
if DatabaseURL(url).dialect != "postgresql":
continue
backend = PostgresBackend(url + "?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 074c051

Please sign in to comment.