From 0a407f053ad7890906cf4f8e734839c6d2e479a4 Mon Sep 17 00:00:00 2001 From: CommanderKeynes Date: Tue, 7 Jan 2025 10:52:36 -0500 Subject: [PATCH] Asyncpg null query fix Adjusted the asyncpg dialect so that an empty SQL string, which is valid for PostgreSQL server, may be successfully processed at the dialect level, such as when using :meth:`.Connection.exec_driver_sql`. Pull request courtesy Andrew Jackson. Closes: #12220 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12220 Pull-request-sha: 45c94febee66b567040b1fbfa3a93079a1314f09 Change-Id: I870df9e31f4a229939e76c702724c25073329282 (cherry picked from commit 7bfb829f25c1bfe2139afe7875882298aaf345ba) --- doc/build/changelog/unreleased_20/12220.rst | 9 +++++++++ lib/sqlalchemy/dialects/postgresql/asyncpg.py | 3 ++- test/dialect/postgresql/test_dialect.py | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_20/12220.rst diff --git a/doc/build/changelog/unreleased_20/12220.rst b/doc/build/changelog/unreleased_20/12220.rst new file mode 100644 index 0000000000..a4b30cca5b --- /dev/null +++ b/doc/build/changelog/unreleased_20/12220.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql + :tickets: 12220 + + Adjusted the asyncpg dialect so that an empty SQL string, which is valid + for PostgreSQL server, may be successfully processed at the dialect level, + such as when using :meth:`.Connection.exec_driver_sql`. Pull request + courtesy Andrew Jackson. + diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index 1761c8de53..523c47abcd 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -546,7 +546,8 @@ class AsyncAdapt_asyncpg_cursor: status = prepared_stmt.get_statusmsg() reg = re.match( - r"(?:SELECT|UPDATE|DELETE|INSERT \d+) (\d+)", status + r"(?:SELECT|UPDATE|DELETE|INSERT \d+) (\d+)", + status or "", ) if reg: self.rowcount = int(reg.group(1)) diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index 892e2abc9b..109101011f 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -1040,6 +1040,12 @@ class MiscBackendTest( __only_on__ = "postgresql" __backend__ = True + @testing.fails_on(["+psycopg2"]) + def test_empty_sql_string(self, connection): + + result = connection.exec_driver_sql("") + assert result._soft_closed + @testing.provide_metadata def test_date_reflection(self): metadata = self.metadata -- 2.47.3