Skip to content
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

Patched cursor does not expect psycopg2 sql Composed Object #246

Open
git-yogeshkumar opened this issue Oct 10, 2023 · 4 comments
Open

Patched cursor does not expect psycopg2 sql Composed Object #246

git-yogeshkumar opened this issue Oct 10, 2023 · 4 comments
Labels

Comments

@git-yogeshkumar
Copy link

What happened?

I am integrating cachalot with my project and everything works fine up until the point we use django's ORM to execute queries, but in some parts of our project we use Raw SQLs and that's where this issue is happening. We are using psycopg2 native composition utility called sql. Here the import for reference:

from psycopg2 import sql

Now this frames a Composed Object which when we try to execute using cursor like this:

with connection.cursor() as cursor:
    cursor.execute(sql_query, query_args)

explodes in the cursors monkey_patch code which does not expect a compose object.

What should've happened instead?

I expect that cachalot should just allow these queries to run naturally and avoid caching them or at least make it a configurable setting.

Steps to reproduce

Configure cachalot in a django project and try to run any query using Raw sql and psycopg2 sql package for example:

from psycopg2 import sql
from django.db import connection

query = sql.SQL("select {field} from {table} where {pkey} = %s").format(
    field=sql.Identifier('my_name'),
    table=sql.Identifier('some_table'),
    pkey=sql.Identifier('id'))

with connection.cursor() as cursor:
    cur.execute(query, (42,))

django~=3.2 database=postgres 13.2
Screenshot 2023-10-10 at 4 33 29 PM

@Andrew-Chen-Wang
Copy link
Collaborator

We don't support that unfortunately, but feel free to add a small if statement for your case. In the meantime, you can use a utility function to temporarily disable cachalot.

@git-yogeshkumar
Copy link
Author

Sure @Andrew-Chen-Wang , once I get some spare time will raise a PR for the above, I'll just raise a warning when encountering sql other than string, and let it execute without bothering caching. Does it sound good?

@Andrew-Chen-Wang
Copy link
Collaborator

Correct. Unsupported measures raise an exception that exits the monkeypatch.

@scorpp
Copy link

scorpp commented Jan 29, 2024

We're evaluating using the lib in our project and faced similar issue. We utilise psycopg2.sql API to make using raw SQL safer.

I can confirm that adding CACHALOT_INVALIDATE_RAW = False allows to workaround the issue.

In the meantime, you can use a utility function to temporarily disable cachalot.
@Andrew-Chen-Wang if you were referring to cachalot.api.cachalot_disabled then it doesn't help, probably because cachalot.monkey_patch._patch_cursor only checks for CACHALOT_INVALIDATE_RAW and not the LOCAL_STORAGE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants