From 5e8ca949b583fbcf0b92b42696545973aad8c78f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 4 Dec 2024 13:57:15 +0100 Subject: [PATCH] fix: allow setting connection.read_only to same value (#1247) 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 65afcd4a2a..416bb2a959 100644 --- a/google/cloud/spanner_dbapi/connection.py +++ b/google/cloud/spanner_dbapi/connection.py @@ -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." diff --git a/tests/unit/spanner_dbapi/test_connection.py b/tests/unit/spanner_dbapi/test_connection.py index 62867bbd2e..a07e94735f 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)