Skip to content

Commit 863664d

Browse files
committed
Handle postgres CursorDebugWrapper in debugsqlshell.
1 parent 9efa204 commit 863664d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

debug_toolbar/management/commands/debugsqlshell.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import sqlparse
44
from django.core.management.commands.shell import Command # noqa
5-
from django.db.backends import utils as db_backends_utils
5+
from django.db import connection
6+
7+
if connection.vendor == "postgresql":
8+
from django.db.backends.postgresql import base as base_module
9+
else:
10+
from django.db.backends import utils as base_module
611

712
# 'debugsqlshell' is the same as the 'shell'.
813

914

10-
class PrintQueryWrapper(db_backends_utils.CursorDebugWrapper):
15+
class PrintQueryWrapper(base_module.CursorDebugWrapper):
1116
def execute(self, sql, params=()):
1217
start_time = time()
1318
try:
@@ -20,4 +25,4 @@ def execute(self, sql, params=()):
2025
print("{} [{:.2f}ms]".format(formatted_sql, duration))
2126

2227

23-
db_backends_utils.CursorDebugWrapper = PrintQueryWrapper
28+
base_module.CursorDebugWrapper = PrintQueryWrapper

tests/commands/test_debugsqlshell.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33

44
from django.contrib.auth.models import User
55
from django.core import management
6-
from django.db.backends import utils as db_backends_utils
6+
from django.db import connection
77
from django.test import TestCase
88
from django.test.utils import override_settings
99

10+
if connection.vendor == "postgresql":
11+
from django.db.backends.postgresql import base as base_module
12+
else:
13+
from django.db.backends import utils as base_module
14+
1015

1116
@override_settings(DEBUG=True)
1217
class DebugSQLShellTestCase(TestCase):
1318
def setUp(self):
14-
self.original_cursor_wrapper = db_backends_utils.CursorDebugWrapper
19+
self.original_wrapper = base_module.CursorDebugWrapper
1520
# Since debugsqlshell monkey-patches django.db.backends.utils, we can
1621
# test it simply by loading it, without executing it. But we have to
1722
# undo the monkey-patch on exit.
@@ -20,7 +25,7 @@ def setUp(self):
2025
management.load_command_class(app_name, command_name)
2126

2227
def tearDown(self):
23-
db_backends_utils.CursorDebugWrapper = self.original_cursor_wrapper
28+
base_module.CursorDebugWrapper = self.original_wrapper
2429

2530
def test_command(self):
2631
original_stdout, sys.stdout = sys.stdout, io.StringIO()

0 commit comments

Comments
 (0)