Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 174 |
-
def delete_record(container, record, partition_key_field=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
except exceptions.CosmosHttpResponseError as e:
|
| 198 |
-
|
|
|
|
| 199 |
except Exception as e:
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|