aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-11-13 04:18:50 +0800
committerGitHub <noreply@github.com>2022-11-12 21:18:50 +0100
commit34283a74e85278fed2c9b70d6f8749dc6a4001ca (patch)
treef8fa43399cb723d55efc34dacf7947faf0ed5acf /services
parenta0a425a13ba587829a831aaecd8469d39d372111 (diff)
downloadgitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.tar.gz
gitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.zip
Allow detect whether it's in a database transaction for a context.Context (#21756)
Fix #19513 This PR introduce a new db method `InTransaction(context.Context)`, and also builtin check on `db.TxContext` and `db.WithTx`. There is also a new method `db.AutoTx` has been introduced but could be used by other PRs. `WithTx` will always open a new transaction, if a transaction exist in context, return an error. `AutoTx` will try to open a new transaction if no transaction exist in context. That means it will always enter a transaction if there is no error. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'services')
-rw-r--r--services/asymkey/deploy_key.go2
-rw-r--r--services/asymkey/ssh_key.go2
-rw-r--r--services/attachment/attachment.go2
-rw-r--r--services/automerge/automerge.go8
-rw-r--r--services/comments/comments.go2
-rw-r--r--services/issue/issue.go2
-rw-r--r--services/issue/label.go2
-rw-r--r--services/issue/milestone.go2
-rw-r--r--services/org/org.go2
-rw-r--r--services/org/repo.go2
-rw-r--r--services/packages/packages.go8
-rw-r--r--services/pull/check.go4
-rw-r--r--services/pull/merge.go2
-rw-r--r--services/release/release.go2
-rw-r--r--services/repository/adopt.go2
-rw-r--r--services/repository/archiver/archiver.go2
-rw-r--r--services/repository/avatar.go4
-rw-r--r--services/repository/fork.go4
-rw-r--r--services/repository/push.go2
-rw-r--r--services/repository/repository.go2
-rw-r--r--services/repository/template.go2
-rw-r--r--services/user/user.go4
22 files changed, 32 insertions, 32 deletions
diff --git a/services/asymkey/deploy_key.go b/services/asymkey/deploy_key.go
index aa0925ab13..4feeadd952 100644
--- a/services/asymkey/deploy_key.go
+++ b/services/asymkey/deploy_key.go
@@ -13,7 +13,7 @@ import (
// DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
func DeleteDeployKey(doer *user_model.User, id int64) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/asymkey/ssh_key.go b/services/asymkey/ssh_key.go
index 143c807a10..06b20001fd 100644
--- a/services/asymkey/ssh_key.go
+++ b/services/asymkey/ssh_key.go
@@ -26,7 +26,7 @@ func DeletePublicKey(doer *user_model.User, id int64) (err error) {
}
}
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go
index 557cc808a3..cc329f568b 100644
--- a/services/attachment/attachment.go
+++ b/services/attachment/attachment.go
@@ -25,7 +25,7 @@ func NewAttachment(attach *repo_model.Attachment, file io.Reader) (*repo_model.A
return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name)
}
- err := db.WithTx(func(ctx context.Context) error {
+ err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
attach.UUID = uuid.New().String()
size, err := storage.Attachments.Save(attach.RelativePath(), file, -1)
if err != nil {
diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go
index 3ee8af2344..5c38367e3b 100644
--- a/services/automerge/automerge.go
+++ b/services/automerge/automerge.go
@@ -63,7 +63,7 @@ func addToQueue(pr *issues_model.PullRequest, sha string) {
// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
- err = db.WithTx(func(ctx context.Context) error {
+ err = db.WithTx(ctx, func(ctx context.Context) error {
lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull)
if err != nil {
return err
@@ -81,20 +81,20 @@ func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_
_, err = issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRScheduledToAutoMerge, pull, doer)
return err
- }, ctx)
+ })
return scheduled, err
}
// RemoveScheduledAutoMerge cancels a previously scheduled pull request
func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest) error {
- return db.WithTx(func(ctx context.Context) error {
+ return db.WithTx(ctx, func(ctx context.Context) error {
if err := pull_model.DeleteScheduledAutoMerge(ctx, pull.ID); err != nil {
return err
}
_, err := issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRUnScheduledToAutoMerge, pull, doer)
return err
- }, ctx)
+ })
}
// MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded
diff --git a/services/comments/comments.go b/services/comments/comments.go
index c40631359b..7167219c20 100644
--- a/services/comments/comments.go
+++ b/services/comments/comments.go
@@ -72,7 +72,7 @@ func UpdateComment(c *issues_model.Comment, doer *user_model.User, oldContent st
// DeleteComment deletes the comment
func DeleteComment(doer *user_model.User, comment *issues_model.Comment) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/issue/issue.go b/services/issue/issue.go
index 47782e50d3..8342d7f5d2 100644
--- a/services/issue/issue.go
+++ b/services/issue/issue.go
@@ -209,7 +209,7 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i
// deleteIssue deletes the issue
func deleteIssue(issue *issues_model.Issue) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/issue/label.go b/services/issue/label.go
index bc5f9b910e..a9a0c20b01 100644
--- a/services/issue/label.go
+++ b/services/issue/label.go
@@ -45,7 +45,7 @@ func AddLabels(issue *issues_model.Issue, doer *user_model.User, labels []*issue
// RemoveLabel removes a label from issue by given ID.
func RemoveLabel(issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/issue/milestone.go b/services/issue/milestone.go
index 756a8625de..965b07556d 100644
--- a/services/issue/milestone.go
+++ b/services/issue/milestone.go
@@ -65,7 +65,7 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *is
// ChangeMilestoneAssign changes assignment of milestone for issue.
func ChangeMilestoneAssign(issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/org/org.go b/services/org/org.go
index 39845610d2..d993f82bbf 100644
--- a/services/org/org.go
+++ b/services/org/org.go
@@ -19,7 +19,7 @@ import (
// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(org *organization.Organization) error {
- ctx, commiter, err := db.TxContext()
+ ctx, commiter, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/org/repo.go b/services/org/repo.go
index 769419d45b..88520c819f 100644
--- a/services/org/repo.go
+++ b/services/org/repo.go
@@ -22,7 +22,7 @@ func TeamAddRepository(t *organization.Team, repo *repo_model.Repository) (err e
return nil
}
- return db.WithTx(func(ctx context.Context) error {
+ return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
return models.AddRepository(ctx, t, repo)
})
}
diff --git a/services/packages/packages.go b/services/packages/packages.go
index 443976e174..4513215ae1 100644
--- a/services/packages/packages.go
+++ b/services/packages/packages.go
@@ -76,7 +76,7 @@ func CreatePackageOrAddFileToExisting(pvci *PackageCreationInfo, pfci *PackageFi
}
func createPackageAndAddFile(pvci *PackageCreationInfo, pfci *PackageFileCreationInfo, allowDuplicate bool) (*packages_model.PackageVersion, *packages_model.PackageFile, error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return nil, nil, err
}
@@ -190,7 +190,7 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all
// AddFileToExistingPackage adds a file to an existing package. If the package does not exist, ErrPackageNotExist is returned
func AddFileToExistingPackage(pvi *PackageInfo, pfci *PackageFileCreationInfo) (*packages_model.PackageVersion, *packages_model.PackageFile, error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return nil, nil, err
}
@@ -388,7 +388,7 @@ func RemovePackageVersionByNameAndVersion(doer *user_model.User, pvi *PackageInf
// RemovePackageVersion deletes the package version and all associated files
func RemovePackageVersion(doer *user_model.User, pv *packages_model.PackageVersion) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
@@ -444,7 +444,7 @@ func DeletePackageFile(ctx context.Context, pf *packages_model.PackageFile) erro
// Cleanup removes expired package data
func Cleanup(unused context.Context, olderThan time.Duration) error {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/pull/check.go b/services/pull/check.go
index 830ff640b5..6e91c22f3e 100644
--- a/services/pull/check.go
+++ b/services/pull/check.go
@@ -63,7 +63,7 @@ func AddToTaskQueue(pr *issues_model.PullRequest) {
// CheckPullMergable check if the pull mergable based on all conditions (branch protection, merge options, ...)
func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *issues_model.PullRequest, manuallMerge, force bool) error {
- return db.WithTx(func(ctx context.Context) error {
+ return db.WithTx(stdCtx, func(ctx context.Context) error {
if pr.HasMerged {
return ErrHasMerged
}
@@ -122,7 +122,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce
}
return nil
- }, stdCtx)
+ })
}
// isSignedIfRequired check if merge will be signed if required
diff --git a/services/pull/merge.go b/services/pull/merge.go
index 56ee9c9a73..6f37a887db 100644
--- a/services/pull/merge.go
+++ b/services/pull/merge.go
@@ -828,7 +828,7 @@ func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGit
pullWorkingPool.CheckIn(fmt.Sprint(pr.ID))
defer pullWorkingPool.CheckOut(fmt.Sprint(pr.ID))
- if err := db.WithTx(func(ctx context.Context) error {
+ if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
prUnit, err := pr.BaseRepo.GetUnitCtx(ctx, unit.TypePullRequests)
if err != nil {
return err
diff --git a/services/release/release.go b/services/release/release.go
index 8ccc222fb2..cf398debbb 100644
--- a/services/release/release.go
+++ b/services/release/release.go
@@ -196,7 +196,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *repo_mod
}
rel.LowerTagName = strings.ToLower(rel.TagName)
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/repository/adopt.go b/services/repository/adopt.go
index a9a0639548..57a21f500c 100644
--- a/services/repository/adopt.go
+++ b/services/repository/adopt.go
@@ -54,7 +54,7 @@ func AdoptRepository(doer, u *user_model.User, opts repo_module.CreateRepoOption
IsEmpty: !opts.AutoInit,
}
- if err := db.WithTx(func(ctx context.Context) error {
+ if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
repoPath := repo_model.RepoPath(u.Name, repo.Name)
isExist, err := util.IsExist(repoPath)
if err != nil {
diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go
index 79537ea051..f1c2693aeb 100644
--- a/services/repository/archiver/archiver.go
+++ b/services/repository/archiver/archiver.go
@@ -174,7 +174,7 @@ func (aReq *ArchiveRequest) Await(ctx context.Context) (*repo_model.RepoArchiver
}
func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
- txCtx, committer, err := db.TxContext()
+ txCtx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return nil, err
}
diff --git a/services/repository/avatar.go b/services/repository/avatar.go
index 1cf9e869c0..e4c8da3ba2 100644
--- a/services/repository/avatar.go
+++ b/services/repository/avatar.go
@@ -33,7 +33,7 @@ func UploadAvatar(repo *repo_model.Repository, data []byte) error {
return nil
}
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
@@ -76,7 +76,7 @@ func DeleteAvatar(repo *repo_model.Repository) error {
avatarPath := repo.CustomAvatarRelativePath()
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/repository/fork.go b/services/repository/fork.go
index 136d7ccab6..e597bfa449 100644
--- a/services/repository/fork.go
+++ b/services/repository/fork.go
@@ -112,7 +112,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
panic(panicErr)
}()
- err = db.WithTx(func(txCtx context.Context) error {
+ err = db.WithTx(ctx, func(txCtx context.Context) error {
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil {
return err
}
@@ -184,7 +184,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
func ConvertForkToNormalRepository(repo *repo_model.Repository) error {
- err := db.WithTx(func(ctx context.Context) error {
+ err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
repo, err := repo_model.GetRepositoryByIDCtx(ctx, repo.ID)
if err != nil {
return err
diff --git a/services/repository/push.go b/services/repository/push.go
index 3a7205d18b..6776ff9f74 100644
--- a/services/repository/push.go
+++ b/services/repository/push.go
@@ -290,7 +290,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
// PushUpdateAddDeleteTags updates a number of added and delete tags
func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
- return db.WithTx(func(ctx context.Context) error {
+ return db.WithTx(db.DefaultContext, func(ctx context.Context) error {
if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
return err
}
diff --git a/services/repository/repository.go b/services/repository/repository.go
index 763ef74927..2f2c27ff20 100644
--- a/services/repository/repository.go
+++ b/services/repository/repository.go
@@ -90,7 +90,7 @@ func Init() error {
// UpdateRepository updates a repository
func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) {
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
diff --git a/services/repository/template.go b/services/repository/template.go
index b73abdce58..bb5e3f4668 100644
--- a/services/repository/template.go
+++ b/services/repository/template.go
@@ -48,7 +48,7 @@ func GenerateRepository(doer, owner *user_model.User, templateRepo *repo_model.R
}
var generateRepo *repo_model.Repository
- if err = db.WithTx(func(ctx context.Context) error {
+ if err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts)
if err != nil {
return err
diff --git a/services/user/user.go b/services/user/user.go
index c8b497a5c4..4186efe682 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -132,7 +132,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error {
}
}
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
@@ -235,7 +235,7 @@ func UploadAvatar(u *user_model.User, data []byte) error {
return err
}
- ctx, committer, err := db.TxContext()
+ ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}