-
Notifications
You must be signed in to change notification settings - Fork 643
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
bug_fix(1477): type handling in _add_sql_comment #3113
base: main
Are you sure you want to change the base?
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, could you please add a test so we are sure that the code is fixing the reported issue?
b75a977
to
03561c8
Compare
@@ -22,6 +22,8 @@ def _add_sql_comment(sql, **meta) -> str: | |||
""" | |||
meta.update(**_add_framework_tags()) | |||
comment = _generate_sql_comment(**meta) | |||
# converting to str to handle any type errors | |||
sql = str(sql) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the type of sql
should be str
here, and we should fix the callers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the statement = _add_sql_comment( function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @aryabharat thanks for working on this!
Yes, and there are also 2 other util callers in the SQLAlchemy instrumentor and the Django instrumentor. These also support sqlcommenting, though not consistently with the way the psycopg2 instrumentor does. Fixing all 3 spots (DB-API, SQLAlchemy, Django) would be great with added testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tammy-baylis-swi thanks for the reply.
Just one question, on handling the composable object
For handling SQL inputs:
If the input is a psycopg2.sql.Composable object (not a string),
convert it to a string using the as_string() method.
Example:
if isinstance(sql_input, sql.Composable):
sql_string = sql_input.as_string(connection)
Should i handle like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's generally good if you can follow the psycopg2 docs and it fixes the type errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have update the code, can you please check if this is the fine then will add the test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @aryabharat I must have mixed up PRs in haste. Using that psycopg2
utility will work within Psycopg2Instrumentor, but adding the usage/dependency might not work for the places where the changes are being made (DB-API, SQLAlchemy, Django). Please could you try these instead:
For SQLAlchemy, their documentation says to use str()
, so please switch to that. It's like the change you originally proposed, but in the Sqlalchemy instrumentor instead.
For DB-API and Django it can depend on the db driver being used. (DB-API instrumentor more of a shared utility rather; Django is an ORM). Sometimes it is psycopg2
but user could be using mysqlclient
instead, for example. Using str()
might work just as well, but should be tested for different cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tammy-baylis-swi , thanks for your help and sorry for the delay.
What I have done for SQLAlchemy is to use str() directly.
However, in the case of DB-API and Django with psycopg2,
we cannot use str(), as it returns an unformatted query. So, I handled the psycopg2 case by checking the function as_string().
For mysqlclient
, I handled it by using str() only.
Please let me, if you have any inputs.
41c9ac8
to
cc7332d
Compare
5cb5d9d
to
577f808
Compare
Description
This PR fixes an issue with SQL comment generation, the current implementation fails when processing non-string SQL queries, particularly those from psycopg2's SQL utilities.
Modified _add_sql_comment() function to handles different SQL query types by converting incoming sql queries to strings
Preserves the original query if comment generation fails
Fixes #1477
Type of change
Please delete options that are not relevant.