Skip to content

Commit

Permalink
Fix psycopg3 detection (#956)
Browse files Browse the repository at this point in the history
Co-authored-by: Joachim Jablon <ewjoachim@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 1, 2024
1 parent 445ad89 commit e0170be
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/howto/django.md
Expand Up @@ -87,7 +87,7 @@ You can also use other subcommands such as `./manage.py procrastinate defer`.

:::{note}
Procrastinate generates an app for you using a `Psycopg` (by default) or
`Aiopg` connector depending on whether `psycopg3` or `aiopg` is
`Aiopg` connector depending on whether `psycopg` version 3 or `aiopg` is
installed, and connects using the `DATABASES` settings. If neither library is
installed, an error will be raised.
:::
Expand Down
4 changes: 3 additions & 1 deletion procrastinate/contrib/django/django_connector.py
Expand Up @@ -127,7 +127,9 @@ def get_worker_connector(self) -> connector.BaseAsyncConnector:
"""
alias = utils.get_setting("DATABASE_ALIAS", default="default")

if utils.package_is_installed("psycopg3"):
if utils.package_is_installed("psycopg") and utils.package_is_version(
"psycopg", 3
):
from procrastinate import psycopg_connector

return psycopg_connector.PsycopgConnector(
Expand Down
9 changes: 9 additions & 0 deletions procrastinate/contrib/django/utils.py
@@ -1,5 +1,6 @@
from __future__ import annotations

import importlib.metadata
import importlib.util
from typing import Any

Expand Down Expand Up @@ -36,3 +37,11 @@ def get_setting(name: str, *, default) -> Any:

def package_is_installed(name: str) -> bool:
return bool(importlib.util.find_spec(name))


def package_is_version(name: str, major: int) -> bool:
"""
Check if package's (PEP440) version matches given major version number
"""
version = importlib.metadata.version(name)
return bool(version) and version.split(".")[0] == f"{major}"
2 changes: 1 addition & 1 deletion tests/integration/contrib/django/test_django_connector.py
Expand Up @@ -82,7 +82,7 @@ def test_execute_query_sync(django_connector):
@pytest.mark.parametrize(
"installed, type",
[
({"psycopg3"}, psycopg_connector.PsycopgConnector),
({"psycopg"}, psycopg_connector.PsycopgConnector),
({"aiopg"}, aiopg_connector.AiopgConnector),
],
)
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/contrib/django/test_utils.py
Expand Up @@ -27,3 +27,18 @@ def test_get_settings_default():
)
def test_package_is_installed(package_name, expected):
assert utils.package_is_installed(package_name) is expected


@pytest.mark.parametrize(
"version, version_wanted,expected",
[
("3.1.3", 3, True),
("2.1.3", 3, False),
("pytest", 3, False),
(None, 3, False),
],
)
def test_package_is_version(version, version_wanted, expected, mocker):
mocker.patch("importlib.metadata.version", return_value=version)

assert utils.package_is_version("abcd", version_wanted) is expected

0 comments on commit e0170be

Please sign in to comment.