From a714448def03e21f26b0d41902a0304d3877a1d1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 15 Dec 2021 00:57:11 +0100 Subject: [PATCH] Fix prepared statements clearing In the conditions illustrated by the test, i.e. with some statements already prepared and a multiple statements query containing a DROP, the check for multiple statements wouldn't have been triggered, and the query would have eventually been asked for preparation, failing because containing multiple statements. --- psycopg/psycopg/_preparing.py | 1 + tests/test_prepared.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 783a25e4f..14008ee16 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -182,6 +182,7 @@ class PrepareManager: Clear the internal state and prepare a command to clear the state of the server. """ + self._counts.clear() if self._names: self._names.clear() self._maint_commands.clear() diff --git a/tests/test_prepared.py b/tests/test_prepared.py index fec835547..fb3da6e99 100644 --- a/tests/test_prepared.py +++ b/tests/test_prepared.py @@ -92,6 +92,16 @@ def test_no_prepare_multi(conn): assert res == [0] * 10 +def test_no_prepare_multi_with_drop(conn): + conn.execute("select 1", prepare=True) + + for i in range(10): + conn.execute("drop table if exists noprep; create table noprep()") + + cur = conn.execute("select count(*) from pg_prepared_statements") + assert cur.fetchone() == (0,) + + def test_no_prepare_error(conn): conn.autocommit = True for i in range(10): -- 2.47.2