RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(file): record commit deletions without a missing status column (#21796)
CCLUCKY0d159a974071
diff --git a/internal/handler/file_commit.go b/internal/handler/file_commit.goindex 5ea68ef3da..d7dc072cd5 100644--- a/internal/handler/file_commit.go+++ b/internal/handler/file_commit.go@@ -18,12 +18,14 @@ package handlerimport ("context"+ "errors""fmt""ragflow/internal/common""ragflow/internal/dao""ragflow/internal/entity""ragflow/internal/permission""ragflow/internal/service"+ "ragflow/internal/service/file""strconv""strings"@@ -200,6 +202,12 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {ctx := c.Request.Context()commit, err := h.commitService.CreateCommit(ctx, folderID, user.ID, req.Message, req.Files)if err != nil {+ // A delete change for a file that is still in the folder is caller+ // input, not a server fault.+ if errors.Is(err, file.ErrDeleteLiveFile) {+ common.ErrorWithCode(c, common.CodeBadRequest, err.Error())+ return+ }jsonInternalError(c, err)return}diff --git a/internal/handler/file_commit_test.go b/internal/handler/file_commit_test.goindex 2d268ed989..fa73595f7c 100644--- a/internal/handler/file_commit_test.go+++ b/internal/handler/file_commit_test.go@@ -19,6 +19,7 @@ package handlerimport ("context""encoding/json"+ "fmt""net/http""net/http/httptest""strings"@@ -29,6 +30,7 @@ import ("ragflow/internal/entity""ragflow/internal/permission""ragflow/internal/permission/testsupport"+ "ragflow/internal/service/file""github.com/gin-gonic/gin""github.com/glebarez/sqlite"@@ -276,6 +278,31 @@ func TestFileCommit_CreateCommit_Success(t *testing.T) {}}+func TestFileCommit_CreateCommit_DeleteLiveFileIsBadRequest(t *testing.T) {+ r, mock := setupFileCommitTest("user-1")+ mock.createCommitFn = func(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {+ return nil, fmt.Errorf("%w: f1", file.ErrDeleteLiveFile)+ }++ body := `{"message": "remove", "files": [{"file_id": "f1", "file_name": "test.txt", "operation": "delete"}]}`+ w := httptest.NewRecorder()+ req, _ := http.NewRequest("POST", "/api/v1/folders/folder-1/commits", strings.NewReader(body))+ req.Header.Set("Content-Type", "application/json")+ r.ServeHTTP(w, req)++ if w.Code != http.StatusOK {+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())+ }+ var resp map[string]interface{}+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatal(err)+ }+ // A rejected deletion is caller input, not a server error.+ if code, _ := resp["code"].(float64); code != float64(common.CodeBadRequest) {+ t.Errorf("expected bad request, got code %v", code)+ }+}+func TestFileCommit_CreateCommit_NoAuth(t *testing.T) {r := setupFileCommitTestNoAuth()body := `{"message": "test", "files": [{"file_id": "f1", "file_name": "t.txt", "operation": "add"}]}`diff --git a/internal/service/file/file_commit.go b/internal/service/file/file_commit.goindex 9394e7cdfc..dc5b725f4e 100644--- a/internal/service/file/file_commit.go+++ b/internal/service/file/file_commit.go@@ -21,6 +21,7 @@ import ("crypto/sha256""encoding/hex""encoding/json"+ "errors""fmt""ragflow/internal/common""ragflow/internal/dao"@@ -56,6 +57,37 @@ func NewFileCommitService() *FileCommitService {}}+// ErrDeleteLiveFile rejects a delete change for a file that still exists in the+// commit folder. Recording the tombstone would hide the live file from+// GetUncommittedChanges, which skips every committed tombstone and marks the+// entry as processed, so later edits would never be reported.+var ErrDeleteLiveFile = errors.New("cannot commit the deletion of a file that is still in the folder")++// fileTreeContains reports whether fileID, or one of its ancestors, is folderID.+// It walks from the file upwards rather than scanning the folder, and treats an+// unknown ID as outside: a deletion is only valid for a file that is already+// gone. The seen set stops a corrupted parent_id cycle.+func (s *FileCommitService) fileTreeContains(ctx context.Context, tx *gorm.DB, folderID, fileID string) (bool, error) {+ seen := make(map[string]bool)+ current := fileID+ for current != "" && !seen[current] {+ seen[current] = true+ if current == folderID {+ return true, nil+ }+ var file entity.File+ err := tx.WithContext(ctx).Select("id", "parent_id").Where("id = ?", current).First(&file).Error+ if errors.Is(err, gorm.ErrRecordNotFound) {+ return false, nil+ }+ if err != nil {+ return false, fmt.Errorf("resolve file %s: %w", fileID, err)+ }+ current = file.ParentID+ }+ return false, nil+}+// CreateCommit creates a new commit for a workspace folderfunc (s *FileCommitService) CreateCommit(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {// 1. Get the latest commit for this folder@@ -182,8 +214,19 @@ func (s *FileCommitService) CreateCommit(ctx context.Context, folderID, authorID}}- if err := tx.Model(&entity.File{}).Where("id = ?", change.FileID).Update("status", "0").Error; err != nil {- return fmt.Errorf("failed to soft-delete file: %w", err)+ // The file table has no status column: a deleted file is one+ // already removed from the live tree, so the commit only keeps+ // the tombstone in tree_state for history and diffs. A file+ // that is still live would otherwise be hidden from+ // GetUncommittedChanges for good: that scan marks every+ // committed entry as processed and skips tombstones, so the+ // live row is reported as neither deleted nor added.+ liveInFolder, err := s.fileTreeContains(ctx, tx, folderID, change.FileID)+ if err != nil {+ return err+ }+ if liveInFolder {+ return fmt.Errorf("%w: %s", ErrDeleteLiveFile, change.FileID)}if entry, ok := treeState[change.FileID]; ok {diff --git a/internal/service/file/file_commit_delete_test.go b/internal/service/file/file_commit_delete_test.gonew file mode 100644index 0000000000..436d9ca3a7--- /dev/null+++ b/internal/service/file/file_commit_delete_test.go@@ -0,0 +1,146 @@+package file++import (+ "encoding/json"+ "errors"+ "testing"++ "ragflow/internal/dao"+ "ragflow/internal/entity"++ "github.com/glebarez/sqlite"+ "gorm.io/gorm"+)++func newCommitDeleteTestDB(t *testing.T) *gorm.DB {+ t.Helper()+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})+ if err != nil {+ t.Fatalf("open sqlite: %v", err)+ }+ if sqlDB, serr := db.DB(); serr == nil {+ sqlDB.SetMaxOpenConns(1)+ }+ if err := db.AutoMigrate(&entity.FileCommit{}, &entity.FileCommitItem{}, &entity.File{}); err != nil {+ t.Fatalf("auto migrate: %v", err)+ }+ old := dao.DB+ dao.DB = db+ t.Cleanup(func() { dao.DB = old })+ return db+}++// A committed file whose live row is already gone is the only shape+// GetUncommittedChanges reports as a deletion, so the delete operation must+// record the tombstone without touching the file table.+func TestCreateCommit_DeleteRecordsTombstoneWhenLiveFileIsGone(t *testing.T) {+ db := newCommitDeleteTestDB(t)+ tree := `{"f1":{"hash":"h1","location":".objects/h1","name":"a.txt","size":3,"status":"1","parent_id":"folder-1"}}`+ parent := &entity.FileCommit{ID: "c1", FolderID: "folder-1", Message: "init", AuthorID: "u1", FileCount: 1, TreeState: &tree}+ if err := db.Create(parent).Error; err != nil {+ t.Fatalf("seed parent commit: %v", err)+ }++ commit, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove a.txt", []entity.FileChange{+ {FileID: "f1", FileName: "a.txt", Operation: "delete"},+ })+ if err != nil {+ t.Fatalf("CreateCommit delete: %v", err)+ }++ var items []entity.FileCommitItem+ if err := db.Where("commit_id = ?", commit.ID).Find(&items).Error; err != nil {+ t.Fatalf("load commit items: %v", err)+ }+ if len(items) != 1 || items[0].Operation != "delete" {+ t.Fatalf("expected one delete item, got %+v", items)+ }+ if items[0].OldHash == nil || *items[0].OldHash != "h1" {+ t.Fatalf("expected old hash h1, got %v", items[0].OldHash)+ }+ if items[0].OldLocation == nil || *items[0].OldLocation != ".objects/h1" {+ t.Fatalf("expected old location .objects/h1, got %v", items[0].OldLocation)+ }++ var saved entity.FileCommit+ if err := db.First(&saved, "id = ?", commit.ID).Error; err != nil {+ t.Fatalf("load commit: %v", err)+ }+ var state map[string]map[string]interface{}+ if err := json.Unmarshal([]byte(*saved.TreeState), &state); err != nil {+ t.Fatalf("unmarshal tree state: %v", err)+ }+ if state["f1"]["status"] != "0" {+ t.Fatalf("expected f1 status 0 in tree state, got %v", state["f1"]["status"])+ }+ if state["f1"]["hash"] != "h1" {+ t.Fatalf("expected the tombstone to keep the old hash, got %v", state["f1"]["hash"])+ }+}++func seedLiveFile(t *testing.T, db *gorm.DB, id, parentID string) {+ t.Helper()+ if err := db.Create(&entity.File{ID: id, ParentID: parentID, TenantID: "t1", CreatedBy: "u1", Name: id + ".txt", Type: "file"}).Error; err != nil {+ t.Fatalf("seed file %s: %v", id, err)+ }+}++func seedFolder(t *testing.T, db *gorm.DB, id, parentID string) {+ t.Helper()+ if err := db.Create(&entity.File{ID: id, ParentID: parentID, TenantID: "t1", CreatedBy: "u1", Name: id, Type: "folder"}).Error; err != nil {+ t.Fatalf("seed folder %s: %v", id, err)+ }+}++// A tombstone for a file that is still in the folder would hide that file from+// GetUncommittedChanges: the scan marks committed entries as processed and+// skips tombstones, so the live row is reported as neither deleted nor added.+func TestCreateCommit_RejectsDeleteOfLiveFile(t *testing.T) {+ db := newCommitDeleteTestDB(t)+ tree := `{+ "f2":{"hash":"h2","location":".objects/h2","name":"a.txt","size":1,"status":"1","parent_id":"folder-1"},+ "f4":{"hash":"h4","location":".objects/h4","name":"b.txt","size":1,"status":"1","parent_id":"sub-1"}+ }`+ parent := &entity.FileCommit{ID: "c1", FolderID: "folder-1", Message: "init", AuthorID: "u1", TreeState: &tree}+ if err := db.Create(parent).Error; err != nil {+ t.Fatalf("seed parent commit: %v", err)+ }+ seedLiveFile(t, db, "f2", "folder-1")+ seedFolder(t, db, "sub-1", "folder-1")+ seedLiveFile(t, db, "f4", "sub-1")++ for _, fileID := range []string{"f2", "f4"} {+ _, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove "+fileID, []entity.FileChange{+ {FileID: fileID, FileName: fileID + ".txt", Operation: "delete"},+ })+ if !errors.Is(err, ErrDeleteLiveFile) {+ t.Fatalf("delete of live file %s: err = %v, want ErrDeleteLiveFile", fileID, err)+ }+ }++ var commits int64+ if err := db.Model(&entity.FileCommit{}).Where("id <> ?", parent.ID).Count(&commits).Error; err != nil {+ t.Fatalf("count commits: %v", err)+ }+ if commits != 0 {+ t.Fatalf("expected rejected commits to roll back, found %d extra commit rows", commits)+ }+}++// The same file ID may exist in another folder; only membership in this+// folder's subtree makes a delete invalid.+func TestCreateCommit_AllowsDeleteOfFileOutsideFolder(t *testing.T) {+ db := newCommitDeleteTestDB(t)+ tree := `{"f3":{"hash":"h3","location":".objects/h3","name":"c.txt","size":1,"status":"1","parent_id":"folder-1"}}`+ parent := &entity.FileCommit{ID: "c1", FolderID: "folder-1", Message: "init", AuthorID: "u1", TreeState: &tree}+ if err := db.Create(parent).Error; err != nil {+ t.Fatalf("seed parent commit: %v", err)+ }+ seedLiveFile(t, db, "f3", "other-folder")++ if _, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove f3", []entity.FileChange{+ {FileID: "f3", FileName: "c.txt", Operation: "delete"},+ }); err != nil {+ t.Fatalf("delete of a file outside the folder: %v", err)+ }+}