Skip to content

Commit a0080f3

Browse files
committed
Rename SQLPanel context var to control SQL access.
The current build started failing for py3.8+ because the inner exception SQLQueryTriggered was raised in the test test_cursor_wrapper_asyncio_ctx. This was identified as being caused by sql.tracking.recording being set to false. The name recording made it seem as if this context var was controlling whether recording was occurring or not. However, it's true functionality was preventing SQL queries from being sent during the template panel's processing. Renaming that context var to allow_sql is more indicative of its purpose and makes the fixed test change clearer.
1 parent 1923f7e commit a0080f3

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

debug_toolbar/panels/sql/tracking.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
except ImportError:
1414
PostgresJson = None
1515

16-
recording = contextvars.ContextVar("debug-toolbar-recording", default=True)
16+
# Prevents SQL queries from being sent to the DB. It's used
17+
# by the TemplatePanel to prevent the toolbar from issuing
18+
# additional queries.
19+
allow_sql = contextvars.ContextVar("debug-toolbar-allow-sql", default=True)
1720

1821

1922
class SQLQueryTriggered(Exception):
@@ -32,7 +35,7 @@ def cursor(*args, **kwargs):
3235
# See:
3336
# https://github.com/jazzband/django-debug-toolbar/pull/615
3437
# https://github.com/jazzband/django-debug-toolbar/pull/896
35-
if recording.get():
38+
if allow_sql.get():
3639
wrapper = NormalCursorWrapper
3740
else:
3841
wrapper = ExceptionCursorWrapper
@@ -43,7 +46,7 @@ def chunked_cursor(*args, **kwargs):
4346
# solves https://github.com/jazzband/django-debug-toolbar/issues/1239
4447
cursor = connection._djdt_chunked_cursor(*args, **kwargs)
4548
if not isinstance(cursor, BaseCursorWrapper):
46-
if recording.get():
49+
if allow_sql.get():
4750
wrapper = NormalCursorWrapper
4851
else:
4952
wrapper = ExceptionCursorWrapper

debug_toolbar/panels/templates/panel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.utils.translation import gettext_lazy as _
1414

1515
from debug_toolbar.panels import Panel
16-
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, recording
16+
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, allow_sql
1717
from debug_toolbar.panels.templates import views
1818

1919
# Monkey-patch to enable the template_rendered signal. The receiver returns
@@ -118,7 +118,7 @@ def _store_template_info(self, sender, **kwargs):
118118
value.model._meta.label,
119119
)
120120
else:
121-
token = recording.set(False)
121+
token = allow_sql.set(False)
122122
try:
123123
saferepr(value) # this MAY trigger a db query
124124
except SQLQueryTriggered:
@@ -130,7 +130,7 @@ def _store_template_info(self, sender, **kwargs):
130130
else:
131131
temp_layer[key] = value
132132
finally:
133-
recording.reset(token)
133+
allow_sql.reset(token)
134134
pformatted = pformat(temp_layer)
135135
self.pformat_layers.append((context_layer, pformatted))
136136
context_list.append(pformatted)

tests/panels/test_sql.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from ..base import BaseTestCase
2020
from ..models import PostgresJSON
21-
from ..sync import database_sync_to_async
2221

2322

2423
def sql_call(use_iterator=False):
@@ -98,19 +97,21 @@ async def test_cursor_wrapper_async(self, mock_wrapper):
9897
wraps=sql_tracking.NormalCursorWrapper,
9998
)
10099
async def test_cursor_wrapper_asyncio_ctx(self, mock_wrapper):
101-
self.assertTrue(sql_tracking.recording.get())
100+
self.assertTrue(sql_tracking.allow_sql.get())
102101
await sync_to_async(sql_call)()
103102

104103
async def task():
105-
sql_tracking.recording.set(False)
106-
# Calling this in another context requires the db connections
107-
# to be closed properly.
108-
await database_sync_to_async(sql_call)()
104+
sql_tracking.allow_sql.set(False)
105+
# By disabling sql_tracking.allow_sql, we are indicating that any
106+
# future SQL queries should be stopped. If SQL query occurs,
107+
# it raises an exception.
108+
with self.assertRaises(sql_tracking.SQLQueryTriggered):
109+
await sync_to_async(sql_call)()
109110

110111
# Ensure this is called in another context
111112
await asyncio.create_task(task())
112113
# Because it was called in another context, it should not have affected ours
113-
self.assertTrue(sql_tracking.recording.get())
114+
self.assertTrue(sql_tracking.allow_sql.get())
114115
self.assertEqual(mock_wrapper.call_count, 1)
115116

116117
def test_generate_server_timing(self):

0 commit comments

Comments
 (0)