]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix prepared statements clearing
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Dec 2021 23:57:11 +0000 (00:57 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 15 Dec 2021 00:26:51 +0000 (01:26 +0100)
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
tests/test_prepared.py

index 783a25e4fdb9bdce7abe130ba152124ca9fb0794..14008ee16d25744f0b064e38d9f9971991a854ce 100644 (file)
@@ -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()
index fec835547efce664e59ab0fb56b4a33bad5f5db6..fb3da6e99d6fa3966ed598649781d83f1361137e 100644 (file)
@@ -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):