Skip to content

Commit

Permalink
Handle logging queries encoded as bytes under PostgreSQL (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucidiot committed Jul 7, 2023
1 parent 43c076a commit acd69df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ def _record(self, method, sql, params):
# Sql might be an object (such as psycopg Composed).
# For logging purposes, make sure it's str.
if vendor == "postgresql" and not isinstance(sql, str):
sql = sql.as_string(conn)
if isinstance(sql, bytes):
sql = sql.decode("utf-8")
else:
sql = sql.as_string(conn)
else:
sql = str(sql)

Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Pending
it collects the used static files in a ``ContextVar``.
* Added check ``debug_toolbar.W007`` to warn when JavaScript files are
resolving to the wrong content type.
* Fixed SQL statement recording under PostgreSQL for queries encoded as byte
strings.

4.1.0 (2023-05-15)
------------------
Expand Down
11 changes: 11 additions & 0 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ def test_non_ascii_query(self):
# ensure the panel renders correctly
self.assertIn("café", self.panel.content)

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

with connection.cursor() as cursor:
cursor.execute(b"SELECT 1")

self.assertEqual(len(self.panel._queries), 1)

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

Expand Down

0 comments on commit acd69df

Please sign in to comment.