summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Macpherson <scott@zerosleeps.com>2023-04-13 21:07:32 +1000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-04-14 10:11:33 +0200
commit53aee470d5b35e2708864d5221d2b5655e10c091 (patch)
tree83db18474eebdf57e8986244e254444798532382
parent813015d67e2557fa859a07930a9becec4e5f64a0 (diff)
downloaddjango-53aee470d5b35e2708864d5221d2b5655e10c091.tar.gz
Fixed #34486 -- Fixed DatabaseOperations.compose_sql() crash with no existing database connection on PostgreSQL.
Regression in 09ffc5c1212d4ced58b708cbbf3dfbfb77b782ca.
-rw-r--r--django/db/backends/postgresql/psycopg_any.py3
-rw-r--r--docs/releases/4.2.1.txt4
-rw-r--r--tests/backends/postgresql/tests.py10
3 files changed, 16 insertions, 1 deletions
diff --git a/django/db/backends/postgresql/psycopg_any.py b/django/db/backends/postgresql/psycopg_any.py
index 579104dead..1fe6b15caf 100644
--- a/django/db/backends/postgresql/psycopg_any.py
+++ b/django/db/backends/postgresql/psycopg_any.py
@@ -18,7 +18,8 @@ try:
TSTZRANGE_OID = types["tstzrange"].oid
def mogrify(sql, params, connection):
- return ClientCursor(connection.connection).mogrify(sql, params)
+ with connection.cursor() as cursor:
+ return ClientCursor(cursor.connection).mogrify(sql, params)
# Adapters.
class BaseTzLoader(TimestamptzLoader):
diff --git a/docs/releases/4.2.1.txt b/docs/releases/4.2.1.txt
index 84e3695cf4..f8bf6cc9d4 100644
--- a/docs/releases/4.2.1.txt
+++ b/docs/releases/4.2.1.txt
@@ -37,3 +37,7 @@ Bugfixes
* Fixed a regression in Django 4.2 where ``timesince`` and ``timeuntil``
template filters returned incorrect results for a datetime with a non-UTC
timezone when a time difference is less than 1 day (:ticket:`34483`).
+
+* Fixed a regression in Django 4.2 that caused a crash of
+ :class:`~django.contrib.postgres.search.SearchHeadline` function with
+ ``psycopg`` 3 (:ticket:`34486`).
diff --git a/tests/backends/postgresql/tests.py b/tests/backends/postgresql/tests.py
index 9c4512be24..947d51ea1e 100644
--- a/tests/backends/postgresql/tests.py
+++ b/tests/backends/postgresql/tests.py
@@ -420,3 +420,13 @@ class Tests(TestCase):
with self.assertRaisesMessage(NotSupportedError, msg):
connection.check_database_version_supported()
self.assertTrue(mocked_get_database_version.called)
+
+ def test_compose_sql_when_no_connection(self):
+ new_connection = connection.copy()
+ try:
+ self.assertEqual(
+ new_connection.ops.compose_sql("SELECT %s", ["test"]),
+ "SELECT 'test'",
+ )
+ finally:
+ new_connection.close()