From: Daniele Varrazzo Date: Wed, 18 Nov 2020 15:21:34 +0000 (+0000) Subject: Use "private" savepoint name to avoid conflicting with user ones X-Git-Tag: 3.0.dev0~351^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aa8c93c960a0cfb8cbb8a1886285475b0011d067;p=thirdparty%2Fpsycopg.git Use "private" savepoint name to avoid conflicting with user ones "s1" is too common. Also omit ``savepoint`` in sql statements: it is a noise word --- diff --git a/psycopg3/psycopg3/transaction.py b/psycopg3/psycopg3/transaction.py index ce4b43e13..63a098f02 100644 --- a/psycopg3/psycopg3/transaction.py +++ b/psycopg3/psycopg3/transaction.py @@ -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) ) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index bc90e9faf..86652b954 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -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"] diff --git a/tests/test_transaction_async.py b/tests/test_transaction_async.py index 097645514..8cae48e70 100644 --- a/tests/test_transaction_async.py +++ b/tests/test_transaction_async.py @@ -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"]