Jash Doshi commited on
Commit
ebfe0e8
·
1 Parent(s): 2dfe817

fix: Delete race condition - properly track ChromaDB items during deletion

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -759,7 +759,21 @@ def delete_card_endpoint(mode, item_id):
759
  _save_user_data(user_api_key, mode, user_data)
760
 
761
  # Step 1.5: Delete from ChromaDB (RAG knowledge base)
 
762
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
763
  if mode == 'cards':
764
  # Remove card document from ChromaDB
765
  rag_core.remove_document_from_knowledge_base(user_api_key, item_id, mode)
@@ -770,6 +784,12 @@ def delete_card_endpoint(mode, item_id):
770
  if contact_id:
771
  # Contact deleted - re-index the brochure's contacts document
772
  brochure = next((b for b in user_data if b.get('id') == item_id), None)
 
 
 
 
 
 
773
  if brochure:
774
  contacts_doc_id = f"{item_id}_contacts"
775
  rag_core.remove_document_from_knowledge_base(user_api_key, contacts_doc_id, mode)
@@ -797,6 +817,7 @@ def delete_card_endpoint(mode, item_id):
797
  print(f"ChromaDB: Removed brochure {item_id} documents and metadata from knowledge base")
798
  except Exception as e:
799
  print(f"ChromaDB removal warning: {e}")
 
800
 
801
  # ## FINAL DATABASE CODE ##
802
  # Step 2: Delete from Database (New Logic)
@@ -825,8 +846,8 @@ def delete_card_endpoint(mode, item_id):
825
  print(f"Database record deleted for brochure json_id: {item_id}")
826
  return jsonify({"success": True})
827
 
828
- if not item_found_in_json:
829
- # Try to find in ChromaDB if not in JSON
830
  chroma_data = rag_core.load_all_metadata_from_chroma(user_api_key, mode)
831
  if chroma_data:
832
  if mode == 'cards':
 
759
  _save_user_data(user_api_key, mode, user_data)
760
 
761
  # Step 1.5: Delete from ChromaDB (RAG knowledge base)
762
+ item_found_in_chroma = False
763
  try:
764
+ # Check if item exists in ChromaDB before deleting
765
+ chroma_data = rag_core.load_all_metadata_from_chroma(user_api_key, mode)
766
+ if chroma_data:
767
+ if mode == 'cards':
768
+ item_found_in_chroma = any(c.get('id') == item_id for c in chroma_data)
769
+ elif mode == 'brochures':
770
+ if contact_id:
771
+ brochure = next((b for b in chroma_data if b.get('id') == item_id), None)
772
+ if brochure:
773
+ item_found_in_chroma = any(c.get('id') == contact_id for c in brochure.get('contacts', []))
774
+ else:
775
+ item_found_in_chroma = any(b.get('id') == item_id for b in chroma_data)
776
+
777
  if mode == 'cards':
778
  # Remove card document from ChromaDB
779
  rag_core.remove_document_from_knowledge_base(user_api_key, item_id, mode)
 
784
  if contact_id:
785
  # Contact deleted - re-index the brochure's contacts document
786
  brochure = next((b for b in user_data if b.get('id') == item_id), None)
787
+ # Also check ChromaDB data if not found in JSON
788
+ if not brochure and chroma_data:
789
+ brochure = next((b for b in chroma_data if b.get('id') == item_id), None)
790
+ if brochure:
791
+ # Remove the contact from the brochure in ChromaDB
792
+ brochure['contacts'] = [c for c in brochure.get('contacts', []) if c.get('id') != contact_id]
793
  if brochure:
794
  contacts_doc_id = f"{item_id}_contacts"
795
  rag_core.remove_document_from_knowledge_base(user_api_key, contacts_doc_id, mode)
 
817
  print(f"ChromaDB: Removed brochure {item_id} documents and metadata from knowledge base")
818
  except Exception as e:
819
  print(f"ChromaDB removal warning: {e}")
820
+
821
 
822
  # ## FINAL DATABASE CODE ##
823
  # Step 2: Delete from Database (New Logic)
 
846
  print(f"Database record deleted for brochure json_id: {item_id}")
847
  return jsonify({"success": True})
848
 
849
+ if not item_found_in_json and not item_found_in_chroma:
850
+ # Try to find in ChromaDB if not in JSON (should rarely happen now)
851
  chroma_data = rag_core.load_all_metadata_from_chroma(user_api_key, mode)
852
  if chroma_data:
853
  if mode == 'cards':