diff options
author | Anton Shurashov <sinkler@sinkler.ru> | 2018-05-09 16:21:18 +0300 |
---|---|---|
committer | Ashley Camba <ashwoods@gmail.com> | 2018-05-10 21:59:23 +0200 |
commit | c00cbda5b3dd6027ef54d26fda05dc30494d1189 (patch) | |
tree | 9424c248e5f47647fb6b2638c7a94f26784d8aac | |
parent | b07c0d58e43f2f76afb6d1b7c0d122c68ebfa7a0 (diff) | |
download | raven-c00cbda5b3dd6027ef54d26fda05dc30494d1189.tar.gz |
fix django sql hook
-rw-r--r-- | raven/contrib/django/client.py | 30 | ||||
-rw-r--r-- | tests/contrib/django/tests.py | 11 |
2 files changed, 27 insertions, 14 deletions
diff --git a/raven/contrib/django/client.py b/raven/contrib/django/client.py index a161342..0940208 100644 --- a/raven/contrib/django/client.py +++ b/raven/contrib/django/client.py @@ -83,6 +83,23 @@ def format_sql(sql, params): return sql, rv +def record_sql(vendor, alias, start, duration, sql, params): + def processor(data): + real_sql, real_params = format_sql(sql, params) + if real_params: + try: + real_sql = real_sql % tuple(real_params) + except TypeError: + pass + # maybe category to 'django.%s.%s' % (vendor, alias or + # 'default') ? + data.update({ + 'message': real_sql, + 'category': 'query', + }) + breadcrumbs.record(processor=processor) + + @once def install_sql_hook(): """If installed this causes Django's queries to be captured.""" @@ -99,19 +116,6 @@ def install_sql_hook(): # trickery would have to look different but I can't be bothered. return - def record_sql(vendor, alias, start, duration, sql, params): - def processor(data): - real_sql, real_params = format_sql(sql, params) - if real_params: - real_sql = real_sql % tuple(real_params) - # maybe category to 'django.%s.%s' % (vendor, alias or - # 'default') ? - data.update({ - 'message': real_sql, - 'category': 'query', - }) - breadcrumbs.record(processor=processor) - def record_many_sql(vendor, alias, start, sql, param_list): duration = time.time() - start for params in param_list: diff --git a/tests/contrib/django/tests.py b/tests/contrib/django/tests.py index 56f863d..086bb4c 100644 --- a/tests/contrib/django/tests.py +++ b/tests/contrib/django/tests.py @@ -29,7 +29,7 @@ except ImportError: from raven.base import Client from raven.utils.compat import StringIO, iteritems, PY2, string_types, text_type -from raven.contrib.django.client import DjangoClient +from raven.contrib.django.client import DjangoClient, record_sql from raven.contrib.django.celery import CeleryClient from raven.contrib.django.handlers import SentryHandler from raven.contrib.django.models import ( @@ -919,3 +919,12 @@ class SentryExceptionHandlerTest(TestCase): self.client.ignore_exceptions.clear() assert not mock_send.called + + +class SQLHookTestCase(TestCase): + def test_wrong_params(self): + query = 'SELECT COUNT(*) FROM mytestmodel WHERE id = %s' + args = ['foobar', 42] + record_sql(None, None, None, None, query, args) + crumbs = get_client().context.breadcrumbs.get_buffer() + self.assertEqual(crumbs[-1]['message'], query) |