Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow password as a callable when using Postgres backend #250

Merged
merged 2 commits into from Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions databases/backends/postgres.py
Expand Up @@ -44,7 +44,7 @@ def _get_dialect(self) -> Dialect:
def _get_connection_kwargs(self) -> dict:
url_options = self._database_url.options

kwargs = {}
kwargs = {} # type: typing.Dict[str, typing.Any]
min_size = url_options.get("min_size")
max_size = url_options.get("max_size")
ssl = url_options.get("ssl")
Expand All @@ -62,15 +62,15 @@ 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(
kwargs = dict(
host=self._database_url.hostname,
port=self._database_url.port,
user=self._database_url.username,
password=self._database_url.password,
nwillems marked this conversation as resolved.
Show resolved Hide resolved
database=self._database_url.database,
**kwargs,
)
kwargs.update(self._get_connection_kwargs())
self._pool = await asyncpg.create_pool(**kwargs)

async def disconnect(self) -> None:
assert self._pool is not None, "DatabaseBackend is not running"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_connection_options.py
Expand Up @@ -43,6 +43,24 @@ def test_postgres_explicit_ssl():
assert kwargs == {"ssl": True}


def test_postgres_no_extra_options():
backend = PostgresBackend("postgres://localhost/database")
kwargs = backend._get_connection_kwargs()
assert kwargs == {}


def test_postgres_password_as_callable():
def gen_password():
return "Foo"

backend = PostgresBackend(
"postgres://:password@localhost/database", password=gen_password
)
kwargs = backend._get_connection_kwargs()
assert kwargs == {"password": gen_password}
assert kwargs["password"]() == "Foo"


def test_mysql_pool_size():
backend = MySQLBackend("mysql://localhost/database?min_size=1&max_size=20")
kwargs = backend._get_connection_kwargs()
Expand Down