Skip to content

Commit 5e77073

Browse files
authored
tests: Handle DDL command in cur.sql() differently (#785)
For ease of programming we have a `cur.sql()` method that can be used to execute any sql command. Fetching rows for DDL statements doesn't work though, so we were catching that error and ignoring it. [In psycopg 3.2.8 the exact error message was changed][1]. Instead of detecting the new error message, this change makes sure that we don't trigger this error at all. This is done by detecting whether our result has rows, before calling `fetchall()`. Originally reported @Alphaxxxxx in #784 (comment) [1]: psycopg/psycopg@1eb7e5a
1 parent 987ac62 commit 5e77073

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

test/pycheck/utils.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,12 @@ def __getattr__(self, name):
312312

313313
def sql(self, query, params=None, **kwargs) -> Any:
314314
self.execute(query, params, **kwargs)
315-
try:
316-
return simplify_query_results(self.fetchall())
317-
except psycopg.ProgrammingError as e:
318-
if "the last operation didn't produce a result" == str(e):
319-
# This happens when the query is a DDL statement
320-
return NoResult
321-
raise
315+
if self.pgresult and self.pgresult.status == psycopg.pq.ExecStatus.COMMAND_OK:
316+
# This happens when the query is a DDL statement. Calling fetchall
317+
# would fail with a ProgrammingError in that case.
318+
return NoResult
319+
320+
return simplify_query_results(self.fetchall())
322321

323322
def dsql(self, query, **kwargs):
324323
"""Run a DuckDB query using duckdb.query()"""
@@ -365,13 +364,12 @@ def sql(self, query, params=None, **kwargs):
365364

366365
async def sql_coroutine(self, query, params=None, **kwargs) -> Any:
367366
await self.execute(query, params, **kwargs)
368-
try:
369-
return simplify_query_results(await self.fetchall())
370-
except psycopg.ProgrammingError as e:
371-
if "the last operation didn't produce a result" == str(e):
372-
# This happens when the query is a DDL statement
373-
return NoResult
374-
raise
367+
if self.pgresult and self.pgresult.status == psycopg.pq.ExecStatus.COMMAND_OK:
368+
# This happens when the query is a DDL statement. Calling fetchall
369+
# would fail with a ProgrammingError in that case.
370+
return NoResult
371+
372+
return simplify_query_results(await self.fetchall())
375373

376374
def dsql(self, query, **kwargs):
377375
"""Run a DuckDB query using duckdb.query()"""

0 commit comments

Comments
 (0)