Skip to content

Commit

Permalink
support for django 5.1 (#94)
Browse files Browse the repository at this point in the history
* added support for django 5.1

* add python 3.13 tests to github action
drop support for django < 4.2

* new version 4.0.8

---------

Co-authored-by: Javier Cordero <[email protected]>
  • Loading branch information
jneight and Javier Cordero authored Feb 23, 2025
1 parent e573357 commit 986782a
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 33 deletions.
28 changes: 20 additions & 8 deletions .github/workflows/ci_psycopg2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,40 @@ jobs:
include:
- os: ubuntu-latest
python-version: '3.8'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.9'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.8'
python-version: '3.11'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.9'
python-version: '3.12'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.11'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.12'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.13'
django-version: '5.1'
services:
postgres:
image: postgres
Expand Down
28 changes: 20 additions & 8 deletions .github/workflows/ci_psycopg2_gis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,40 @@ jobs:
include:
- os: ubuntu-latest
python-version: '3.8'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.9'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '3.2.23'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.8'
python-version: '3.11'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.9'
python-version: '3.12'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.11'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.12'
django-version: '4.2.9'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.13'
django-version: '5.1'
services:
postgres:
image: postgis/postgis
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/ci_psycopg3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ jobs:
- os: ubuntu-latest
python-version: '3.12'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.13'
django-version: '5.1'
services:
postgres:
image: postgres
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/ci_psycopg3_gis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ jobs:
- os: ubuntu-latest
python-version: '3.12'
django-version: '4.2.9'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.0.8'
- os: ubuntu-latest
python-version: '3.10'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.11'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.12'
django-version: '5.1'
- os: ubuntu-latest
python-version: '3.13'
django-version: '5.1'
services:
postgres:
image: postgis/postgis
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Settings
> persistent connection feature. And read below note if you are
> manually spawning greenlets
For Django < 5.1:
``` {.python}
DATABASES = {
'default': {
Expand All @@ -88,6 +90,28 @@ DATABASES = {
}
```

For Django >= 5.1, native pool support should be disabled:

``` {.python}
DATABASES = {
'default': {
'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg',
'NAME': 'db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': False,
'CONN_MAX_AGE': 0,
'OPTIONS': {
'MAX_CONNS': 20,
'REUSE_CONNS': 10.
'pool': False,
}
}
}
```

Using ORM when not serving requests
-----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion django_db_geventpool/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 'v4.0.7'
version = 'v4.0.8'
21 changes: 11 additions & 10 deletions django_db_geventpool/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

logger = logging.getLogger("django.geventpool")

connection_pools = {}
connection_pools_lock = Semaphore(value=1)


class DatabaseWrapperMixin(object):
class DatabaseWrapperMixin:
pool_class = None
creation_class = DatabaseCreation
INTRANS = None
_connection_pools = {}


def __init__(self, *args, **kwargs):
self._pool = None
Expand All @@ -29,11 +30,11 @@ def pool(self):
if self._pool is not None:
return self._pool
with connection_pools_lock:
if self.alias not in connection_pools:
self._pool = self.pool_class(super(), **self.get_connection_params(), connect=lambda parent, **kw: parent.get_new_connection(conn_params=kw))
connection_pools[self.alias] = self._pool
if self.alias not in self._connection_pools:
self._pool = self.pool_class(**self.get_connection_params())
self._connection_pools[self.alias] = self._pool
else:
self._pool = connection_pools[self.alias]
self._pool = self._connection_pools[self.alias]
return self._pool

def get_new_connection(self, conn_params: dict):
Expand Down Expand Up @@ -62,7 +63,7 @@ def close(self):
# will occur at every request.
self.connection = None
logger.warning(
"psycopg2 error while closing the connection.", exc_info=sys.exc_info()
"psycopg error while closing the connection.", exc_info=sys.exc_info()
)
raise
finally:
Expand All @@ -74,7 +75,7 @@ def close_if_unusable_or_obsolete(self):

def _close(self):
if self.connection.closed:
self.pool.closeall()
self.pool.close()
else:
if self.connection.info.transaction_status == self.INTRANS:
self.connection.rollback()
Expand All @@ -84,8 +85,8 @@ def _close(self):
self.connection = None

def closeall(self):
for pool in connection_pools.values():
pool.closeall()
for pool in self._connection_pools.values():
pool.close()

def set_clean(self):
if self.in_atomic_block:
Expand Down
3 changes: 1 addition & 2 deletions django_db_geventpool/backends/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
)


class DatabaseCreationMixin(object):
class DatabaseCreationMixin:
def _create_test_db(self, verbosity, autoclobber, keepdb=False):
self.connection.closeall()
return super()._create_test_db(verbosity, autoclobber, keepdb)

def _destroy_test_db(self, test_database_name, verbosity):
Expand Down
3 changes: 2 additions & 1 deletion django_db_geventpool/backends/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def put(self, item):
item.close()
self._conns.discard(item)

def closeall(self):
def close(self):
while not self.pool.empty():
try:
conn = self.pool.get_nowait()
Expand All @@ -82,3 +82,4 @@ def closeall(self):
self._conns.discard(conn)

logger.debug("DB connections all closed")

3 changes: 3 additions & 0 deletions django_db_geventpool/backends/postgresql_psycopg3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ def check_usable(self, connection):
class DatabaseWrapper(base.DatabaseWrapperMixin, OriginalDatabaseWrapper):
pool_class = PostgresConnectionPool
INTRANS = psycopg.pq.TransactionStatus.INTRANS



2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Application Frameworks",
]
dependencies = [
"django>=3.1",
"django>=4.2",
]

[project.urls]
Expand Down
5 changes: 5 additions & 0 deletions runtests_psycopg3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import sys
import gevent.monkey
from distutils.version import StrictVersion

gevent.monkey.patch_all()

Expand Down Expand Up @@ -28,6 +29,10 @@
),
USE_TZ=True,
)

if StrictVersion(django.get_version()) >= StrictVersion('5.1.0'):
settings.DATABASES['default']["OPTIONS"] = {"pool": False}

django.setup()

test_runner = DiscoverRunner(verbosity=2)
Expand Down
5 changes: 5 additions & 0 deletions runtests_psycopg3_gis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import sys
import gevent.monkey
from distutils.version import StrictVersion

gevent.monkey.patch_all()

Expand Down Expand Up @@ -28,6 +29,10 @@
),
USE_TZ=True,
)

if StrictVersion(django.get_version()) >= StrictVersion('5.1.0'):
settings.DATABASES['default']["OPTIONS"] = {"pool": False}

django.setup()

test_runner = DiscoverRunner(verbosity=2)
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ python =
3.10: py310
3.11: py311
3.12: py312
3.13: py313

[tox]
envlist =
py3{8,9,10,11,12}-dj{32,42}-pg{2-gis,2}
py3{8,9,10,11,12}-dj{42}-pg{2-gis,2}
py3{8,9,10,11,12}-dj{42}-pg{3-gis,3}
py3{10,11,12}-dj{50}-pg{2-gis,2,3-gis,3}
py3{10,11,12,13}-dj{51}-pg{3-gis,3}

[testenv]
deps =
gevent
psycogreen
dj32: django~=3.2
dj42: django~=4.2
dj50: django~=5.0
dj51: django~=5.1
pg2-gis: psycopg2-binary
pg2: psycopg2-binary
pg3-gis: psycopg[binary,pool]
Expand Down

0 comments on commit 986782a

Please sign in to comment.