]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Use "private" savepoint name to avoid conflicting with user ones
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 18 Nov 2020 15:21:34 +0000 (15:21 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 18 Nov 2020 15:21:34 +0000 (15:21 +0000)
"s1" is too common.

Also omit ``savepoint`` in sql statements: it is a noise word

psycopg3/psycopg3/transaction.py
tests/test_transaction.py
tests/test_transaction_async.py

index ce4b43e138f63ad0ddd0364d011fe270f09ee5a3..63a098f0208b55dedf5215f5b2da844be22a0e48 100644 (file)
@@ -59,7 +59,7 @@ class BaseTransaction(Generic[ConnectionType]):
             # inner transaction: it always has a name
             self._outer_transaction = False
             self._savepoint_name = (
-                savepoint_name or f"s{len(self._conn._savepoints) + 1}"
+                savepoint_name or f"_pg3_{len(self._conn._savepoints) + 1}"
             )
 
     @property
@@ -106,7 +106,7 @@ class BaseTransaction(Generic[ConnectionType]):
         commands = []
         if self._savepoint_name and not self._outer_transaction:
             commands.append(
-                sql.SQL("release savepoint {}")
+                sql.SQL("release {}")
                 .format(sql.Identifier(self._savepoint_name))
                 .as_string(self._conn)
             )
@@ -124,7 +124,7 @@ class BaseTransaction(Generic[ConnectionType]):
         commands = []
         if self._savepoint_name and not self._outer_transaction:
             commands.append(
-                sql.SQL("rollback to savepoint {n}; release savepoint {n}")
+                sql.SQL("rollback to {n}; release {n}")
                 .format(n=sql.Identifier(self._savepoint_name))
                 .as_string(self._conn)
             )
index bc90e9faf1a896dc29e3d51503c38e21d474b9a2..86652b9545217e6908f5adfb46a84734dca1a60e 100644 (file)
@@ -382,9 +382,9 @@ def test_named_savepoints_successful_exit(conn, commands):
     conn.cursor().execute("select 1")
     assert commands.popall() == ["begin"]
     with conn.transaction() as tx:
-        assert commands.popall() == ['savepoint "s1"']
-        assert tx.savepoint_name == "s1"
-    assert commands.popall() == ['release savepoint "s1"']
+        assert commands.popall() == ['savepoint "_pg3_1"']
+        assert tx.savepoint_name == "_pg3_1"
+    assert commands.popall() == ['release "_pg3_1"']
     conn.rollback()
     assert commands.popall() == ["rollback"]
 
@@ -400,16 +400,16 @@ def test_named_savepoints_successful_exit(conn, commands):
         with conn.transaction(savepoint_name="bar") as tx:
             assert commands.popall() == ['savepoint "bar"']
             assert tx.savepoint_name == "bar"
-        assert commands.popall() == ['release savepoint "bar"']
+        assert commands.popall() == ['release "bar"']
     assert commands.popall() == ["commit"]
 
     # Case 3 (with savepoint name auto-generated)
     with conn.transaction():
         assert commands.popall() == ["begin"]
         with conn.transaction() as tx:
-            assert commands.popall() == ['savepoint "s2"']
-            assert tx.savepoint_name == "s2"
-        assert commands.popall() == ['release savepoint "s2"']
+            assert commands.popall() == ['savepoint "_pg3_2"']
+            assert tx.savepoint_name == "_pg3_2"
+        assert commands.popall() == ['release "_pg3_2"']
     assert commands.popall() == ["commit"]
 
 
@@ -443,9 +443,7 @@ def test_named_savepoints_exception_exit(conn, commands):
                 assert commands.popall() == ['savepoint "bar"']
                 assert tx.savepoint_name == "bar"
                 raise ExpectedException
-        assert commands.popall() == [
-            'rollback to savepoint "bar"; release savepoint "bar"'
-        ]
+        assert commands.popall() == ['rollback to "bar"; release "bar"']
     assert commands.popall() == ["commit"]
 
     # Case 3 (with savepoint name auto-generated)
@@ -453,12 +451,10 @@ def test_named_savepoints_exception_exit(conn, commands):
         assert commands.popall() == ["begin"]
         with pytest.raises(ExpectedException):
             with conn.transaction() as tx:
-                assert commands.popall() == ['savepoint "s2"']
-                assert tx.savepoint_name == "s2"
+                assert commands.popall() == ['savepoint "_pg3_2"']
+                assert tx.savepoint_name == "_pg3_2"
                 raise ExpectedException
-        assert commands.popall() == [
-            'rollback to savepoint "s2"; release savepoint "s2"'
-        ]
+        assert commands.popall() == ['rollback to "_pg3_2"; release "_pg3_2"']
     assert commands.popall() == ["commit"]
 
 
index 09764551461e0990fe5b36ab317e02124e1bbef2..8cae48e70f7e4f48e64048bc89f9dec241377e07 100644 (file)
@@ -333,10 +333,10 @@ async def test_named_savepoints_successful_exit(aconn, commands):
     await (await aconn.cursor()).execute("select 1")
     assert commands.popall() == ["begin"]
     async with aconn.transaction() as tx:
-        assert commands.popall() == ['savepoint "s1"']
-        assert tx.savepoint_name == "s1"
+        assert commands.popall() == ['savepoint "_pg3_1"']
+        assert tx.savepoint_name == "_pg3_1"
 
-    assert commands.popall() == ['release savepoint "s1"']
+    assert commands.popall() == ['release "_pg3_1"']
     await aconn.rollback()
     assert commands.popall() == ["rollback"]
 
@@ -352,16 +352,16 @@ async def test_named_savepoints_successful_exit(aconn, commands):
         async with aconn.transaction(savepoint_name="bar") as tx:
             assert commands.popall() == ['savepoint "bar"']
             assert tx.savepoint_name == "bar"
-        assert commands.popall() == ['release savepoint "bar"']
+        assert commands.popall() == ['release "bar"']
     assert commands.popall() == ["commit"]
 
     # Case 3 (with savepoint name auto-generated)
     async with aconn.transaction():
         assert commands.popall() == ["begin"]
         async with aconn.transaction() as tx:
-            assert commands.popall() == ['savepoint "s2"']
-            assert tx.savepoint_name == "s2"
-        assert commands.popall() == ['release savepoint "s2"']
+            assert commands.popall() == ['savepoint "_pg3_2"']
+            assert tx.savepoint_name == "_pg3_2"
+        assert commands.popall() == ['release "_pg3_2"']
     assert commands.popall() == ["commit"]
 
 
@@ -395,9 +395,7 @@ async def test_named_savepoints_exception_exit(aconn, commands):
                 assert commands.popall() == ['savepoint "bar"']
                 assert tx.savepoint_name == "bar"
                 raise ExpectedException
-        assert commands.popall() == [
-            'rollback to savepoint "bar"; release savepoint "bar"'
-        ]
+        assert commands.popall() == ['rollback to "bar"; release "bar"']
     assert commands.popall() == ["commit"]
 
     # Case 3 (with savepoint name auto-generated)
@@ -405,12 +403,10 @@ async def test_named_savepoints_exception_exit(aconn, commands):
         assert commands.popall() == ["begin"]
         with pytest.raises(ExpectedException):
             async with aconn.transaction() as tx:
-                assert commands.popall() == ['savepoint "s2"']
-                assert tx.savepoint_name == "s2"
+                assert commands.popall() == ['savepoint "_pg3_2"']
+                assert tx.savepoint_name == "_pg3_2"
                 raise ExpectedException
-        assert commands.popall() == [
-            'rollback to savepoint "s2"; release savepoint "s2"'
-        ]
+        assert commands.popall() == ['rollback to "_pg3_2"; release "_pg3_2"']
     assert commands.popall() == ["commit"]