Skip to content

Commit

Permalink
fix: allow setting connection.read_only to same value (#1247)
Browse files Browse the repository at this point in the history
Setting the read_only value of a connection to the same value as the
current value should be allowed during a transaction, as it does not
change anything. SQLAlchemy regularly does this if engine options have
been specified.

Fixes googleapis/python-spanner-sqlalchemy#493
  • Loading branch information
olavloite authored Dec 4, 2024
1 parent eeb7836 commit 5e8ca94
Show file tree
Hide file tree
Showing 2 changed files with 5 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 @@ -239,7 +239,7 @@ def read_only(self, value):
Args:
value (bool): True for ReadOnly mode, False for ReadWrite.
"""
if self._spanner_transaction_started:
if self._read_only != value and self._spanner_transaction_started:
raise ValueError(
"Connection read/write mode can't be changed while a transaction is in progress. "
"Commit or rollback the current transaction and try again."
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/spanner_dbapi/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def test_read_only_connection(self):
):
connection.read_only = False

# Verify that we can set the value to the same value as it already has.
connection.read_only = True
self.assertTrue(connection.read_only)

connection._spanner_transaction_started = False
connection.read_only = False
self.assertFalse(connection.read_only)
Expand Down

0 comments on commit 5e8ca94

Please sign in to comment.