8000 Handle psycopg2's string composition (#170) · etherscan-io/sentry-python@b8699bf · GitHub
[go: up one dir, main page]

Skip to content

Commit b8699bf

Browse files
bmdavi3untitaker
authored andcommitted
Handle psycopg2's string composition (getsentry#170)
* Handle psycopg2's string composition * Remove extraneous import * Add tests, run black
1 parent 3ab6d78 commit b8699bf

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
from django import VERSION as DJANGO_VERSION
88
from django.core import signals
99

10+
try:
11+
import psycopg2.sql
12+
13+
def sql_to_string(sql):
14+
if isinstance(sql, psycopg2.sql.SQL):
15+
return sql.string
16+
return sql
17+
18+
19+
except ImportError:
20+
21+
def sql_to_string(sql):
22+
return sql
23+
24+
1025
try:
1126
from django.urls import resolve
1227
except ImportError:
@@ -203,6 +218,7 @@ def format_sql(sql, params):
203218
# convert sql with named parameters to sql with unnamed parameters
204219
conv = _FormatConverter(params)
205220
if params:
221+
sql = sql_to_string(sql)
206222
sql = sql % conv
207223
params = conv.params
208224
else:

tests/integrations/django/test_basic.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,52 @@ def test_sql_queries(sentry_init, capture_events):
141141
assert crumb["message"] == """SELECT count(*) FROM people_person WHERE foo = 123"""
142142

143143

144+
@pytest.mark.django_db
145+
def test_sql_dict_query_params(sentry_init, capture_events):
146+
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
147+
from django.db import connection
148+
149+
sql = connection.cursor()
150+
151+
events = capture_events()
152+
with pytest.raises(Exception):
153+
# table doesn't even exist
154+
sql.execute(
155+
"""SELECT count(*) FROM people_person WHERE foo = %(my_foo)s""",
156+
{"my_foo": 10},
157+
)
158+
159+
capture_message("HI")
160+
161+
event, = events
162+
163+
crumb, = event["breadcrumbs"]
164+
assert crumb["message"] == ("SELECT count(*) FROM people_person WHERE foo = 10")
165+
166+
167+
@pytest.mark.django_db
168+
def test_sql_psycopg2_string_composition(sentry_init, capture_events):
169+
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
170+
from django.db import connection
171+
from psycopg2 import sql as psycopg2_sql
172+
173+
sql = connection.cursor()
174+
175+
events = capture_events()
176+
with pytest.raises(Exception):
177+
# table doesn't even exist
178+
sql.execute(
179+
psycopg2_sql.SQL("SELECT %(my_param)s FROM people_person"), {"my_param": 10}
180+
)
181+
182+
capture_message("HI")
183+
184+
event, = events
185+
186+
crumb, = event["breadcrumbs"]
187+
assert crumb["message"] == ("SELECT 10 FROM people_person")
188+
189+
144190
@pytest.mark.django_db
145191
def test_sql_queries_large_params(sentry_init, capture_events):
146192
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ envlist =
4242
deps =
4343
-r test-requirements.txt
4444

45+
django: psycopg2>=2.7.5
46+
4547
django-{1.6,1.7,1.8}: pytest-django<3.0
4648
django-{1.9,1.10,1.11,2.0,2.1,dev}: pytest-django>=3.0
4749
django-1.6: Django>=1.6,<1.7

0 commit comments

Comments
 (0)
0