4848run_small = mode_int in (1 , 3 )
4949run_chunk = mode_int in (2 , 3 )
5050
51- delete_table_choice = input ("Delete the table at end? (y/N): " ).strip () or "n"
52- cleanup_table = delete_table_choice .lower () in ("y" , "yes" , "true" , "1" )
53-
5451delete_record_choice = input ("Delete the created record at end? (Y/n): " ).strip () or "y"
5552cleanup_record = delete_record_choice .lower () in ("y" , "yes" , "true" , "1" )
5653
54+ delete_table_choice = input ("Delete the table at end? (y/N): " ).strip () or "n"
55+ cleanup_table = delete_table_choice .lower () in ("y" , "yes" , "true" , "1" )
56+
5757credential = InteractiveBrowserCredential ()
5858client = DataverseClient (base_url = base_url , credential = credential )
5959
@@ -183,7 +183,7 @@ def backoff(op, *, delays=(0, 2, 5, 10, 20, 20)):
183183
184184
185185# --------------------------- Table ensure ---------------------------
186- TABLE_SCHEMA_NAME = "new_FileSample "
186+ TABLE_SCHEMA_NAME = "new_FileSample111 "
187187
188188
189189def ensure_table ():
@@ -192,7 +192,7 @@ def ensure_table():
192192 if existing :
193193 print ({"table" : TABLE_SCHEMA_NAME , "existed" : True })
194194 return existing
195- log ("client.create_table('new_FileSample ', schema={'new_Title': 'string'})" )
195+ log (f "client.create_table('{ TABLE_SCHEMA_NAME } ', schema={{ 'new_Title': 'string'} })" )
196196 info = backoff (lambda : client .create_table (TABLE_SCHEMA_NAME , {"new_Title" : "string" }))
197197 print ({"table" : TABLE_SCHEMA_NAME , "existed" : False , "metadata_id" : info .get ("metadata_id" )})
198198 return info
@@ -214,99 +214,6 @@ def ensure_table():
214214chunk_file_attr_schema = f"{ attr_prefix } _ChunkDocument" # attribute for streaming chunk upload demo
215215chunk_file_attr_logical = f"{ attr_prefix } _chunkdocument" # expected logical name
216216
217-
218- def ensure_file_attribute_generic (schema_name : str , label : str , key_prefix : str ):
219- meta_id = table_info .get ("metadata_id" )
220- if not meta_id :
221- print ({f"{ key_prefix } _attribute" : "skipped" , "reason" : "missing metadata_id" })
222- return False
223- odata = client ._get_odata ()
224- # Probe existing
225- try :
226- url = (
227- f"{ odata .api } /EntityDefinitions({ meta_id } )/Attributes?$select=SchemaName&$filter="
228- f"SchemaName eq '{ schema_name } '"
229- )
230- r = backoff (lambda : odata ._request ("get" , url ), delays = ATTRIBUTE_VISIBILITY_DELAYS )
231- val = []
232- try :
233- val = r .json ().get ("value" , [])
234- except Exception : # noqa: BLE001
235- pass
236- if any (a .get ("SchemaName" ) == schema_name for a in val if isinstance (a , dict )):
237- return True
238- except Exception as ex : # noqa: BLE001
239- print ({f"{ key_prefix } _file_attr_probe_error" : str (ex )})
240-
241- payload = {
242- "@odata.type" : "Microsoft.Dynamics.CRM.FileAttributeMetadata" ,
243- "SchemaName" : schema_name ,
244- "DisplayName" : {
245- "@odata.type" : "Microsoft.Dynamics.CRM.Label" ,
246- "LocalizedLabels" : [
247- {
248- "@odata.type" : "Microsoft.Dynamics.CRM.LocalizedLabel" ,
249- "Label" : label ,
250- "LanguageCode" : int (client ._config .language_code ),
251- }
252- ],
253- },
254- "RequiredLevel" : {"Value" : "None" },
255- }
256- try :
257- url = f"{ odata .api } /EntityDefinitions({ meta_id } )/Attributes"
258- backoff (lambda : odata ._request ("post" , url , json = payload ), delays = ATTRIBUTE_VISIBILITY_DELAYS )
259- print ({f"{ key_prefix } _file_attribute_created" : True })
260- time .sleep (2 )
261- return True
262- except Exception as ex : # noqa: BLE001
263- resp = getattr (ex , "response" , None )
264- body_l = None
265- try :
266- body_l = resp .text .lower () if getattr (resp , "text" , None ) else None
267- except Exception : # noqa: BLE001
268- pass
269- if body_l and ("duplicate" in body_l or "exists" in body_l ):
270- print ({f"{ key_prefix } _file_attribute_created" : False , "reason" : "already exists (race)" })
271- return True
272- print ({f"{ key_prefix } _file_attribute_created" : False , "error" : str (ex )})
273- return False
274-
275-
276- def wait_for_attribute_visibility (logical_name : str , label : str ):
277- if not logical_name or not entity_set :
278- return False
279- odata = client ._get_odata ()
280- probe_url = f"{ odata .api } /{ entity_set } ?$top=1&$select={ logical_name } "
281- waited = 0
282- last_error = None
283- for delay in ATTRIBUTE_VISIBILITY_DELAYS :
284- if delay :
285- time .sleep (delay )
286- waited += delay
287- try :
288- resp = odata ._request ("get" , probe_url )
289- try :
290- resp .json ()
291- except Exception : # noqa: BLE001
292- pass
293- if waited :
294- print ({f"{ label } _attribute_visible_wait_seconds" : waited })
295- return True
296- except Exception as ex : # noqa: BLE001
297- last_error = ex
298- continue
299- raise RuntimeError (f"Timed out waiting for attribute '{ logical_name } ' to materialize" ) from last_error
300-
301-
302- # Conditionally ensure each attribute only if its mode is selected
303- if run_small :
304- ensure_file_attribute_generic (small_file_attr_schema , "Small Document" , "small" )
305- wait_for_attribute_visibility (small_file_attr_logical , "small" )
306- if run_chunk :
307- ensure_file_attribute_generic (chunk_file_attr_schema , "Chunk Document" , "chunk" )
308- wait_for_attribute_visibility (chunk_file_attr_logical , "chunk" )
309-
310217# --------------------------- Record create ---------------------------
311218record_id = None
312219try :
0 commit comments