From ca7caf64d6866254b6238d42710336902d5a9b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Tue, 3 Dec 2024 10:16:38 +0100 Subject: [PATCH] fix: allow setting connection.read_only to same value 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 https://github.com/googleapis/python-spanner-sqlalchemy/issues/493 --- google/cloud/spanner_dbapi/connection.py | 2 +- tests/unit/spanner_dbapi/test_connection.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/google/cloud/spanner_dbapi/connection.py b/google/cloud/spanner_dbapi/connection.py index b02d62ea27..e8676c4291 100644 --- a/google/cloud/spanner_dbapi/connection.py +++ b/google/cloud/spanner_dbapi/connection.py @@ -232,7 +232,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." diff --git a/tests/unit/spanner_dbapi/test_connection.py b/tests/unit/spanner_dbapi/test_connection.py index d0fa521f8f..16ab6984a4 100644 --- a/tests/unit/spanner_dbapi/test_connection.py +++ b/tests/unit/spanner_dbapi/test_connection.py @@ -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)