MagicStack / MagicStack/asyncpg

Feature request: A way to inspect the query string run by asyncpg

Open
#914 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

I have hasked this before SO (https://stackoverflow.com/questions/69814471/is-there-a-way-to-inspect-the-query-string-run-by-asyncpg) but I am getting no answer, so this is a feature request.

-------------------------

When I use [asyncpg][1] to run a query on my database (and I have to run several queries),

I always split up things like this:

- the string of the query, with parameters placeholders:
```
QUERY_1 = """
UPDATE TABLE my_table
SET field_1 = $1
WHERE field_id = $2;
"""
```
- a dictionary with the query parameters:
```
query_params_dict = {
"field_1" : "value_1",
"field_id" : "10"
}
```
- and the function that will get the query string, replace the placeholders with the parameters values, run the query with [connection.execute][2], and manage the errors:
````
async def run_query_1(query_params_dict):
connection = await Utils.get_connection_to_db(**conf.asyncpg_db_conn) # custom function to open the connection to my db
query = QUERY_1
try:
await connection.execute(
query, query_params_dict["field_1"], int(query_params_dict["field_id"])
)
return None
except (Exception, asyncpg.UniqueViolationError) as integrError:
logger.error("Violated unique constraint: {}".format(integrError), exc_info=True)
return "{}".format(integrError)
except (Exception, asyncpg.ConnectionFailureError) as error:
logger.error("Failed connection: {}".format(error), exc_info=True)
return "{}".format(error)
finally:
if (connection):
await Utils.close_connection(connection)
````
But the queries I have to run often require many mores parameters, so that I always make some mistake in assigning values to the variables,

so that the query fails/returns unwanted results,

and the only way I know to determine what is wrong with it, is to check carefully the value assigned to every parameter of `query_params_dict`.

This takes a lot of time.

Instead, I could understand quickly what is wrong by checking the query run by asyncpg.

So, **is there a way to inspect the query string run by asyncpg?**

I would expect a command of asyncpg taking the same arguments of `connection.execute` as input, and returning as output the string of the query filled up with parameters, without running any query on the database.

Let's suppose this command exists and is `connection.expected_query`, in my case it would work like this:

connection.expected_query(
query, query_params_dict["field_1"], int(query_params_dict["field_id"])
>>>

"""
UPDATE TABLE my_table
SET field_1 = 'value_1'
WHERE field_id = 10;
"""

Could you please implement a feature like this?
Thanks in advance!

[1]: https://magicstack.github.io/asyncpg/current/index.html
[2]: https://magicstack.github.io/asyncpg/current/api/index.html?highlight=connection%20execute#asyncpg.connection.Connection.execute

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named. Start by tracing the Connection.execute entry point and how asyncpg handles query parameters; done would be a defined API that accepts the execute arguments, returns the parameterized query representation without executing it, and has tests covering the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.