Skip to content

Commit

Permalink
fix(django): Fix psycopg3
Browse files Browse the repository at this point in the history
Fixes GH-3061
  • Loading branch information
szokeasaurusrex committed May 29, 2024
1 parent b496a71 commit 7f1d61e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 3 additions & 8 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,10 @@ def _set_db_data(span, cursor_or_db):
if is_psycopg2:
connection_params = cursor_or_db.connection.get_dsn_parameters()
else:
is_psycopg3 = (
hasattr(cursor_or_db, "connection")
and hasattr(cursor_or_db.connection, "info")
and hasattr(cursor_or_db.connection.info, "get_parameters")
and inspect.isroutine(cursor_or_db.connection.info.get_parameters)
)
if is_psycopg3:
try:
# psycopg3
connection_params = cursor_or_db.connection.info.get_parameters()
else:
except Exception:
connection_params = db.get_connection_params()

db_name = connection_params.get("dbname") or connection_params.get("database")
Expand Down
12 changes: 12 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,18 @@ def get_connection_params(self):
pytest.fail("A TypeError was raised")


def test_connection_reconnect_does_not_error(sentry_init):
sentry_init(integrations=[DjangoIntegration()])

from django.db import connections

if "postgres" not in connections:
pytest.skip("postgres tests disabled")

connections["postgres"].close()
connections["postgres"].connect()


@pytest.mark.parametrize(
"transaction_style,client_url,expected_transaction,expected_source,expected_response",
[
Expand Down

0 comments on commit 7f1d61e

Please sign in to comment.