8000 Handle psycopg2's string composition by bmdavi3 · Pull Request #170 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

Handle psycopg2's string composition #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
from django import VERSION as DJANGO_VERSION
from django.core import signals

try:
import psycopg2.sql

def sql_to_string(sql):
if isinstance(sql, psycopg2.sql.SQL):
return sql.string
return sql


except ImportError:

def sql_to_string(sql):
return sql

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the full try/except/else blocks so there's less in the try:

try:
    import psycopg2.sql
except ImportError:
    def sql_to_string(sql):
        return sql
else:
    # Handle psycopg2 sql object strings (or some such comment)
    def sql_to_string(sql):
        if isinstance(sql, psycopg2.sql.SQL):
            return sql.string
        return sql

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine, just a function declaration whose errors are caught



try:
from django.urls import resolve
except ImportError:
Expand Down Expand Up @@ -203,6 +218,7 @@ def format_sql(sql, params):
# convert sql with named parameters to sql with unnamed parameters
conv = _FormatConverter(params)
if params:
sql = sql_to_string(sql)
sql = sql % conv
params = conv.params
else:
Expand Down
46 changes: 46 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ def test_sql_queries(sentry_init, capture_events):
assert crumb["message"] == """SELECT count(*) FROM people_person WHERE foo = 123"""


@pytest.mark.django_db
def test_sql_dict_query_params(sentry_init, capture_events):
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
from django.db import connection

sql = connection.cursor()

events = capture_events()
with pytest.raises(Exception):
# table doesn't even exist
sql.execute(
"""SELECT count(*) FROM people_person WHERE foo = %(my_foo)s""",
{"my_foo": 10},
)

capture_message("HI")

event, = events

crumb, = event["breadcrumbs"]
assert crumb["message"] == ("SELECT count(*) FROM people_person WHERE foo = 10")


@pytest.mark.django_db
def test_sql_psycopg2_string_composition(sentry_init, capture_events):
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
from django.db import connection
from psycopg2 import sql as psycopg2_sql

sql = connection.cursor()

events = capture_events()
with pytest.raises(Exception):
# table doesn't even exist
sql.execute(
psycopg2_sql.SQL("SELECT %(my_param)s FROM people_person"), {"my_param": 10}
)

capture_message("HI")

event, = events

crumb, = event["breadcrumbs"]
assert crumb["message"] == ("SELECT 10 FROM people_person")


@pytest.mark.django_db
def test_sql_queries_large_params(sentry_init, capture_events):
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ envlist =
deps =
-r test-requirements.txt

django: psycopg2>=2.7.5

django-{1.6,1.7,1.8}: pytest-django<3.0
django-{1.9,1.10,1.11,2.0,2.1,dev}: pytest-django>=3.0
django-1.6: Django>=1.6,<1.7
Expand Down
0