Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(file-commit): stop delete operations crashing the whole commit (#19654)

MarMar Labs701b82aa1e6b
diff --git a/api/db/services/file_commit_service.py b/api/db/services/file_commit_service.py
index 30a6df25ac..2269e1e19d 100644
--- a/api/db/services/file_commit_service.py
+++ b/api/db/services/file_commit_service.py
@@ -21,6 +21,7 @@ import json
import logging
from typing import Optional
+from api.db import FileType
from api.db.db_models import DB, FileCommit, FileCommitItem, File, User
from api.db.services.common_service import CommonService
from api.db.services.file_service import FileService
@@ -370,8 +371,26 @@ class FileCommitService(CommonService):
item["old_hash"] = old_hash
item["old_location"] = old_location
- # Soft-delete the file record
- File.update(status="0", update_time=current_timestamp()).where(File.id == file_id).execute()
+ # Remove the file record. The blob stays in the content-addressed object
+ # store, and history keeps its metadata in the tree_state tombstone below
+ # rather than in the File row.
+ #
+ # Folders are exempt: _build_hierarchical_tree resolves sub-folder parentage
+ # from live File rows, so dropping one makes every historical entry beneath it
+ # unreachable. File entries are read from tree_state, so they are unaffected.
+ row = File.get_or_none(File.id == file_id)
+ if row is not None and row.type == FileType.FOLDER.value:
+ # Drop the whole change, not just the delete: recording the tombstone and
+ # the commit item below would leave history calling the folder deleted
+ # while its row is still there.
+ logging.warning(
+ "create_commit: refusing to delete folder %s in commit for %s",
+ file_id,
+ folder_id,
+ )
+ continue
+ if row is not None:
+ File.delete().where(File.id == file_id).execute()
# Remove from tree state (mark deleted)
if file_id in tree_state:
diff --git a/test/testcases/restful_api/test_file_commit_routes_unit.py b/test/testcases/restful_api/test_file_commit_routes_unit.py
index d3b9f21dce..a47b8c6d47 100644
--- a/test/testcases/restful_api/test_file_commit_routes_unit.py
+++ b/test/testcases/restful_api/test_file_commit_routes_unit.py
@@ -25,6 +25,7 @@ import functools
import importlib.util
import logging
import sys
+from enum import StrEnum
from pathlib import Path
from types import ModuleType, SimpleNamespace
@@ -111,7 +112,6 @@ class FileTestModel(BaseTestModel):
size = BigIntegerField(default=0, index=True)
type = CharField(max_length=32, index=True)
source_type = CharField(max_length=128, default="", index=True)
- status = CharField(max_length=1, null=True, default="1", index=True)
create_time = BigIntegerField(null=True, index=True)
create_date = CharField(null=True, max_length=32, index=True)
update_time = BigIntegerField(null=True, index=True)
@@ -320,7 +320,8 @@ def _load_module(monkeypatch):
db_pkg.__path__ = [str(repo_root / "api" / "db")]
db_pkg.UserTenantRole = type("UserTenantRole", (), {k: k for k in ("OWNER", "ADMIN", "NORMAL", "INVITE")})
db_pkg.TenantPermission = type("TenantPermission", (), {"ME": "me", "TEAM": "team"})
- db_pkg.FileType = type("FileType", (), {"FOLDER": "folder", "DOC": "doc", "VISUAL": "visual", "AURAL": "aural", "VIRTUAL": "virtual", "PDF": "pdf", "OTHER": "other"})
+ # Mirror api.db.FileType, which is a StrEnum, so members carry .value like the real one.
+ db_pkg.FileType = StrEnum("FileType", {"FOLDER": "folder", "DOC": "doc", "VISUAL": "visual", "AURAL": "aural", "VIRTUAL": "virtual", "PDF": "pdf", "OTHER": "other"})
db_pkg.KNOWLEDGEBASE_FOLDER_NAME = ".knowledgebase"
db_pkg.SKILLS_FOLDER_NAME = "skills"
monkeypatch.setitem(sys.modules, "api.db", db_pkg)
@@ -520,6 +521,30 @@ def test_create_commit_delete(monkeypatch):
)
res = _run(module.create_commit("root-folder"))
assert res["code"] == 0
+ # The File row is removed rather than flagged, because the model has no status column.
+ assert FileTestModel.get_or_none(FileTestModel.id == "f1") is None
+
+
+def test_create_commit_delete_folder_is_refused(monkeypatch):
+ module = _load_module(monkeypatch)
+ FileTestModel.create(id="d1", parent_id="root-folder", tenant_id="t1", created_by="test-user", name="sub", type="folder")
+
+ _setup_request(
+ module,
+ json_payload={
+ "message": "delete folder",
+ "files": [{"file_id": "d1", "file_name": "sub", "operation": "delete"}],
+ },
+ )
+ res = _run(module.create_commit("root-folder"))
+ assert res["code"] == 0
+ # A dropped folder row would strand every historical entry beneath it, because
+ # the hierarchy is rebuilt from live File rows rather than from tree state.
+ assert FileTestModel.get_or_none(FileTestModel.id == "d1") is not None
+ # The refusal drops the whole change, so history must not call the folder deleted.
+ items = list(FileCommitItemTestModel.select().where(FileCommitItemTestModel.file_id == "d1"))
+ assert items == []
@pytest.mark.p2