From 85e6668c25609451fcb8d50470b07edfad33efca Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 17 Jul 2025 21:05:38 -0700 Subject: [PATCH] Rename the function for post database transaction --- modules/util/cleanup.go | 17 ---------------- modules/util/post_tx_action.go | 18 +++++++++++++++++ services/issue/comments.go | 2 +- services/issue/issue.go | 36 +++++++++++++++++----------------- services/issue/issue_test.go | 12 ++++++------ services/repository/delete.go | 4 ++-- services/user/delete.go | 8 ++++---- services/user/user.go | 4 ++-- 8 files changed, 51 insertions(+), 50 deletions(-) delete mode 100644 modules/util/cleanup.go create mode 100644 modules/util/post_tx_action.go diff --git a/modules/util/cleanup.go b/modules/util/cleanup.go deleted file mode 100644 index 687dac27b5f..00000000000 --- a/modules/util/cleanup.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2025 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package util - -type CleanUpFunc func() - -func NewCleanUpFunc() CleanUpFunc { - return func() {} -} - -func (f CleanUpFunc) Append(newF CleanUpFunc) CleanUpFunc { - return func() { - f() - newF() - } -} diff --git a/modules/util/post_tx_action.go b/modules/util/post_tx_action.go new file mode 100644 index 00000000000..b5b0e636173 --- /dev/null +++ b/modules/util/post_tx_action.go @@ -0,0 +1,18 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package util + +// PostTxAction is a function that is executed after a database transaction +type PostTxAction func() + +func NewPostTxAction() PostTxAction { + return func() {} +} + +func (f PostTxAction) Append(appendF PostTxAction) PostTxAction { + return func() { + f() + appendF() + } +} diff --git a/services/issue/comments.go b/services/issue/comments.go index 273af900ffc..e92330ceba7 100644 --- a/services/issue/comments.go +++ b/services/issue/comments.go @@ -136,7 +136,7 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion // deleteComment deletes the comment func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAttachments bool) (*issues_model.Comment, func(), error) { - storageCleanup := util.NewCleanUpFunc() + storageCleanup := util.NewPostTxAction() deletedReviewComment, err := db.WithTx2(ctx, func(ctx context.Context) (*issues_model.Comment, error) { if removeAttachments { // load attachments before deleting the comment diff --git a/services/issue/issue.go b/services/issue/issue.go index 80ac9c1d6ae..04b3e3f1793 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -191,16 +191,16 @@ func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Reposi } // delete entries in database - cleanup, err := deleteIssue(ctx, issue, true) + postTxActions, err := deleteIssue(ctx, issue, true) if err != nil { return err } - cleanup() + postTxActions() // delete pull request related git data if issue.IsPull && gitRepo != nil { if err := gitRepo.RemoveReference(issue.PullRequest.GetGitHeadRefName()); err != nil { - return err + log.Error("DeleteIssue: RemoveReference %s: %v", issue.PullRequest.GetGitHeadRefName(), err) } } @@ -259,8 +259,8 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i } // deleteIssue deletes the issue -func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) (util.CleanUpFunc, error) { - cleanup := util.NewCleanUpFunc() +func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) (util.PostTxAction, error) { + postTxActions := util.NewPostTxAction() if err := db.WithTx(ctx, func(ctx context.Context) error { if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil { return err @@ -316,11 +316,11 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen } for _, comment := range issue.Comments { - _, cleanupDeleteComment, err := deleteComment(ctx, comment, deleteAttachments) + _, postTxActionsDeleteComment, err := deleteComment(ctx, comment, deleteAttachments) if err != nil { return fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err) } - cleanup = cleanup.Append(cleanupDeleteComment) + postTxActions = postTxActions.Append(postTxActionsDeleteComment) } if deleteAttachments { @@ -330,7 +330,7 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen return err } // the storage cleanup function to remove attachments could be called after all transactions are committed - cleanup = cleanup.Append(func() { + postTxActions = postTxActions.Append(func() { // Remove issue attachment files. for i := range issue.Attachments { system_model.RemoveStorageWithNotice(ctx, storage.Attachments, "Delete issue attachment", issue.Attachments[i].RelativePath()) @@ -341,35 +341,35 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen }); err != nil { return nil, err } - return cleanup, nil + return postTxActions, nil } // DeleteOrphanedIssues delete issues without a repo func DeleteOrphanedIssues(ctx context.Context) error { - cleanup := util.NewCleanUpFunc() + postTxActions := util.NewPostTxAction() if err := db.WithTx(ctx, func(ctx context.Context) error { repoIDs, err := issues_model.GetOrphanedIssueRepoIDs(ctx) if err != nil { return err } for i := range repoIDs { - deleteIssuesCleanup, err := DeleteIssuesByRepoID(ctx, repoIDs[i], true) + postTxActionsDeleteIssues, err := DeleteIssuesByRepoID(ctx, repoIDs[i], true) if err != nil { return err } - cleanup = cleanup.Append(deleteIssuesCleanup) + postTxActions = postTxActions.Append(postTxActionsDeleteIssues) } return nil }); err != nil { return err } - cleanup() + postTxActions() return nil } // DeleteIssuesByRepoID deletes issues by repositories id -func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) (util.CleanUpFunc, error) { - cleanup := util.NewCleanUpFunc() +func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) (util.PostTxAction, error) { + postTxActions := util.NewPostTxAction() for { issues := make([]*issues_model.Issue, 0, db.DefaultMaxInSize) if err := db.GetEngine(ctx). @@ -385,13 +385,13 @@ func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments b } for _, issue := range issues { - deleteIssueCleanUp, err := deleteIssue(ctx, issue, deleteAttachments) + postTxActionsDeleteIssue, err := deleteIssue(ctx, issue, deleteAttachments) if err != nil { return nil, fmt.Errorf("deleteIssue [issue_id: %d]: %w", issue.ID, err) } - cleanup = cleanup.Append(deleteIssueCleanUp) + postTxActions = postTxActions.Append(postTxActionsDeleteIssue) } } - return cleanup, nil + return postTxActions, nil } diff --git a/services/issue/issue_test.go b/services/issue/issue_test.go index f8cb85db3f3..27a10ce9de4 100644 --- a/services/issue/issue_test.go +++ b/services/issue/issue_test.go @@ -44,9 +44,9 @@ func TestIssue_DeleteIssue(t *testing.T) { ID: issueIDs[2], } - cleanup, err := deleteIssue(db.DefaultContext, issue, true) + postTxActions, err := deleteIssue(db.DefaultContext, issue, true) assert.NoError(t, err) - cleanup() + postTxActions() issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1) assert.NoError(t, err) assert.Len(t, issueIDs, 4) @@ -56,9 +56,9 @@ func TestIssue_DeleteIssue(t *testing.T) { assert.NoError(t, err) issue, err = issues_model.GetIssueByID(db.DefaultContext, 4) assert.NoError(t, err) - cleanup, err = deleteIssue(db.DefaultContext, issue, true) + postTxActions, err = deleteIssue(db.DefaultContext, issue, true) assert.NoError(t, err) - cleanup() + postTxActions() assert.Len(t, attachments, 2) for i := range attachments { attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attachments[i].UUID) @@ -80,9 +80,9 @@ func TestIssue_DeleteIssue(t *testing.T) { assert.NoError(t, err) assert.False(t, left) - cleanup, err = deleteIssue(db.DefaultContext, issue2, true) + postTxActions, err = deleteIssue(db.DefaultContext, issue2, true) assert.NoError(t, err) - cleanup() + postTxActions() left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1) assert.NoError(t, err) assert.True(t, left) diff --git a/services/repository/delete.go b/services/repository/delete.go index 0527546d1dc..f6bb58222dc 100644 --- a/services/repository/delete.go +++ b/services/repository/delete.go @@ -201,7 +201,7 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams // Delete Issues and related objects // attachments will be deleted later with repo_id - cleanup, err := issue_service.DeleteIssuesByRepoID(ctx, repoID, false) + postTxActions, err := issue_service.DeleteIssuesByRepoID(ctx, repoID, false) if err != nil { return err } @@ -298,7 +298,7 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams committer.Close() - cleanup() + postTxActions() if needRewriteKeysFile { if err := asymkey_service.RewriteAllPublicKeys(ctx); err != nil { diff --git a/services/user/delete.go b/services/user/delete.go index 63874f392c3..b88d1fa54b7 100644 --- a/services/user/delete.go +++ b/services/user/delete.go @@ -33,8 +33,8 @@ import ( ) // deleteUser deletes models associated to an user. -func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup util.CleanUpFunc, err error) { - cleanup = util.NewCleanUpFunc() +func deleteUser(ctx context.Context, u *user_model.User, purge bool) (postTxActions util.PostTxAction, err error) { + postTxActions = util.NewPostTxAction() // ***** START: Watch ***** watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id", @@ -135,7 +135,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup ut return nil, err } - cleanup = cleanup.Append(func() { + postTxActions = postTxActions.Append(func() { for _, a := range comment.Attachments { if err := storage.Attachments.Delete(a.RelativePath()); err != nil { if !errors.Is(err, os.ErrNotExist) { @@ -227,5 +227,5 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup ut return nil, fmt.Errorf("delete: %w", err) } - return cleanup, nil + return postTxActions, nil } diff --git a/services/user/user.go b/services/user/user.go index d92763d2d7a..26d7b11cdcc 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -243,7 +243,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { return packages_model.ErrUserOwnPackages{UID: u.ID} } - cleanup, err := deleteUser(ctx, u, purge) + postTxActions, err := deleteUser(ctx, u, purge) if err != nil { return fmt.Errorf("DeleteUser: %w", err) } @@ -253,7 +253,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { } _ = committer.Close() - cleanup() + postTxActions() if err = asymkey_service.RewriteAllPublicKeys(ctx); err != nil { return err