Skip to content

Commit 93bcfd1

Browse files
alkihisLouis Bérangertim-schilling
authored
Add PRETTIFY_SQL setting to control token grouping in SQL panel (#1438)
feat: New parameter PRETTIFY_SQL Co-authored-by: Louis Béranger <[email protected]> Co-authored-by: Tim Schilling <[email protected]>
1 parent ea65261 commit 93bcfd1

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

debug_toolbar/panels/sql/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.utils.html import escape
55
from sqlparse import tokens as T
66

7+
from debug_toolbar import settings as dt_settings
8+
79

810
class BoldKeywordFilter:
911
"""sqlparse filter to bold SQL keywords"""
@@ -31,7 +33,8 @@ def reformat_sql(sql, with_toggle=False):
3133

3234
def parse_sql(sql, aligned_indent=False):
3335
stack = sqlparse.engine.FilterStack()
34-
stack.enable_grouping()
36+
if dt_settings.get_config()["PRETTIFY_SQL"]:
37+
stack.enable_grouping()
3538
if aligned_indent:
3639
stack.stmtprocess.append(
3740
sqlparse.filters.AlignedIndentFilter(char="&nbsp;", n="<br/>")

debug_toolbar/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"django.utils.deprecation",
3838
"django.utils.functional",
3939
),
40+
"PRETTIFY_SQL": True,
4041
"PROFILER_MAX_DEPTH": 10,
4142
"SHOW_TEMPLATE_CONTEXT": True,
4243
"SKIP_TEMPLATE_PREFIXES": ("django/forms/widgets/", "admin/widgets/"),

docs/configuration.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,39 @@ Panel options
180180
Useful for eliminating server-related entries which can result
181181
in enormous DOM structures and toolbar rendering delays.
182182

183+
* ``PRETTIFY_SQL``
184+
185+
Default: ``True``
186+
187+
Panel: SQL
188+
189+
Controls SQL token grouping.
190+
191+
Token grouping allows pretty print of similar tokens,
192+
like aligned indentation for every selected field.
193+
194+
When set to ``True``, it might cause render slowdowns
195+
when a view make long SQL textual queries.
196+
197+
**Without grouping**::
198+
199+
SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name"
200+
FROM "auth_user"
201+
WHERE "auth_user"."username" = '''test_username'''
202+
LIMIT 21
203+
204+
**With grouping**::
205+
206+
SELECT "auth_user"."id",
207+
"auth_user"."password",
208+
"auth_user"."last_login",
209+
"auth_user"."is_superuser",
210+
"auth_user"."username",
211+
"auth_user"."first_name",
212+
"auth_user"."last_name",
213+
FROM "auth_user"
214+
WHERE "auth_user"."username" = '''test_username'''
215+
LIMIT 21
183216

184217
* ``PROFILER_MAX_DEPTH``
185218

tests/panels/test_sql.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from django.shortcuts import render
1010
from django.test.utils import override_settings
1111

12+
from debug_toolbar import settings as dt_settings
13+
1214
from ..base import BaseTestCase
1315

1416
try:
@@ -357,3 +359,40 @@ def test_regression_infinite_recursion(self):
357359

358360
# ensure the stacktrace is populated
359361
self.assertTrue(len(query[1]["stacktrace"]) > 0)
362+
363+
@override_settings(
364+
DEBUG_TOOLBAR_CONFIG={"PRETTIFY_SQL": True},
365+
)
366+
def test_prettify_sql(self):
367+
"""
368+
Test case to validate that the PRETTIFY_SQL setting changes the output
369+
of the sql when it's toggled. It does not validate what it does
370+
though.
371+
"""
372+
list(User.objects.filter(username__istartswith="spam"))
373+
374+
response = self.panel.process_request(self.request)
375+
self.panel.generate_stats(self.request, response)
376+
pretty_sql = self.panel._queries[-1][1]["sql"]
377+
self.assertEqual(len(self.panel._queries), 1)
378+
379+
# Reset the queries
380+
self.panel._queries = []
381+
# Run it again, but with prettyify off. Verify that it's different.
382+
dt_settings.get_config()["PRETTIFY_SQL"] = False
383+
list(User.objects.filter(username__istartswith="spam"))
384+
response = self.panel.process_request(self.request)
385+
self.panel.generate_stats(self.request, response)
386+
self.assertEqual(len(self.panel._queries), 1)
387+
self.assertNotEqual(pretty_sql, self.panel._queries[-1][1]["sql"])
388+
389+
self.panel._queries = []
390+
# Run it again, but with prettyify back on.
391+
# This is so we don't have to check what PRETTIFY_SQL does exactly,
392+
# but we know it's doing something.
393+
dt_settings.get_config()["PRETTIFY_SQL"] = True
394+
list(User.objects.filter(username__istartswith="spam"))
395+
response = self.panel.process_request(self.request)
396+
self.panel.generate_stats(self.request, response)
397+
self.assertEqual(len(self.panel._queries), 1)
398+
self.assertEqual(pretty_sql, self.panel._queries[-1][1]["sql"])

0 commit comments

Comments
 (0)