Rename the function for post database transaction

This commit is contained in:
Lunny Xiao 2025-07-17 21:05:38 -07:00
parent cdb6147f38
commit 85e6668c25
8 changed files with 51 additions and 50 deletions

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -136,7 +136,7 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion
// deleteComment deletes the comment // deleteComment deletes the comment
func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAttachments bool) (*issues_model.Comment, func(), error) { 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) { deletedReviewComment, err := db.WithTx2(ctx, func(ctx context.Context) (*issues_model.Comment, error) {
if removeAttachments { if removeAttachments {
// load attachments before deleting the comment // load attachments before deleting the comment

View File

@ -191,16 +191,16 @@ func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Reposi
} }
// delete entries in database // delete entries in database
cleanup, err := deleteIssue(ctx, issue, true) postTxActions, err := deleteIssue(ctx, issue, true)
if err != nil { if err != nil {
return err return err
} }
cleanup() postTxActions()
// delete pull request related git data // delete pull request related git data
if issue.IsPull && gitRepo != nil { if issue.IsPull && gitRepo != nil {
if err := gitRepo.RemoveReference(issue.PullRequest.GetGitHeadRefName()); err != 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 // deleteIssue deletes the issue
func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) (util.CleanUpFunc, error) { func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) (util.PostTxAction, error) {
cleanup := util.NewCleanUpFunc() postTxActions := util.NewPostTxAction()
if err := db.WithTx(ctx, func(ctx context.Context) error { if err := db.WithTx(ctx, func(ctx context.Context) error {
if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil { if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
return err return err
@ -316,11 +316,11 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen
} }
for _, comment := range issue.Comments { for _, comment := range issue.Comments {
_, cleanupDeleteComment, err := deleteComment(ctx, comment, deleteAttachments) _, postTxActionsDeleteComment, err := deleteComment(ctx, comment, deleteAttachments)
if err != nil { if err != nil {
return fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err) return fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err)
} }
cleanup = cleanup.Append(cleanupDeleteComment) postTxActions = postTxActions.Append(postTxActionsDeleteComment)
} }
if deleteAttachments { if deleteAttachments {
@ -330,7 +330,7 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen
return err return err
} }
// the storage cleanup function to remove attachments could be called after all transactions are committed // 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. // Remove issue attachment files.
for i := range issue.Attachments { for i := range issue.Attachments {
system_model.RemoveStorageWithNotice(ctx, storage.Attachments, "Delete issue attachment", issue.Attachments[i].RelativePath()) 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 { }); err != nil {
return nil, err return nil, err
} }
return cleanup, nil return postTxActions, nil
} }
// DeleteOrphanedIssues delete issues without a repo // DeleteOrphanedIssues delete issues without a repo
func DeleteOrphanedIssues(ctx context.Context) error { func DeleteOrphanedIssues(ctx context.Context) error {
cleanup := util.NewCleanUpFunc() postTxActions := util.NewPostTxAction()
if err := db.WithTx(ctx, func(ctx context.Context) error { if err := db.WithTx(ctx, func(ctx context.Context) error {
repoIDs, err := issues_model.GetOrphanedIssueRepoIDs(ctx) repoIDs, err := issues_model.GetOrphanedIssueRepoIDs(ctx)
if err != nil { if err != nil {
return err return err
} }
for i := range repoIDs { for i := range repoIDs {
deleteIssuesCleanup, err := DeleteIssuesByRepoID(ctx, repoIDs[i], true) postTxActionsDeleteIssues, err := DeleteIssuesByRepoID(ctx, repoIDs[i], true)
if err != nil { if err != nil {
return err return err
} }
cleanup = cleanup.Append(deleteIssuesCleanup) postTxActions = postTxActions.Append(postTxActionsDeleteIssues)
} }
return nil return nil
}); err != nil { }); err != nil {
return err return err
} }
cleanup() postTxActions()
return nil return nil
} }
// DeleteIssuesByRepoID deletes issues by repositories id // DeleteIssuesByRepoID deletes issues by repositories id
func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) (util.CleanUpFunc, error) { func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) (util.PostTxAction, error) {
cleanup := util.NewCleanUpFunc() postTxActions := util.NewPostTxAction()
for { for {
issues := make([]*issues_model.Issue, 0, db.DefaultMaxInSize) issues := make([]*issues_model.Issue, 0, db.DefaultMaxInSize)
if err := db.GetEngine(ctx). if err := db.GetEngine(ctx).
@ -385,13 +385,13 @@ func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments b
} }
for _, issue := range issues { for _, issue := range issues {
deleteIssueCleanUp, err := deleteIssue(ctx, issue, deleteAttachments) postTxActionsDeleteIssue, err := deleteIssue(ctx, issue, deleteAttachments)
if err != nil { if err != nil {
return nil, fmt.Errorf("deleteIssue [issue_id: %d]: %w", issue.ID, err) 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
} }

View File

@ -44,9 +44,9 @@ func TestIssue_DeleteIssue(t *testing.T) {
ID: issueIDs[2], ID: issueIDs[2],
} }
cleanup, err := deleteIssue(db.DefaultContext, issue, true) postTxActions, err := deleteIssue(db.DefaultContext, issue, true)
assert.NoError(t, err) assert.NoError(t, err)
cleanup() postTxActions()
issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1) issueIDs, err = issues_model.GetIssueIDsByRepoID(db.DefaultContext, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, issueIDs, 4) assert.Len(t, issueIDs, 4)
@ -56,9 +56,9 @@ func TestIssue_DeleteIssue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
issue, err = issues_model.GetIssueByID(db.DefaultContext, 4) issue, err = issues_model.GetIssueByID(db.DefaultContext, 4)
assert.NoError(t, err) assert.NoError(t, err)
cleanup, err = deleteIssue(db.DefaultContext, issue, true) postTxActions, err = deleteIssue(db.DefaultContext, issue, true)
assert.NoError(t, err) assert.NoError(t, err)
cleanup() postTxActions()
assert.Len(t, attachments, 2) assert.Len(t, attachments, 2)
for i := range attachments { for i := range attachments {
attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attachments[i].UUID) 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.NoError(t, err)
assert.False(t, left) assert.False(t, left)
cleanup, err = deleteIssue(db.DefaultContext, issue2, true) postTxActions, err = deleteIssue(db.DefaultContext, issue2, true)
assert.NoError(t, err) assert.NoError(t, err)
cleanup() postTxActions()
left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1) left, err = issues_model.IssueNoDependenciesLeft(db.DefaultContext, issue1)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, left) assert.True(t, left)

View File

@ -201,7 +201,7 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
// Delete Issues and related objects // Delete Issues and related objects
// attachments will be deleted later with repo_id // 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 { if err != nil {
return err return err
} }
@ -298,7 +298,7 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
committer.Close() committer.Close()
cleanup() postTxActions()
if needRewriteKeysFile { if needRewriteKeysFile {
if err := asymkey_service.RewriteAllPublicKeys(ctx); err != nil { if err := asymkey_service.RewriteAllPublicKeys(ctx); err != nil {

View File

@ -33,8 +33,8 @@ import (
) )
// deleteUser deletes models associated to an user. // deleteUser deletes models associated to an user.
func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup util.CleanUpFunc, err error) { func deleteUser(ctx context.Context, u *user_model.User, purge bool) (postTxActions util.PostTxAction, err error) {
cleanup = util.NewCleanUpFunc() postTxActions = util.NewPostTxAction()
// ***** START: Watch ***** // ***** START: Watch *****
watchedRepoIDs, err := db.FindIDs(ctx, "watch", "watch.repo_id", 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 return nil, err
} }
cleanup = cleanup.Append(func() { postTxActions = postTxActions.Append(func() {
for _, a := range comment.Attachments { for _, a := range comment.Attachments {
if err := storage.Attachments.Delete(a.RelativePath()); err != nil { if err := storage.Attachments.Delete(a.RelativePath()); err != nil {
if !errors.Is(err, os.ErrNotExist) { 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 nil, fmt.Errorf("delete: %w", err)
} }
return cleanup, nil return postTxActions, nil
} }

View File

@ -243,7 +243,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
return packages_model.ErrUserOwnPackages{UID: u.ID} return packages_model.ErrUserOwnPackages{UID: u.ID}
} }
cleanup, err := deleteUser(ctx, u, purge) postTxActions, err := deleteUser(ctx, u, purge)
if err != nil { if err != nil {
return fmt.Errorf("DeleteUser: %w", err) return fmt.Errorf("DeleteUser: %w", err)
} }
@ -253,7 +253,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
} }
_ = committer.Close() _ = committer.Close()
cleanup() postTxActions()
if err = asymkey_service.RewriteAllPublicKeys(ctx); err != nil { if err = asymkey_service.RewriteAllPublicKeys(ctx); err != nil {
return err return err