diff --git a/models/issues/pull_list_test.go b/models/issues/pull_list_test.go index feb59df2160..eb2de006d60 100644 --- a/models/issues/pull_list_test.go +++ b/models/issues/pull_list_test.go @@ -39,8 +39,9 @@ func TestPullRequestList_LoadReviewCommentsCounts(t *testing.T) { reviewComments, err := prs.LoadReviewCommentsCounts(db.DefaultContext) assert.NoError(t, err) assert.Len(t, reviewComments, 2) - assert.Equal(t, 1, reviewComments[prs[0].IssueID]) - assert.Equal(t, 2, reviewComments[prs[1].IssueID]) + for _, pr := range prs { + assert.Equal(t, 1, reviewComments[pr.IssueID]) + } } func TestPullRequestList_LoadReviews(t *testing.T) { diff --git a/models/migrations/v1_25/main_test.go b/models/migrations/v1_25/main_test.go index 8ac213c908f..d2c4a4105d3 100644 --- a/models/migrations/v1_25/main_test.go +++ b/models/migrations/v1_25/main_test.go @@ -1,4 +1,4 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. +// Copyright 2025 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package v1_25 diff --git a/models/migrations/v1_25/v321_test.go b/models/migrations/v1_25/v321_test.go index 0febbe861fa..47bb9ea2429 100644 --- a/models/migrations/v1_25/v321_test.go +++ b/models/migrations/v1_25/v321_test.go @@ -14,18 +14,17 @@ import ( func Test_AddFileStatusToAttachment(t *testing.T) { type Attachment struct { - ID int64 `xorm:"pk autoincr"` - UUID string `xorm:"uuid UNIQUE"` - RepoID int64 `xorm:"INDEX"` // this should not be zero - IssueID int64 `xorm:"INDEX"` // maybe zero when creating - ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating - UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added - CommentID int64 `xorm:"INDEX"` - Name string - DownloadCount int64 `xorm:"DEFAULT 0"` - Size int64 `xorm:"DEFAULT 0"` - CreatedUnix timeutil.TimeStamp `xorm:"created"` - CustomDownloadURL string `xorm:"-"` + ID int64 `xorm:"pk autoincr"` + UUID string `xorm:"uuid UNIQUE"` + RepoID int64 `xorm:"INDEX"` // this should not be zero + IssueID int64 `xorm:"INDEX"` // maybe zero when creating + ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating + UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added + CommentID int64 `xorm:"INDEX"` + Name string + DownloadCount int64 `xorm:"DEFAULT 0"` + Size int64 `xorm:"DEFAULT 0"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` } // Prepare and load the testing database diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index 840ad2fd911..6f57c55c4e6 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -151,24 +151,31 @@ func cleanAttachments(ctx context.Context, attachmentIDs []int64) []int64 { // ScanToBeDeletedAttachments scans for attachments that are marked as to be deleted and send to // clean queue func ScanToBeDeletedAttachments(ctx context.Context) error { - attachments := make([]*repo_model.Attachment, 0, 10) + attachmentIDs := make([]int64, 0, 100) lastID := int64(0) for { if err := db.GetEngine(ctx). + Select("id"). // use the status and id index to speed up the query Where("status = ? AND id > ?", db.FileStatusToBeDeleted, lastID). Asc("id"). Limit(100). - Find(&attachments); err != nil { + Find(&attachmentIDs); err != nil { return fmt.Errorf("scan to-be-deleted attachments: %w", err) } - if len(attachments) == 0 { + if len(attachmentIDs) == 0 { log.Trace("No more attachments to be deleted") break } - AddAttachmentsToCleanQueue(ctx, attachments) - lastID = attachments[len(attachments)-1].ID + for _, id := range attachmentIDs { + if err := cleanQueue.Push(id); err != nil { + log.Error("Failed to push attachment ID %d to clean queue: %v", id, err) + } + } + + lastID = attachmentIDs[len(attachmentIDs)-1] + attachmentIDs = attachmentIDs[0:0] } return nil diff --git a/services/issue/issue.go b/services/issue/issue.go index 967e0376749..9b3b0c66b21 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -318,7 +318,9 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen if err := deleteComment(ctx, comment, deleteAttachments); err != nil { return nil, fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err) } - toBeCleanedAttachments = append(toBeCleanedAttachments, comment.Attachments...) + if deleteAttachments { + toBeCleanedAttachments = append(toBeCleanedAttachments, comment.Attachments...) + } } if deleteAttachments {