1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #35021 -- Fixed capturing queries when using client-side parameters binding with psycopg 3+.

This commit is contained in:
Michail Chatzis 2024-02-02 14:38:30 +02:00 committed by Mariusz Felisiak
parent 177e649396
commit 4426b1a72d
4 changed files with 30 additions and 3 deletions

View File

@ -105,6 +105,15 @@ class DatabaseFeatures(BaseDatabaseFeatures):
}, },
} }
) )
if self.uses_server_side_binding:
skips.update(
{
"The actual query cannot be determined for server side bindings": {
"backends.base.test_base.ExecuteWrapperTests."
"test_wrapper_debug",
}
},
)
return skips return skips
@cached_property @cached_property

View File

@ -296,9 +296,14 @@ class DatabaseOperations(BaseDatabaseOperations):
if is_psycopg3: if is_psycopg3:
def last_executed_query(self, cursor, sql, params): def last_executed_query(self, cursor, sql, params):
try: if self.connection.features.uses_server_side_binding:
return self.compose_sql(sql, params) try:
except errors.DataError: return self.compose_sql(sql, params)
except errors.DataError:
return None
else:
if cursor._query and cursor._query.query is not None:
return cursor._query.query.decode()
return None return None
else: else:

View File

@ -89,6 +89,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"db_functions.math.test_round.RoundTests." "db_functions.math.test_round.RoundTests."
"test_integer_with_negative_precision", "test_integer_with_negative_precision",
}, },
"The actual query cannot be determined on SQLite": {
"backends.base.test_base.ExecuteWrapperTests.test_wrapper_debug",
},
} }
if self.connection.is_in_memory_db(): if self.connection.is_in_memory_db():
skips.update( skips.update(

View File

@ -211,6 +211,16 @@ class ExecuteWrapperTests(TestCase):
self.assertEqual(connection.execute_wrappers, []) self.assertEqual(connection.execute_wrappers, [])
self.assertEqual(connections["other"].execute_wrappers, []) self.assertEqual(connections["other"].execute_wrappers, [])
def test_wrapper_debug(self):
def wrap_with_comment(execute, sql, params, many, context):
return execute(f"/* My comment */ {sql}", params, many, context)
with CaptureQueriesContext(connection) as ctx:
with connection.execute_wrapper(wrap_with_comment):
list(Person.objects.all())
last_query = ctx.captured_queries[-1]["sql"]
self.assertTrue(last_query.startswith("/* My comment */"))
class ConnectionHealthChecksTests(SimpleTestCase): class ConnectionHealthChecksTests(SimpleTestCase):
databases = {"default"} databases = {"default"}