Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

Commit 689bf87

Browse files
authored
Revert "feat: enable instance-level connection (#931)"
This reverts commit d6963e2.
1 parent d6963e2 commit 689bf87

4 files changed

Lines changed: 9 additions & 102 deletions

File tree

google/cloud/spanner_dbapi/connection.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Connection:
8383
should end a that a new one should be started when the next statement is executed.
8484
"""
8585

86-
def __init__(self, instance, database=None, read_only=False):
86+
def __init__(self, instance, database, read_only=False):
8787
self._instance = instance
8888
self._database = database
8989
self._ddl_statements = []
@@ -242,8 +242,6 @@ def _session_checkout(self):
242242
:rtype: :class:`google.cloud.spanner_v1.session.Session`
243243
:returns: Cloud Spanner session object ready to use.
244244
"""
245-
if self.database is None:
246-
raise ValueError("Database needs to be passed for this operation")
247245
if not self._session:
248246
self._session = self.database._pool.get()
249247

@@ -254,8 +252,6 @@ def _release_session(self):
254252
255253
The session will be returned into the sessions pool.
256254
"""
257-
if self.database is None:
258-
raise ValueError("Database needs to be passed for this operation")
259255
self.database._pool.put(self._session)
260256
self._session = None
261257

@@ -372,7 +368,7 @@ def close(self):
372368
if self.inside_transaction:
373369
self._transaction.rollback()
374370

375-
if self._own_pool and self.database:
371+
if self._own_pool:
376372
self.database._pool.clear()
377373

378374
self.is_closed = True
@@ -382,8 +378,6 @@ def commit(self):
382378
383379
This method is non-operational in autocommit mode.
384380
"""
385-
if self.database is None:
386-
raise ValueError("Database needs to be passed for this operation")
387381
self._snapshot = None
388382

389383
if self._autocommit:
@@ -426,8 +420,6 @@ def cursor(self):
426420

427421
@check_not_closed
428422
def run_prior_DDL_statements(self):
429-
if self.database is None:
430-
raise ValueError("Database needs to be passed for this operation")
431423
if self._ddl_statements:
432424
ddl_statements = self._ddl_statements
433425
self._ddl_statements = []
@@ -482,8 +474,6 @@ def validate(self):
482474
:raises: :class:`google.cloud.exceptions.NotFound`: if the linked instance
483475
or database doesn't exist.
484476
"""
485-
if self.database is None:
486-
raise ValueError("Database needs to be passed for this operation")
487477
with self.database.snapshot() as snapshot:
488478
result = list(snapshot.execute_sql("SELECT 1"))
489479
if result != [[1]]:
@@ -502,7 +492,7 @@ def __exit__(self, etype, value, traceback):
502492

503493
def connect(
504494
instance_id,
505-
database_id=None,
495+
database_id,
506496
project=None,
507497
credentials=None,
508498
pool=None,
@@ -515,7 +505,7 @@ def connect(
515505
:param instance_id: The ID of the instance to connect to.
516506
517507
:type database_id: str
518-
:param database_id: (Optional) The ID of the database to connect to.
508+
:param database_id: The ID of the database to connect to.
519509
520510
:type project: str
521511
:param project: (Optional) The ID of the project which owns the
@@ -567,9 +557,7 @@ def connect(
567557
raise ValueError("project in url does not match client object project")
568558

569559
instance = client.instance(instance_id)
570-
conn = Connection(
571-
instance, instance.database(database_id, pool=pool) if database_id else None
572-
)
560+
conn = Connection(instance, instance.database(database_id, pool=pool))
573561
if pool is not None:
574562
conn._own_pool = False
575563

google/cloud/spanner_dbapi/cursor.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ def execute(self, sql, args=None):
228228
:type args: list
229229
:param args: Additional parameters to supplement the SQL query.
230230
"""
231-
if self.connection.database is None:
232-
raise ValueError("Database needs to be passed for this operation")
233231
self._itr = None
234232
self._result_set = None
235233
self._row_count = _UNSET_COUNT
@@ -303,8 +301,6 @@ def executemany(self, operation, seq_of_params):
303301
:param seq_of_params: Sequence of additional parameters to run
304302
the query with.
305303
"""
306-
if self.connection.database is None:
307-
raise ValueError("Database needs to be passed for this operation")
308304
self._itr = None
309305
self._result_set = None
310306
self._row_count = _UNSET_COUNT
@@ -448,8 +444,6 @@ def _handle_DQL_with_snapshot(self, snapshot, sql, params):
448444
self._row_count = _UNSET_COUNT
449445

450446
def _handle_DQL(self, sql, params):
451-
if self.connection.database is None:
452-
raise ValueError("Database needs to be passed for this operation")
453447
sql, params = parse_utils.sql_pyformat_args_to_spanner(sql, params)
454448
if self.connection.read_only and not self.connection.autocommit:
455449
# initiate or use the existing multi-use snapshot
@@ -490,8 +484,6 @@ def list_tables(self):
490484
def run_sql_in_snapshot(self, sql, params=None, param_types=None):
491485
# Some SQL e.g. for INFORMATION_SCHEMA cannot be run in read-write transactions
492486
# hence this method exists to circumvent that limit.
493-
if self.connection.database is None:
494-
raise ValueError("Database needs to be passed for this operation")
495487
self.connection.run_prior_DDL_statements()
496488

497489
with self.connection.database.snapshot() as snapshot:

tests/unit/spanner_dbapi/test_connection.py

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,6 @@ def test__session_checkout(self, mock_database):
169169
connection._session_checkout()
170170
self.assertEqual(connection._session, "db_session")
171171

172-
def test__session_checkout_database_error(self):
173-
from google.cloud.spanner_dbapi import Connection
174-
175-
connection = Connection(INSTANCE)
176-
177-
with pytest.raises(ValueError):
178-
connection._session_checkout()
179-
180172
@mock.patch("google.cloud.spanner_v1.database.Database")
181173
def test__release_session(self, mock_database):
182174
from google.cloud.spanner_dbapi import Connection
@@ -190,13 +182,6 @@ def test__release_session(self, mock_database):
190182
pool.put.assert_called_once_with("session")
191183
self.assertIsNone(connection._session)
192184

193-
def test__release_session_database_error(self):
194-
from google.cloud.spanner_dbapi import Connection
195-
196-
connection = Connection(INSTANCE)
197-
with pytest.raises(ValueError):
198-
connection._release_session()
199-
200185
def test_transaction_checkout(self):
201186
from google.cloud.spanner_dbapi import Connection
202187

@@ -309,14 +294,6 @@ def test_commit(self, mock_warn):
309294
AUTOCOMMIT_MODE_WARNING, UserWarning, stacklevel=2
310295
)
311296

312-
def test_commit_database_error(self):
313-
from google.cloud.spanner_dbapi import Connection
314-
315-
connection = Connection(INSTANCE)
316-
317-
with pytest.raises(ValueError):
318-
connection.commit()
319-
320297
@mock.patch.object(warnings, "warn")
321298
def test_rollback(self, mock_warn):
322299
from google.cloud.spanner_dbapi import Connection
@@ -370,13 +347,6 @@ def test_run_prior_DDL_statements(self, mock_database):
370347
with self.assertRaises(InterfaceError):
371348
connection.run_prior_DDL_statements()
372349

373-
def test_run_prior_DDL_statements_database_error(self):
374-
from google.cloud.spanner_dbapi import Connection
375-
376-
connection = Connection(INSTANCE)
377-
with pytest.raises(ValueError):
378-
connection.run_prior_DDL_statements()
379-
380350
def test_as_context_manager(self):
381351
connection = self._make_connection()
382352
with connection as conn:
@@ -796,14 +766,6 @@ def test_validate_error(self):
796766

797767
snapshot_obj.execute_sql.assert_called_once_with("SELECT 1")
798768

799-
def test_validate_database_error(self):
800-
from google.cloud.spanner_dbapi import Connection
801-
802-
connection = Connection(INSTANCE)
803-
804-
with pytest.raises(ValueError):
805-
connection.validate()
806-
807769
def test_validate_closed(self):
808770
from google.cloud.spanner_dbapi.exceptions import InterfaceError
809771

@@ -954,14 +916,16 @@ def test_request_priority(self):
954916
sql, params, param_types=param_types, request_options=None
955917
)
956918

957-
def test_custom_client_connection(self):
919+
@mock.patch("google.cloud.spanner_v1.Client")
920+
def test_custom_client_connection(self, mock_client):
958921
from google.cloud.spanner_dbapi import connect
959922

960923
client = _Client()
961924
connection = connect("test-instance", "test-database", client=client)
962925
self.assertTrue(connection.instance._client == client)
963926

964-
def test_invalid_custom_client_connection(self):
927+
@mock.patch("google.cloud.spanner_v1.Client")
928+
def test_invalid_custom_client_connection(self, mock_client):
965929
from google.cloud.spanner_dbapi import connect
966930

967931
client = _Client()
@@ -973,12 +937,6 @@ def test_invalid_custom_client_connection(self):
973937
client=client,
974938
)
975939

976-
def test_connection_wo_database(self):
977-
from google.cloud.spanner_dbapi import connect
978-
979-
connection = connect("test-instance")
980-
self.assertTrue(connection.database is None)
981-
982940

983941
def exit_ctx_func(self, exc_type, exc_value, traceback):
984942
"""Context __exit__ method mock."""

tests/unit/spanner_dbapi/test_cursor.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,6 @@ def test_execute_attribute_error(self):
163163
with self.assertRaises(AttributeError):
164164
cursor.execute(sql="SELECT 1")
165165

166-
def test_execute_database_error(self):
167-
connection = self._make_connection(self.INSTANCE)
168-
cursor = self._make_one(connection)
169-
170-
with self.assertRaises(ValueError):
171-
cursor.execute(sql="SELECT 1")
172-
173166
def test_execute_autocommit_off(self):
174167
from google.cloud.spanner_dbapi.utils import PeekIterator
175168

@@ -614,16 +607,6 @@ def test_executemany_insert_batch_aborted(self):
614607
)
615608
self.assertIsInstance(connection._statements[0][1], ResultsChecksum)
616609

617-
@mock.patch("google.cloud.spanner_v1.Client")
618-
def test_executemany_database_error(self, mock_client):
619-
from google.cloud.spanner_dbapi import connect
620-
621-
connection = connect("test-instance")
622-
cursor = connection.cursor()
623-
624-
with self.assertRaises(ValueError):
625-
cursor.executemany("""SELECT * FROM table1 WHERE "col1" = @a1""", ())
626-
627610
@unittest.skipIf(
628611
sys.version_info[0] < 3, "Python 2 has an outdated iterator definition"
629612
)
@@ -771,13 +754,6 @@ def test_handle_dql_priority(self):
771754
sql, None, None, request_options=RequestOptions(priority=1)
772755
)
773756

774-
def test_handle_dql_database_error(self):
775-
connection = self._make_connection(self.INSTANCE)
776-
cursor = self._make_one(connection)
777-
778-
with self.assertRaises(ValueError):
779-
cursor._handle_DQL("sql", params=None)
780-
781757
def test_context(self):
782758
connection = self._make_connection(self.INSTANCE, self.DATABASE)
783759
cursor = self._make_one(connection)
@@ -838,13 +814,6 @@ def test_run_sql_in_snapshot(self):
838814
mock_snapshot.execute_sql.return_value = results
839815
self.assertEqual(cursor.run_sql_in_snapshot("sql"), list(results))
840816

841-
def test_run_sql_in_snapshot_database_error(self):
842-
connection = self._make_connection(self.INSTANCE)
843-
cursor = self._make_one(connection)
844-
845-
with self.assertRaises(ValueError):
846-
cursor.run_sql_in_snapshot("sql")
847-
848817
def test_get_table_column_schema(self):
849818
from google.cloud.spanner_dbapi.cursor import ColumnDetails
850819
from google.cloud.spanner_dbapi import _helpers

0 commit comments

Comments
 (0)