diff --git a/ephios/modellogging/models.py b/ephios/modellogging/models.py index 755245cc7..d870ee883 100644 --- a/ephios/modellogging/models.py +++ b/ephios/modellogging/models.py @@ -1,7 +1,11 @@ +import json + from django.conf import settings from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models +from django.db.models.signals import pre_save +from django.dispatch import receiver from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ @@ -76,3 +80,10 @@ def __str__(self): if self.content_object: return f"{self.action_type} {type(self.content_object)._meta.verbose_name} {str(self.content_object)}" return f"{self.action_type} {self.content_type.model} {self.content_object_or_str}" + + +@receiver(pre_save, sender=LogEntry) +def encode_data_for_psycopg3(sender, instance, raw, using, update_fields, **kwargs): + # psycopg3 does not allow read operations on the database while the model is being saved. + # details and source of this workaround: https://forum.djangoproject.com/t/connection-lock-hanging-after-upgrading-psycopg/30826/3 + instance.data = json.loads(LogJSONEncoder().encode(instance.data)) diff --git a/poetry.lock b/poetry.lock index 35978e2c9..c52a997fe 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2955,23 +2955,29 @@ files = [ ] [[package]] -name = "psycopg2" -version = "2.9.11" -description = "psycopg2 - Python-PostgreSQL Database Adapter" -optional = true -python-versions = ">=3.9" +name = "psycopg" +version = "3.3.2" +description = "PostgreSQL database adapter for Python" +optional = false +python-versions = ">=3.10" groups = ["main"] -markers = "extra == \"pgsql\"" files = [ - {file = "psycopg2-2.9.11-cp310-cp310-win_amd64.whl", hash = "sha256:103e857f46bb76908768ead4e2d0ba1d1a130e7b8ed77d3ae91e8b33481813e8"}, - {file = "psycopg2-2.9.11-cp311-cp311-win_amd64.whl", hash = "sha256:210daed32e18f35e3140a1ebe059ac29209dd96468f2f7559aa59f75ee82a5cb"}, - {file = "psycopg2-2.9.11-cp312-cp312-win_amd64.whl", hash = "sha256:e03e4a6dbe87ff81540b434f2e5dc2bddad10296db5eea7bdc995bf5f4162938"}, - {file = "psycopg2-2.9.11-cp313-cp313-win_amd64.whl", hash = "sha256:8dc379166b5b7d5ea66dcebf433011dfc51a7bb8a5fc12367fa05668e5fc53c8"}, - {file = "psycopg2-2.9.11-cp314-cp314-win_amd64.whl", hash = "sha256:f10a48acba5fe6e312b891f290b4d2ca595fc9a06850fe53320beac353575578"}, - {file = "psycopg2-2.9.11-cp39-cp39-win_amd64.whl", hash = "sha256:6ecddcf573777536bddfefaea8079ce959287798c8f5804bee6933635d538924"}, - {file = "psycopg2-2.9.11.tar.gz", hash = "sha256:964d31caf728e217c697ff77ea69c2ba0865fa41ec20bb00f0977e62fdcc52e3"}, + {file = "psycopg-3.3.2-py3-none-any.whl", hash = "sha256:3e94bc5f4690247d734599af56e51bae8e0db8e4311ea413f801fef82b14a99b"}, + {file = "psycopg-3.3.2.tar.gz", hash = "sha256:707a67975ee214d200511177a6a80e56e654754c9afca06a7194ea6bbfde9ca7"}, ] +[package.dependencies] +typing-extensions = {version = ">=4.6", markers = "python_version < \"3.13\""} +tzdata = {version = "*", markers = "sys_platform == \"win32\""} + +[package.extras] +binary = ["psycopg-binary (==3.3.2) ; implementation_name != \"pypy\""] +c = ["psycopg-c (==3.3.2) ; implementation_name != \"pypy\""] +dev = ["ast-comments (>=1.1.2)", "black (>=24.1.0)", "codespell (>=2.2)", "cython-lint (>=0.16)", "dnspython (>=2.1)", "flake8 (>=4.0)", "isort-psycopg", "isort[colors] (>=6.0)", "mypy (>=1.19.0)", "pre-commit (>=4.0.1)", "types-setuptools (>=57.4)", "types-shapely (>=2.0)", "wheel (>=0.37)"] +docs = ["Sphinx (>=5.0)", "furo (==2022.6.21)", "sphinx-autobuild (>=2021.3.14)", "sphinx-autodoc-typehints (>=1.12)"] +pool = ["psycopg-pool"] +test = ["anyio (>=4.0)", "mypy (>=1.19.0) ; implementation_name != \"pypy\"", "pproxy (>=2.7)", "pytest (>=6.2.5)", "pytest-cov (>=3.0)", "pytest-randomly (>=3.5)"] + [[package]] name = "py-vapid" version = "1.9.4" @@ -4522,10 +4528,10 @@ propcache = ">=0.2.1" [extras] mysql = ["mysqlclient"] -pgsql = ["psycopg2"] +pgsql = ["psycopg"] redis = ["redis"] [metadata] lock-version = "2.1" python-versions = "^3.10" -content-hash = "798acb173c9a4d11435e017df0a9bd3a29d4533e0d51451984400c1a038a40e4" +content-hash = "f32c7abfea98e3f1c273a0d23b469445ffeeb0ad934496655c042cc8e5470fba" diff --git a/pyproject.toml b/pyproject.toml index 183c28ce4..208d0913a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,7 @@ django-csp = "^4.0" bleach = ">=6.0.0,<7.0.0" Markdown = "^3.3.7" -psycopg2 = {version = "^2.9.3", optional = true} +psycopg = "^3.3.2" mysqlclient = {version = "^2.1.1", optional = true} redis = {extras = ["hiredis"], version = ">=5,<8", optional = true} django-compressor = "^4.5" @@ -62,7 +62,7 @@ cryptography = "^46.0.3" py-vapid = "^1.9.4" [tool.poetry.extras] -pgsql = ["psycopg2"] +pgsql = ["psycopg"] mysql = ["mysqlclient"] redis = ["redis"]