MagicStack / MagicStack/asyncpg

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

オープン
#914 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
8.1k
フォーク
468
PR マージ指標
30日以内にマージされた PR はありません

説明

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 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, 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. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リポジトリのファイルやテストは指定されていません。まず、エントリポイントである Connection.execute と、asyncpg がクエリパラメータをどのように処理するかを追跡します。完了条件は、execute の引数を受け取り、実行せずにパラメータ化されたクエリの表現を返し、要求された動作を網羅するテストを備えた API が定義されていることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
postgresql, python
領域
databases
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。