Skip to content

Commit 0844c8b

Browse files
committed
Merge branch 'issue_230' of git://github.com/akaariai/django-debug-toolbar into akaariai-issue_230
Conflicts: debug_toolbar/utils/tracking/db.py
2 parents 42d7dde + 54f4f3a commit 0844c8b

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

debug_toolbar/utils/tracking/db.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ def execute(self, sql, params=()):
102102
try:
103103
return self.cursor.execute(sql, params)
104104
finally:
105+
# FIXME: Sometimes connections which are not in the connections
106+
# dict are used (for example in test database destroying).
107+
# The code below (at least get_transaction_id(alias) needs to have
108+
# the connection in the connections dict. It would be good to
109+
# not have this requirement at all, but for now lets just skip
110+
# these connections.
111+
if self.db.alias not in connections:
112+
return
105113
stop = datetime.now()
106114
duration = ms_from_timedelta(stop - start)
107115
enable_stacktraces = getattr(settings,
@@ -131,7 +139,7 @@ def execute(self, sql, params=()):
131139
del cur_frame
132140

133141
alias = getattr(self.db, 'alias', 'default')
134-
conn = connections[alias].connection
142+
conn = self.db.connection
135143
# HACK: avoid imports
136144
if conn:
137145
engine = conn.__class__.__module__.split('.', 1)[0]
@@ -158,11 +166,17 @@ def execute(self, sql, params=()):
158166
}
159167

160168
if engine == 'psycopg2':
161-
from psycopg2.extensions import TRANSACTION_STATUS_INERROR
169+
# If an erroneous query was ran on the connection, it might
170+
# be in a state where checking isolation_level raises an
171+
# exception.
172+
try:
173+
iso_level = conn.isolation_level
174+
except conn.InternalError:
175+
iso_level = 'unknown'
162176
params.update({
163177
'trans_id': self.logger.get_transaction_id(alias),
164178
'trans_status': conn.get_transaction_status(),
165-
'iso_level': conn.isolation_level if not conn.get_transaction_status() == TRANSACTION_STATUS_INERROR else "",
179+
'iso_level': iso_level,
166180
'encoding': conn.encoding,
167181
})
168182

runtests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env python
22
import sys
3+
import os
34
from os.path import dirname, abspath
45
from optparse import OptionParser
56

67
from django.conf import settings, global_settings
78

8-
if not settings.configured:
9+
# For convenience configure settings if they are not pre-configured or if we
10+
# haven't been provided settings to use by environment variable.
11+
if not settings.configured and not os.environ.get('DJANGO_SETTINGS_MODULE'):
912
settings.configure(
1013
DATABASES={
1114
'default': {

test_pgsql.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.conf import global_settings
2+
DATABASES={
3+
'default': {
4+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
5+
# Edit the below settings before use...
6+
'USER': '',
7+
'NAME': '',
8+
'HOST': '',
9+
'PASSWORD': '',
10+
}
11+
}
12+
INSTALLED_APPS=[
13+
'django.contrib.auth',
14+
'django.contrib.admin',
15+
'django.contrib.contenttypes',
16+
'django.contrib.sessions',
17+
'django.contrib.sites',
18+
19+
'debug_toolbar',
20+
21+
'tests',
22+
]
23+
MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES + (
24+
'debug_toolbar.middleware.DebugToolbarMiddleware',
25+
)
26+
ROOT_URLCONF=''
27+
DEBUG=False
28+
SITE_ID=1

tests/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from django.conf import settings
44
from django.contrib.auth.models import User
5+
from django.db import connection
56
from django.http import HttpResponse
67
from django.test import TestCase, RequestFactory
78
from django.template import Template, Context
9+
from django.utils import unittest
810

911
from debug_toolbar.middleware import DebugToolbarMiddleware
1012
from debug_toolbar.panels.sql import SQLDebugPanel
@@ -214,6 +216,19 @@ def test_recording(self):
214216
# ensure the stacktrace is populated
215217
self.assertTrue(len(query[1]['stacktrace']) > 0)
216218

219+
@unittest.skipUnless(connection.vendor=='postgresql',
220+
'Test valid only on PostgreSQL')
221+
def test_erroneous_query(self):
222+
"""
223+
Test that an error in the query isn't swallowed by the middleware.
224+
"""
225+
from django.db import connection
226+
from django.db.utils import DatabaseError
227+
try:
228+
connection.cursor().execute("erroneous query")
229+
except DatabaseError as e:
230+
self.assertTrue('erroneous query' in str(e))
231+
217232
def test_disable_stacktraces(self):
218233
panel = self.toolbar.get_panel(SQLDebugPanel)
219234
self.assertEquals(len(panel._queries), 0)

0 commit comments

Comments
 (0)