Skip to content

Commit 8abaebd

Browse files
authored
feat: add support for custom timeout and retry parameters in execute_update method in transactions (#251)
* feat: Added support for custom timeout and retry parameters in transactions. * feat: Added support for custom timeout and retry parameters in transactions * docs: added documentation for execute_update method in Transactions file * feat: changed retry and timeout params to keyword only arguments * feat: undo deleted line after license text * feat: changed default timeout value from None to gapic_v1.method.DEFAULT
1 parent 489ac0a commit 8abaebd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

google/cloud/spanner_v1/transaction.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.cloud.spanner_v1.snapshot import _SnapshotBase
3030
from google.cloud.spanner_v1.batch import _BatchBase
3131
from google.cloud.spanner_v1._opentelemetry_tracing import trace_call
32+
from google.api_core import gapic_v1
3233

3334

3435
class Transaction(_SnapshotBase, _BatchBase):
@@ -185,7 +186,15 @@ def _make_params_pb(params, param_types):
185186
return {}
186187

187188
def execute_update(
188-
self, dml, params=None, param_types=None, query_mode=None, query_options=None
189+
self,
190+
dml,
191+
params=None,
192+
param_types=None,
193+
query_mode=None,
194+
query_options=None,
195+
*,
196+
retry=gapic_v1.method.DEFAULT,
197+
timeout=gapic_v1.method.DEFAULT,
189198
):
190199
"""Perform an ``ExecuteSql`` API request with DML.
191200
@@ -212,6 +221,12 @@ def execute_update(
212221
or :class:`dict`
213222
:param query_options: (Optional) Options that are provided for query plan stability.
214223
224+
:type retry: :class:`~google.api_core.retry.Retry`
225+
:param retry: (Optional) The retry settings for this request.
226+
227+
:type timeout: float
228+
:param timeout: (Optional) The timeout for this request.
229+
215230
:rtype: int
216231
:returns: Count of rows affected by the DML statement.
217232
"""
@@ -245,7 +260,9 @@ def execute_update(
245260
with trace_call(
246261
"CloudSpanner.ReadWriteTransaction", self._session, trace_attributes
247262
):
248-
response = api.execute_sql(request=request, metadata=metadata)
263+
response = api.execute_sql(
264+
request=request, metadata=metadata, retry=retry, timeout=timeout
265+
)
249266
return response.stats.row_count_exact
250267

251268
def batch_update(self, statements):

tests/unit/test_transaction.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from tests._helpers import OpenTelemetryBase, StatusCanonicalCode
1818
from google.cloud.spanner_v1 import Type
1919
from google.cloud.spanner_v1 import TypeCode
20+
from google.api_core import gapic_v1
2021

2122
TABLE_NAME = "citizens"
2223
COLUMNS = ["email", "first_name", "last_name", "age"]
@@ -410,7 +411,13 @@ def test_execute_update_w_params_wo_param_types(self):
410411
with self.assertRaises(ValueError):
411412
transaction.execute_update(DML_QUERY_WITH_PARAM, PARAMS)
412413

413-
def _execute_update_helper(self, count=0, query_options=None):
414+
def _execute_update_helper(
415+
self,
416+
count=0,
417+
query_options=None,
418+
retry=gapic_v1.method.DEFAULT,
419+
timeout=gapic_v1.method.DEFAULT,
420+
):
414421
from google.protobuf.struct_pb2 import Struct
415422
from google.cloud.spanner_v1 import (
416423
ResultSet,
@@ -439,6 +446,8 @@ def _execute_update_helper(self, count=0, query_options=None):
439446
PARAM_TYPES,
440447
query_mode=MODE,
441448
query_options=query_options,
449+
retry=retry,
450+
timeout=timeout,
442451
)
443452

444453
self.assertEqual(row_count, 1)
@@ -466,6 +475,8 @@ def _execute_update_helper(self, count=0, query_options=None):
466475
)
467476
api.execute_sql.assert_called_once_with(
468477
request=expected_request,
478+
retry=retry,
479+
timeout=timeout,
469480
metadata=[("google-cloud-resource-prefix", database.name)],
470481
)
471482

@@ -477,6 +488,15 @@ def test_execute_update_new_transaction(self):
477488
def test_execute_update_w_count(self):
478489
self._execute_update_helper(count=1)
479490

491+
def test_execute_update_w_timeout_param(self):
492+
self._execute_update_helper(timeout=2.0)
493+
494+
def test_execute_update_w_retry_param(self):
495+
self._execute_update_helper(retry=gapic_v1.method.DEFAULT)
496+
497+
def test_execute_update_w_timeout_and_retry_params(self):
498+
self._execute_update_helper(retry=gapic_v1.method.DEFAULT, timeout=2.0)
499+
480500
def test_execute_update_error(self):
481501
database = _Database()
482502
database.spanner_api = self._make_spanner_api()

0 commit comments

Comments
 (0)