Repository navigation
Conversation
delete_datasets() accessed f2d[0].file_id without checking for an empty lookup result, crashing on API/connector documents with no file link. Mirror the existing if f2d guard from file_service.py. Fixes infiniflow#15532
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe PR guards File deletion inside delete_datasets() by checking File2DocumentService.get_by_document_id(...) before using its results, and adds unit tests covering both the no-mapping and mapping-present deletion paths. ChangesDataset deletion robustness
Sequence Diagram(s)sequenceDiagram
participant DeleteDatasets
participant File2DocumentService
participant FileService
participant DocumentService
DeleteDatasets->>File2DocumentService: get_by_document_id(doc.id)
File2DocumentService-->>DeleteDatasets: [] or [File2Document]
alt mapping exists
DeleteDatasets->>FileService: filter_delete(... File.id == f2d[0].file_id)
else no mapping
DeleteDatasets->>DeleteDatasets: log warning and skip file deletion
end
DeleteDatasets->>DocumentService: delete_by_document_id(doc.id)
DeleteDatasets-->>Client: return (ok, result)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
api/apps/services/dataset_api_service.py (1)
147-154: ⚡ Quick winConsider adding logging when skipping file deletion.
The guard correctly prevents the IndexError, but per coding guidelines for
api/**/*.pyfiles, new conditional flows should include logging. Whenf2dis empty and file deletion is skipped, logging this event would help with debugging and observability.📋 Suggested logging addition
f2d = File2DocumentService.get_by_document_id(doc.id) if f2d: FileService.filter_delete( [ File.source_type == FileSource.KNOWLEDGEBASE, File.id == f2d[0].file_id, ] ) +else: + logging.debug("Skipping linked file deletion for document %s (no File2Document mapping)", doc.id) File2DocumentService.delete_by_document_id(doc.id)As per coding guidelines: "Add logging for new flows" for
api/**/*.pyfiles.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/services/dataset_api_service.py` around lines 147 - 154, The branch that skips file deletion when f2d is empty lacks observability; update the conditional around FileService.filter_delete/File2DocumentService.delete_by_document_id to log a concise debug/info message when f2d is falsy (e.g., "No File2Document found for document {doc.id}, skipping file delete") so callers can trace this flow; use the existing logger in this module (or create one named logger) and include doc.id and the FileSource.KNOWLEDGEBASE context in the log entry for clarity.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@api/apps/services/dataset_api_service.py`:
- Around line 147-154: The branch that skips file deletion when f2d is empty
lacks observability; update the conditional around
FileService.filter_delete/File2DocumentService.delete_by_document_id to log a
concise debug/info message when f2d is falsy (e.g., "No File2Document found for
document {doc.id}, skipping file delete") so callers can trace this flow; use
the existing logger in this module (or create one named logger) and include
doc.id and the FileSource.KNOWLEDGEBASE context in the log entry for clarity.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d6f16e98-f45a-4c45-ae64-37f6436a92d1
📒 Files selected for processing (2)
api/apps/services/dataset_api_service.pytest/unit_test/api/apps/services/test_delete_datasets.py
|
@yingfeng |
|
@wangq8 , @Haruko386 , @JinHai-CN @yingfeng @xugangqiang |
…lete Address review feedback: document cleanup proceeds without linked file delete, with a warning for observability when the mapping row is absent.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15533 +/- ##
==========================================
- Coverage 94.56% 93.16% -1.40%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 668 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…nfiniflow#15715) Resolve conflicts: drop the duplicate File2DocumentDAO.Create (main added an identical method in infiniflow#15533) and union fakeDocumentService's upload* and metadata* test fields. Build, vet, gofmt and the service/handler/dao/router tests (incl. the upload-linkage regression tests) pass.
Summary
Fixes #15532 —
delete_datasets()crashes withIndexErrorwhen a document has noFile2Documentrow.delete_datasets()indataset_api_service.pycalledFile2DocumentService.get_by_document_id()and immediately accessedf2d[0].file_idwithout checking whether the lookup returned any rows. Documents created via API ingestion or connector sync may exist without a linked file record, causing dataset deletion to abort with HTTP 500.This PR mirrors the existing guard already used in
file_service.pyanddocument_api_service.py.Test plan
pytest test/unit_test/api/apps/services/test_delete_datasets.py -vf2dlookup completes without crash