Skip to content

Commit

Permalink
fix: allow setting staleness to same value in tx (#1253)
Browse files Browse the repository at this point in the history
* fix: allow setting staleness to same value in tx

Repeatedly setting the staleness property of a connection in a
transaction to the same value caused an error. This made it
harder to use this property in SQLAlchemy.

Updates googleapis/python-spanner-sqlalchemy#495

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Revert "🦉 Updates from OwlBot post-processor"

This reverts commit 282a982.

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
olavloite and gcf-owl-bot[bot] authored Dec 5, 2024
1 parent 7df93ca commit a214885
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/cloud/spanner_dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def staleness(self, value):
Args:
value (dict): Staleness type and value.
"""
if self._spanner_transaction_started:
if self._spanner_transaction_started and value != self._staleness:
raise ValueError(
"`staleness` option can't be changed while a transaction is in progress. "
"Commit or rollback the current transaction and try again."
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/spanner_dbapi/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,20 @@ def test_staleness_inside_transaction(self):
with self.assertRaises(ValueError):
connection.staleness = {"read_timestamp": datetime.datetime(2021, 9, 21)}

def test_staleness_inside_transaction_same_value(self):
"""
Verify that setting `staleness` to the same value in a transaction is allowed.
"""
connection = self._make_connection()
connection.staleness = {"read_timestamp": datetime.datetime(2021, 9, 21)}
connection._spanner_transaction_started = True
connection._transaction = mock.Mock()

connection.staleness = {"read_timestamp": datetime.datetime(2021, 9, 21)}
self.assertEqual(
connection.staleness, {"read_timestamp": datetime.datetime(2021, 9, 21)}
)

def test_staleness_multi_use(self):
"""
Check that `staleness` option is correctly
Expand Down

0 comments on commit a214885

Please sign in to comment.