MagicStack / MagicStack/asyncpg

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

Offen
#914 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
8.1k
Forks
468
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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!

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Es werden keine Repository-Dateien oder Tests genannt. Beginne damit, den Einstiegspunkt Connection.execute und die Verarbeitung von Abfrageparametern durch asyncpg nachzuverfolgen; erledigt wäre eine definierte API, die die execute-Argumente akzeptiert, die Darstellung der parametrisierten Abfrage zurückgibt, ohne sie auszuführen, und Tests enthält, die das angeforderte Verhalten abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
postgresql, python
Bereich
databases
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.