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

Commit 16388c1

Browse files
author
Ilya Gurov
authored
feat: drop read_only on a connection returned back to a pool (#189)
* feat: drop read_only on a connection returned back to a pool
1 parent 2932a02 commit 16388c1

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def reset_connection(dbapi_conn, connection_record):
5151
if getattr(dbapi_conn.connection, "staleness", None) is not None:
5252
dbapi_conn.connection.staleness = None
5353

54+
if getattr(dbapi_conn.connection, "read_only", None) is not None:
55+
dbapi_conn.connection.read_only = False
56+
5457

5558
# register a method to get a single value of a JSON object
5659
OPERATORS[json_getitem_op] = operator_lookup["json_getitem_op"]

test/test_suite_13.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pkg_resources
2222
import pytest
2323
import random
24-
import unittest
24+
import time
2525
from unittest import mock
2626

2727
import sqlalchemy
@@ -1579,45 +1579,32 @@ def test_user_agent(self):
15791579
)
15801580

15811581

1582-
class ExecutionOptionsTest(fixtures.TestBase, unittest.TestCase):
1582+
class ExecutionOptionsReadOnlyTest(fixtures.TestBase):
15831583
"""
15841584
Check that `execution_options()` method correctly
15851585
sets parameters on the underlying DB API connection.
15861586
"""
15871587

1588-
@classmethod
1589-
def setUpClass(cls):
1590-
cls._engine = create_engine(get_db_url(), pool_size=1)
1591-
cls._metadata = MetaData(bind=cls._engine)
1588+
def setUp(self):
1589+
self._engine = create_engine(get_db_url(), pool_size=1)
1590+
metadata = MetaData(bind=self._engine)
15921591

1593-
cls._table = Table(
1592+
self._table = Table(
15941593
"execution_options",
1595-
cls._metadata,
1594+
metadata,
15961595
Column("opt_id", Integer, primary_key=True),
15971596
Column("opt_name", String(16), nullable=False),
15981597
)
15991598

1600-
cls._metadata.create_all(cls._engine)
1599+
metadata.create_all(self._engine)
16011600

16021601
def test_read_only(self):
16031602
with self._engine.connect().execution_options(read_only=True) as connection:
16041603
connection.execute(select(["*"], from_obj=self._table)).fetchall()
16051604
assert connection.connection.read_only is True
16061605

1607-
def test_staleness(self):
1608-
with self._engine.connect().execution_options(
1609-
read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=5)}
1610-
) as connection:
1611-
connection.execute(select(["*"], from_obj=self._table)).fetchall()
1612-
assert connection.connection.staleness == {
1613-
"exact_staleness": datetime.timedelta(seconds=5)
1614-
}
1615-
1616-
with self._engine.connect() as connection:
1617-
assert connection.connection.staleness is None
1618-
16191606
with self._engine.connect() as connection:
1620-
del connection.staleness
1607+
assert connection.connection.read_only is False
16211608

16221609

16231610
class LimitOffsetTest(fixtures.TestBase):
@@ -1645,6 +1632,43 @@ def test_offset_only(self):
16451632
list(connection.execute(self._table.select().offset(offset)).fetchall())
16461633

16471634

1635+
class ExecutionOptionsStalenessTest(fixtures.TestBase):
1636+
"""
1637+
Check that `execution_options()` method correctly
1638+
sets parameters on the underlying DB API connection.
1639+
"""
1640+
1641+
def setUp(self):
1642+
self._engine = create_engine(get_db_url(), pool_size=1)
1643+
metadata = MetaData(bind=self._engine)
1644+
1645+
self._table = Table(
1646+
"execution_options",
1647+
metadata,
1648+
Column("opt_id", Integer, primary_key=True),
1649+
Column("opt_name", String(16), nullable=False),
1650+
)
1651+
1652+
metadata.create_all(self._engine)
1653+
time.sleep(1)
1654+
1655+
def test_staleness(self):
1656+
with self._engine.connect().execution_options(
1657+
read_only=True, staleness={"exact_staleness": datetime.timedelta(seconds=1)}
1658+
) as connection:
1659+
connection.execute(select(["*"], from_obj=self._table)).fetchall()
1660+
assert connection.connection.staleness == {
1661+
"exact_staleness": datetime.timedelta(seconds=1)
1662+
}
1663+
1664+
with self._engine.connect() as connection:
1665+
assert connection.connection.staleness == {}
1666+
1667+
engine = create_engine("sqlite:///database")
1668+
with engine.connect() as connection:
1669+
pass
1670+
1671+
16481672
class TemporaryTableTest(fixtures.TestBase):
16491673
"""
16501674
Check that temporary tables raise an error on creation.

0 commit comments

Comments
 (0)