Skip to content

Commit 43d7ebf

Browse files
added create_table_if_not_exists method to table service client (#13385)
* added create_table_if_not_exists method to table service client, eseentially create table wrapped in try/except block * lint fix * fixing up issy's comments * table_name not name, fixed test bug
1 parent c77746b commit 43d7ebf

10 files changed

+515
-20
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
from ._deserialize import _convert_to_entity, _trim_service_metadata
2121
from ._entity import TableEntity
2222
from ._generated import AzureTable
23-
from ._generated.models import AccessPolicy, SignedIdentifier, TableProperties, QueryOptions
23+
from ._generated.models import (
24+
AccessPolicy,
25+
SignedIdentifier,
26+
TableProperties,
27+
QueryOptions
28+
)
2429
from ._serialize import _get_match_headers, _add_entity_properties
2530
from ._base_client import parse_connection_str
2631
from ._table_client_base import TableClientBase

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import functools
88
from typing import Any, Union
9-
from azure.core.exceptions import HttpResponseError
9+
from azure.core.exceptions import HttpResponseError, ResourceExistsError
1010
from azure.core.paging import ItemPaged
1111
from azure.core.tracing.decorator import distributed_trace
1212
from azure.core.pipeline import Pipeline
@@ -154,6 +154,30 @@ def create_table(
154154
table.create_table(**kwargs)
155155
return table
156156

157+
@distributed_trace
158+
def create_table_if_not_exists(
159+
self,
160+
table_name, # type: str
161+
**kwargs # type: Any
162+
):
163+
# type: (...) -> TableClient
164+
"""Creates a new table if it does not currently exist.
165+
If the table currently exists, the current table is
166+
returned.
167+
168+
:param table_name: The Table name.
169+
:type table_name: str
170+
:return: TableClient
171+
:rtype: ~azure.data.tables.TableClient
172+
:raises: ~azure.core.exceptions.HttpResponseError
173+
"""
174+
table = self.get_table_client(table_name=table_name)
175+
try:
176+
table.create_table(**kwargs)
177+
except ResourceExistsError:
178+
pass
179+
return table
180+
157181
@distributed_trace
158182
def delete_table(
159183
self,

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313
from azure.core.async_paging import AsyncItemPaged
14-
from azure.core.exceptions import HttpResponseError
14+
from azure.core.exceptions import HttpResponseError, ResourceExistsError
1515
from azure.core.pipeline import AsyncPipeline
1616
from azure.core.tracing.decorator import distributed_trace
1717
from azure.core.tracing.decorator_async import distributed_trace_async
@@ -196,6 +196,30 @@ async def create_table(
196196
await table.create_table(**kwargs)
197197
return table
198198

199+
@distributed_trace_async
200+
async def create_table_if_not_exists(
201+
self,
202+
table_name, # type: str
203+
**kwargs # type: Any
204+
):
205+
# type: (...) -> TableClient
206+
"""Creates a new table if it does not currently exist.
207+
If the table currently exists, the current table is
208+
returned.
209+
210+
:param table_name: The Table name.
211+
:type table_name: str
212+
:return: TableClient
213+
:rtype: ~azure.data.tables.aio.TableClient
214+
:raises: ~azure.core.exceptions.HttpResponseError
215+
"""
216+
table = self.get_table_client(table_name=table_name)
217+
try:
218+
await table.create_table(**kwargs)
219+
except ResourceExistsError:
220+
pass
221+
return table
222+
199223
@distributed_trace_async
200224
async def delete_table(
201225
self,

sdk/tables/azure-data-tables/tests/recordings/test_table.test_create_table_fail_on_exist.yaml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ interactions:
1515
DataServiceVersion:
1616
- '3.0'
1717
Date:
18-
- Wed, 19 Aug 2020 21:18:00 GMT
18+
- Thu, 27 Aug 2020 21:28:19 GMT
1919
User-Agent:
20-
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
20+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
2121
x-ms-date:
22-
- Wed, 19 Aug 2020 21:18:00 GMT
22+
- Thu, 27 Aug 2020 21:28:19 GMT
2323
x-ms-version:
2424
- '2019-07-07'
2525
method: POST
@@ -33,7 +33,7 @@ interactions:
3333
content-type:
3434
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
3535
date:
36-
- Wed, 19 Aug 2020 21:18:00 GMT
36+
- Thu, 27 Aug 2020 21:28:20 GMT
3737
location:
3838
- https://storagename.table.core.windows.net/Tables('pytablesync6d7c1113')
3939
server:
@@ -63,26 +63,26 @@ interactions:
6363
DataServiceVersion:
6464
- '3.0'
6565
Date:
66-
- Wed, 19 Aug 2020 21:18:00 GMT
66+
- Thu, 27 Aug 2020 21:28:20 GMT
6767
User-Agent:
68-
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
68+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
6969
x-ms-date:
70-
- Wed, 19 Aug 2020 21:18:00 GMT
70+
- Thu, 27 Aug 2020 21:28:20 GMT
7171
x-ms-version:
7272
- '2019-07-07'
7373
method: POST
7474
uri: https://storagename.table.core.windows.net/Tables
7575
response:
7676
body:
7777
string: '{"odata.error":{"code":"TableAlreadyExists","message":{"lang":"en-US","value":"The
78-
table specified already exists.\nRequestId:fbae76e2-b002-009f-206e-7649d9000000\nTime:2020-08-19T21:18:00.8893697Z"}}}'
78+
table specified already exists.\nRequestId:2e7b208e-d002-003b-7bb8-7c685f000000\nTime:2020-08-27T21:28:21.0100931Z"}}}'
7979
headers:
8080
cache-control:
8181
- no-cache
8282
content-type:
8383
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
8484
date:
85-
- Wed, 19 Aug 2020 21:18:00 GMT
85+
- Thu, 27 Aug 2020 21:28:20 GMT
8686
server:
8787
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
8888
transfer-encoding:
@@ -106,11 +106,11 @@ interactions:
106106
Content-Length:
107107
- '0'
108108
Date:
109-
- Wed, 19 Aug 2020 21:18:00 GMT
109+
- Thu, 27 Aug 2020 21:28:20 GMT
110110
User-Agent:
111-
- azsdk-python-storage-table/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
111+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
112112
x-ms-date:
113-
- Wed, 19 Aug 2020 21:18:00 GMT
113+
- Thu, 27 Aug 2020 21:28:20 GMT
114114
x-ms-version:
115115
- '2019-07-07'
116116
method: DELETE
@@ -124,7 +124,7 @@ interactions:
124124
content-length:
125125
- '0'
126126
date:
127-
- Wed, 19 Aug 2020 21:18:00 GMT
127+
- Thu, 27 Aug 2020 21:28:20 GMT
128128
server:
129129
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
130130
x-content-type-options:
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
interactions:
2+
- request:
3+
body: '{"TableName": "pytablesync2c5a0f7d"}'
4+
headers:
5+
Accept:
6+
- application/json;odata=minimalmetadata
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '36'
13+
Content-Type:
14+
- application/json;odata=nometadata
15+
DataServiceVersion:
16+
- '3.0'
17+
Date:
18+
- Thu, 27 Aug 2020 21:42:12 GMT
19+
User-Agent:
20+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
21+
x-ms-date:
22+
- Thu, 27 Aug 2020 21:42:12 GMT
23+
x-ms-version:
24+
- '2019-07-07'
25+
method: POST
26+
uri: https://storagename.table.core.windows.net/Tables
27+
response:
28+
body:
29+
string: '{"odata.metadata":"https://storagename.table.core.windows.net/$metadata#Tables/@Element","TableName":"pytablesync2c5a0f7d"}'
30+
headers:
31+
cache-control:
32+
- no-cache
33+
content-type:
34+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
35+
date:
36+
- Thu, 27 Aug 2020 21:42:12 GMT
37+
location:
38+
- https://storagename.table.core.windows.net/Tables('pytablesync2c5a0f7d')
39+
server:
40+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
41+
transfer-encoding:
42+
- chunked
43+
x-content-type-options:
44+
- nosniff
45+
x-ms-version:
46+
- '2019-07-07'
47+
status:
48+
code: 201
49+
message: Created
50+
- request:
51+
body: '{"TableName": "pytablesync2c5a0f7d"}'
52+
headers:
53+
Accept:
54+
- application/json;odata=minimalmetadata
55+
Accept-Encoding:
56+
- gzip, deflate
57+
Connection:
58+
- keep-alive
59+
Content-Length:
60+
- '36'
61+
Content-Type:
62+
- application/json;odata=nometadata
63+
DataServiceVersion:
64+
- '3.0'
65+
Date:
66+
- Thu, 27 Aug 2020 21:42:12 GMT
67+
User-Agent:
68+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
69+
x-ms-date:
70+
- Thu, 27 Aug 2020 21:42:12 GMT
71+
x-ms-version:
72+
- '2019-07-07'
73+
method: POST
74+
uri: https://storagename.table.core.windows.net/Tables
75+
response:
76+
body:
77+
string: '{"odata.error":{"code":"TableAlreadyExists","message":{"lang":"en-US","value":"The
78+
table specified already exists.\nRequestId:64cfdefd-d002-0057-80ba-7c2831000000\nTime:2020-08-27T21:42:13.5449901Z"}}}'
79+
headers:
80+
cache-control:
81+
- no-cache
82+
content-type:
83+
- application/json;odata=minimalmetadata;streaming=true;charset=utf-8
84+
date:
85+
- Thu, 27 Aug 2020 21:42:13 GMT
86+
server:
87+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
88+
transfer-encoding:
89+
- chunked
90+
x-content-type-options:
91+
- nosniff
92+
x-ms-version:
93+
- '2019-07-07'
94+
status:
95+
code: 409
96+
message: Conflict
97+
- request:
98+
body: null
99+
headers:
100+
Accept:
101+
- '*/*'
102+
Accept-Encoding:
103+
- gzip, deflate
104+
Connection:
105+
- keep-alive
106+
Content-Length:
107+
- '0'
108+
Date:
109+
- Thu, 27 Aug 2020 21:42:12 GMT
110+
User-Agent:
111+
- azsdk-python-data-tables/2019-07-07 Python/3.8.4 (Windows-10-10.0.19041-SP0)
112+
x-ms-date:
113+
- Thu, 27 Aug 2020 21:42:12 GMT
114+
x-ms-version:
115+
- '2019-07-07'
116+
method: DELETE
117+
uri: https://storagename.table.core.windows.net/Tables('pytablesync2c5a0f7d')
118+
response:
119+
body:
120+
string: ''
121+
headers:
122+
cache-control:
123+
- no-cache
124+
content-length:
125+
- '0'
126+
date:
127+
- Thu, 27 Aug 2020 21:42:13 GMT
128+
server:
129+
- Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
130+
x-content-type-options:
131+
- nosniff
132+
x-ms-version:
133+
- '2019-07-07'
134+
status:
135+
code: 204
136+
message: No Content
137+
version: 1

0 commit comments

Comments
 (0)