Skip to content

Commit

Permalink
Fix Postgres JSON Explain when getting tuple as params (#1722)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaligniana committed Dec 28, 2022
1 parent fbda63d commit c919827
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def _quote_params(self, params):
return params
if isinstance(params, dict):
return {key: self._quote_expr(value) for key, value in params.items()}
if isinstance(params, tuple):
return tuple(self._quote_expr(p) for p in params)
return [self._quote_expr(p) for p in params]

def _decode(self, param):
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change log
Pending
-------

* Fixed PostgreSQL raw query with a tuple parameter during on explain.

3.8.1 (2022-12-03)
------------------

Expand Down
20 changes: 20 additions & 0 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ def test_json_param_conversion(self):
'["{\\"foo\\": \\"bar\\"}"]',
)

@unittest.skipUnless(
connection.vendor == "postgresql", "Test valid only on PostgreSQL"
)
def test_tuple_param_conversion(self):
self.assertEqual(len(self.panel._queries), 0)

list(
PostgresJSON.objects.raw(
"SELECT * FROM tests_postgresjson WHERE field ->> 'key' IN %s",
[("a", "b'")],
)
)

response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)

# ensure query was logged
self.assertEqual(len(self.panel._queries), 1)
self.assertEqual(self.panel._queries[0]["params"], '[["a", "b\'"]]')

def test_binary_param_force_text(self):
self.assertEqual(len(self.panel._queries), 0)

Expand Down

0 comments on commit c919827

Please sign in to comment.