awacke1 commited on
Commit
3b9852b
ยท
verified ยท
1 Parent(s): 0d64ea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -25
app.py CHANGED
@@ -170,34 +170,52 @@ def update_record(container, updated_record):
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} ๐Ÿ˜ฑ"
172
 
173
- # ๐Ÿ—‘๏ธ Delete record โ€“ query first to get the proper partition key value from "testPART"
174
- def delete_record(container, record, partition_key_field="testPART"):
 
 
 
 
 
 
 
 
 
 
 
175
  try:
176
- # Query for the document using its id
177
- query = "SELECT * FROM c WHERE c.id=@id"
178
- parameters = [{"name": "@id", "value": record["id"]}]
179
- docs = list(container.query_items(
180
- query=query,
181
- parameters=parameters,
182
- enable_cross_partition_query=True
183
- ))
184
- if not docs:
185
- # Document not found; treat as already deleted
186
- return True, f"Record {record['id']} not found (already deleted). ๐Ÿ—‘๏ธ"
187
-
188
- doc = docs[0]
189
- # Retrieve the partition key value from the document (adjust field name as needed)
190
- partition_key_value = doc.get(partition_key_field)
191
- if partition_key_value is None:
192
- return False, f"Partition key field '{partition_key_field}' not found in document {doc['id']}."
193
-
194
- # Delete the document using its id and the actual partition key value
195
- container.delete_item(item=doc["id"], partition_key=partition_key_value)
196
- return True, f"Record {doc['id']} successfully deleted. ๐Ÿ—‘๏ธ"
 
 
 
197
  except exceptions.CosmosHttpResponseError as e:
198
- return False, f"HTTP error: {str(e)} ๐Ÿšจ"
 
199
  except Exception as e:
200
- return False, f"Unexpected error: {traceback.format_exc()} ๐Ÿ˜ฑ"
 
 
 
201
 
202
 
203
  # ๐Ÿ’พ Save a new document to Cosmos DB with extra fields
 
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} ๐Ÿ˜ฑ"
172
 
173
+ # ๐Ÿ—‘๏ธ Delete a record from Cosmos DB
174
+ def delete_record(container, record, partition_key_field=None):
175
+ """
176
+ Delete a record from Cosmos DB using its ID and partition key.
177
+
178
+ Args:
179
+ container: Cosmos DB container client
180
+ record: Dictionary containing at least 'id' and optionally the partition key field
181
+ partition_key_field: Name of the field containing the partition key value (optional)
182
+
183
+ Returns:
184
+ tuple: (success: bool, message: str)
185
+ """
186
  try:
187
+ # Ensure record has an ID
188
+ if "id" not in record:
189
+ return False, "Record must contain an 'id' field. ๐Ÿ›‘"
190
+
191
+ doc_id = record["id"]
192
+
193
+ # Determine partition key value
194
+ if partition_key_field:
195
+ # Use specified partition key field from the record
196
+ partition_key_value = record.get(partition_key_field)
197
+ if partition_key_value is None:
198
+ return False, f"Partition key field '{partition_key_field}' not found in record {doc_id}. ๐Ÿ›‘"
199
+ else:
200
+ # If no partition key field specified, try to use the ID as the partition key
201
+ # (common when partition key is same as ID)
202
+ partition_key_value = doc_id
203
+
204
+ # Perform the deletion
205
+ container.delete_item(item=doc_id, partition_key=partition_key_value)
206
+ return True, f"Record {doc_id} successfully deleted. ๐Ÿ—‘๏ธ"
207
+
208
+ except exceptions.CosmosResourceNotFoundError:
209
+ # Document doesn't exist, treat as success since goal is deletion
210
+ return True, f"Record {doc_id} not found (already deleted?). ๐Ÿ—‘๏ธ"
211
  except exceptions.CosmosHttpResponseError as e:
212
+ # Specific HTTP errors (e.g., wrong partition key)
213
+ return False, f"HTTP error deleting {doc_id}: {str(e)}. ๐Ÿšจ"
214
  except Exception as e:
215
+ # Unexpected errors with full traceback
216
+ return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. ๐Ÿ˜ฑ"
217
+
218
+
219
 
220
 
221
  # ๐Ÿ’พ Save a new document to Cosmos DB with extra fields