Skip to content

Handle SQL commands executed using as psycopg2 Composed object #1157

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

Conversation

hakib
Copy link
Contributor

@hakib hakib commented Apr 29, 2019

The Composed object is the recommended way of composing SQL statements.
The SQL panel is currently expecting an string sql. Executing a composed
sql command will cause the following exception:

======================================================================
ERROR: test_execute_with_psycopg2_composed_sql (tests.panels.test_sql.SQLPanelTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/haki/src/django-debug-toolbar/tests/panels/test_sql.py", line 228, in test_execute_with_psycopg2_composed_sql
    cursor.execute(command)
  File "/home/haki/src/django-debug-toolbar/debug_toolbar/panels/sql/tracking.py", line 184, in execute
    return self._record(self.cursor.execute, sql, params)
  File "/home/haki/src/django-debug-toolbar/debug_toolbar/panels/sql/tracking.py", line 156, in _record
    "is_select": sql.lower().strip().startswith("select"),
AttributeError: 'Composed' object has no attribute 'lower'

The solution is to cast the sql to string. If it's already string - no harm. If it's a
Composable object, it will return the composed SQL as str.

The Composed object is the recommended way of composing SQL statements.
The SQL panel is currently expecting an string sql. Executing a composed
sql command will cause the following exception:

```
======================================================================
ERROR: test_execute_with_psycopg2_composed_sql (tests.panels.test_sql.SQLPanelTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/haki/src/django-debug-toolbar/tests/panels/test_sql.py", line 228, in test_execute_with_psycopg2_composed_sql
    cursor.execute(command)
  File "/home/haki/src/django-debug-toolbar/debug_toolbar/panels/sql/tracking.py", line 184, in execute
    return self._record(self.cursor.execute, sql, params)
  File "/home/haki/src/django-debug-toolbar/debug_toolbar/panels/sql/tracking.py", line 156, in _record
    "is_select": sql.lower().strip().startswith("select"),
AttributeError: 'Composed' object has no attribute 'lower'
```

The solution is to cast the sql to string. If it's already string - no harm. If it's a
`Composable` object, it will return the composed SQL as str.
@hakib hakib force-pushed the postgresql-psycopg2-composable branch from d868f59 to 98ebfbe Compare April 29, 2019 08:03
@hakib
Copy link
Contributor Author

hakib commented Apr 29, 2019

CI fail seem unrelated. Looks like some trouble connecting to mariadb for testing...

@hakib
Copy link
Contributor Author

hakib commented Apr 29, 2019

Might be a duplicate of #1040

@matthiask matthiask merged commit db24e9b into django-commons:master May 5, 2019
@hakib hakib deleted the postgresql-psycopg2-composable branch May 5, 2019 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants