Skip to content

Commit 8e20b81

Browse files
[Tables] use etag from entity if match condition is given (#18271)
* use etag from entity if match condition is given * added tests, fixed another one, updated changelog * tests for batch * adding one more test for a scenario to catch, fixing up sloppiness * unused overload
1 parent 0a58f4a commit 8e20b81

27 files changed

+3503
-175
lines changed

sdk/tables/azure-data-tables/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Added support for Azurite storage emulator
2929
* Throws a `RequestTooLargeError` on transaction requests that return a 413 error code
3030
* Added support for Int64 and Binary types in query filters
31+
* On `update_entity` and `delete_entity` if no `etag` is supplied via kwargs, the `etag` in the entity will be used if it is in the entity.
3132

3233
## 12.0.0b6 (2021-04-06)
3334
* Updated deserialization of datetime fields in entities to support preservation of the service format with additional decimal place.

sdk/tables/azure-data-tables/azure/data/tables/_table_batch.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,19 @@ def update(
238238
self._verify_partition_key(entity)
239239
temp = entity.copy()
240240

241+
match_condition = kwargs.pop("match_condition", None)
242+
etag = kwargs.pop("etag", None)
243+
if match_condition and not etag:
244+
try:
245+
etag = entity.metadata.get("etag", None)
246+
except (AttributeError, TypeError):
247+
pass
248+
241249
if_match, _ = _get_match_headers(
242250
kwargs=dict(
243251
kwargs,
244-
etag=kwargs.pop("etag", None),
245-
match_condition=kwargs.pop("match_condition", None),
252+
etag=etag,
253+
match_condition=match_condition,
246254
),
247255
etag_param="etag",
248256
match_param="match_condition",
@@ -516,16 +524,25 @@ def delete(
516524
partition_key = temp["PartitionKey"]
517525
row_key = temp["RowKey"]
518526

527+
match_condition = kwargs.pop("match_condition", None)
528+
etag = kwargs.pop("etag", None)
529+
if match_condition and not etag:
530+
try:
531+
etag = entity.metadata.get("etag", None)
532+
except (AttributeError, TypeError):
533+
pass
534+
519535
if_match, _ = _get_match_headers(
520536
kwargs=dict(
521537
kwargs,
522-
etag=kwargs.pop("etag", None),
523-
match_condition=kwargs.pop("match_condition", None),
538+
etag=etag,
539+
match_condition=match_condition,
524540
),
525541
etag_param="etag",
526542
match_param="match_condition",
527543
)
528544

545+
529546
self._batch_delete_entity(
530547
table=self.table_name,
531548
partition_key=partition_key,

sdk/tables/azure-data-tables/azure/data/tables/_table_client.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,19 @@ def delete_entity(self, *args, **kwargs):
323323
if not row_key:
324324
row_key = args[1]
325325

326+
match_condition = kwargs.pop("match_condition", None)
327+
etag = kwargs.pop("etag", None)
328+
if match_condition and entity and not etag:
329+
try:
330+
etag = entity.metadata.get("etag", None)
331+
except (AttributeError, TypeError):
332+
pass
326333

327334
if_match, _ = _get_match_headers(
328335
kwargs=dict(
329336
kwargs,
330-
etag=kwargs.pop("etag", None),
331-
match_condition=kwargs.pop("match_condition", None),
337+
etag=etag,
338+
match_condition=match_condition,
332339
),
333340
etag_param="etag",
334341
match_param="match_condition",
@@ -414,12 +421,19 @@ def update_entity(
414421
:dedent: 8
415422
:caption: Updating an already exiting entity in a Table
416423
"""
424+
match_condition = kwargs.pop("match_condition", None)
425+
etag = kwargs.pop("etag", None)
426+
if match_condition and not etag:
427+
try:
428+
etag = entity.metadata.get("etag", None)
429+
except (AttributeError, TypeError):
430+
pass
417431

418432
if_match, _ = _get_match_headers(
419433
kwargs=dict(
420434
kwargs,
421-
etag=kwargs.pop("etag", None),
422-
match_condition=kwargs.pop("match_condition", None),
435+
etag=etag,
436+
match_condition=match_condition,
423437
),
424438
etag_param="etag",
425439
match_param="match_condition",

sdk/tables/azure-data-tables/azure/data/tables/aio/_table_batch_async.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,19 @@ def update(
214214
self._verify_partition_key(entity)
215215
temp = entity.copy()
216216

217+
match_condition = kwargs.pop("match_condition", None)
218+
etag = kwargs.pop("etag", None)
219+
if match_condition and not etag:
220+
try:
221+
etag = entity.metadata.get("etag", None)
222+
except (AttributeError, TypeError):
223+
pass
224+
217225
if_match, _ = _get_match_headers(
218226
kwargs=dict(
219227
kwargs,
220-
etag=kwargs.pop("etag", None),
221-
match_condition=kwargs.pop("match_condition", None),
228+
etag=etag,
229+
match_condition=match_condition,
222230
),
223231
etag_param="etag",
224232
match_param="match_condition",
@@ -484,11 +492,20 @@ def delete(
484492
temp = entity.copy()
485493
partition_key = temp["PartitionKey"]
486494
row_key = temp["RowKey"]
495+
496+
match_condition = kwargs.pop("match_condition", None)
497+
etag = kwargs.pop("etag", None)
498+
if match_condition and not etag:
499+
try:
500+
etag = entity.metadata.get("etag", None)
501+
except (AttributeError, TypeError):
502+
pass
503+
487504
if_match, _ = _get_match_headers(
488505
kwargs=dict(
489506
kwargs,
490-
etag=kwargs.pop("etag", None),
491-
match_condition=kwargs.pop("match_condition", None),
507+
etag=etag,
508+
match_condition=match_condition,
492509
),
493510
etag_param="etag",
494511
match_param="match_condition",

sdk/tables/azure-data-tables/azure/data/tables/aio/_table_client_async.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,24 @@ async def delete_entity(self, *args: Union[TableEntity, str], **kwargs: Any) ->
314314
if not row_key:
315315
row_key = args[1]
316316

317+
match_condition = kwargs.pop("match_condition", None)
318+
etag = kwargs.pop("etag", None)
319+
if match_condition and entity and not etag:
320+
try:
321+
etag = entity.metadata.get("etag", None)
322+
except (AttributeError, TypeError):
323+
pass
324+
317325
if_match, _ = _get_match_headers(
318326
kwargs=dict(
319327
kwargs,
320-
etag=kwargs.pop("etag", None),
321-
match_condition=kwargs.pop("match_condition", None),
328+
etag=etag,
329+
match_condition=match_condition,
322330
),
323331
etag_param="etag",
324332
match_param="match_condition",
325333
)
334+
326335
try:
327336
await self._client.table.delete_entity(
328337
table=self.table_name,
@@ -402,11 +411,19 @@ async def update_entity(
402411
:dedent: 8
403412
:caption: Querying entities from a TableClient
404413
"""
414+
match_condition = kwargs.pop("match_condition", None)
415+
etag = kwargs.pop("etag", None)
416+
if match_condition and entity and not etag:
417+
try:
418+
etag = entity.metadata.get("etag", None)
419+
except (AttributeError, TypeError):
420+
pass
421+
405422
if_match, _ = _get_match_headers(
406423
kwargs=dict(
407424
kwargs,
408-
etag=kwargs.pop("etag", None),
409-
match_condition=kwargs.pop("match_condition", None),
425+
etag=etag,
426+
match_condition=match_condition,
410427
),
411428
etag_param="etag",
412429
match_param="match_condition",

0 commit comments

Comments
 (0)