diff --git a/models/activities/action.go b/models/activities/action.go index cad3263c2d790..5c3419c5ecf90 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -572,7 +572,7 @@ func NotifyWatchers(actions ...*Action) error { // NotifyWatchersActions creates batch of actions for every watcher. func NotifyWatchersActions(acts []*Action) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/activities/notification.go b/models/activities/notification.go index 5748b807a0687..28adc8cc4efa1 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -142,7 +142,7 @@ func CountNotifications(opts *FindNotificationOptions) (int64, error) { // CreateRepoTransferNotification creates notification for the user a repository was transferred to func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -185,7 +185,7 @@ func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_ // for each watcher, or updates it if already exists // receiverID > 0 just send to receiver, else send to all watcher func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/asymkey/gpg_key.go b/models/asymkey/gpg_key.go index 83774533aa7d0..30f5ecc949646 100644 --- a/models/asymkey/gpg_key.go +++ b/models/asymkey/gpg_key.go @@ -234,7 +234,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) { return ErrGPGKeyAccessDenied{doer.ID, key.ID} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/asymkey/gpg_key_add.go b/models/asymkey/gpg_key_add.go index d01f2deb03151..e7be19073aa83 100644 --- a/models/asymkey/gpg_key_add.go +++ b/models/asymkey/gpg_key_add.go @@ -73,7 +73,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro return nil, err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/gpg_key_verify.go b/models/asymkey/gpg_key_verify.go index 152765cc3aeb7..4e9d04f576071 100644 --- a/models/asymkey/gpg_key_verify.go +++ b/models/asymkey/gpg_key_verify.go @@ -31,7 +31,7 @@ import ( // VerifyGPGKey marks a GPG key as verified func VerifyGPGKey(ownerID int64, keyID, token, signature string) (string, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return "", err } diff --git a/models/asymkey/ssh_key.go b/models/asymkey/ssh_key.go index 7ed4ad6b3f410..811f563c8d2c7 100644 --- a/models/asymkey/ssh_key.go +++ b/models/asymkey/ssh_key.go @@ -100,7 +100,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub return nil, err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -321,7 +321,7 @@ func PublicKeyIsExternallyManaged(id int64) (bool, error) { // deleteKeysMarkedForDeletion returns true if ssh keys needs update func deleteKeysMarkedForDeletion(keys []string) (bool, error) { // Start session - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return false, err } diff --git a/models/asymkey/ssh_key_deploy.go b/models/asymkey/ssh_key_deploy.go index d5c981da47bf1..601e5c32e5f36 100644 --- a/models/asymkey/ssh_key_deploy.go +++ b/models/asymkey/ssh_key_deploy.go @@ -126,7 +126,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey accessMode = perm.AccessModeWrite } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/ssh_key_principals.go b/models/asymkey/ssh_key_principals.go index e0d407af35677..7a769fd84dea3 100644 --- a/models/asymkey/ssh_key_principals.go +++ b/models/asymkey/ssh_key_principals.go @@ -26,7 +26,7 @@ import ( // AddPrincipalKey adds new principal to database and authorized_principals file. func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/ssh_key_verify.go b/models/asymkey/ssh_key_verify.go index 710cb3af00cdb..6931df9e88419 100644 --- a/models/asymkey/ssh_key_verify.go +++ b/models/asymkey/ssh_key_verify.go @@ -15,7 +15,7 @@ import ( // VerifySSHKey marks a SSH key as verified func VerifySSHKey(ownerID int64, fingerprint, token, signature string) (string, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return "", err } diff --git a/models/auth/oauth2.go b/models/auth/oauth2.go index ccd9336f65180..2ff1066288808 100644 --- a/models/auth/oauth2.go +++ b/models/auth/oauth2.go @@ -201,7 +201,7 @@ type UpdateOAuth2ApplicationOptions struct { // UpdateOAuth2Application updates an oauth2 application func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -265,7 +265,7 @@ func deleteOAuth2Application(ctx context.Context, id, userid int64) error { // DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app. func DeleteOAuth2Application(id, userid int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/auth/session.go b/models/auth/session.go index 5b130c64b680b..b5e4712bded67 100644 --- a/models/auth/session.go +++ b/models/auth/session.go @@ -37,7 +37,7 @@ func ReadSession(key string) (*Session, error) { Key: key, } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -73,7 +73,7 @@ func DestroySession(key string) error { // RegenerateSession regenerates a session from the old id func RegenerateSession(oldKey, newKey string) (*Session, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/avatars/avatar.go b/models/avatars/avatar.go index ec3b611312a9a..61f0ac19c311a 100644 --- a/models/avatars/avatar.go +++ b/models/avatars/avatar.go @@ -97,7 +97,7 @@ func saveEmailHash(email string) string { Hash: emailHash, } // OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { has, err := db.GetEngine(ctx).Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash)) if has || err != nil { // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time diff --git a/models/db/context.go b/models/db/context.go index e90780e4e93e9..8c81469ebc10a 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -8,6 +8,7 @@ import ( "context" "database/sql" + "xorm.io/xorm" "xorm.io/xorm/schemas" ) @@ -86,7 +87,11 @@ type Committer interface { } // TxContext represents a transaction Context -func TxContext() (*Context, Committer, error) { +func TxContext(parentCtx context.Context) (*Context, Committer, error) { + if InTransaction(parentCtx) { + return nil, nil, ErrAlreadyInTransaction + } + sess := x.NewSession() if err := sess.Begin(); err != nil { sess.Close() @@ -97,14 +102,24 @@ func TxContext() (*Context, Committer, error) { } // WithTx represents executing database operations on a transaction -// you can optionally change the context to a parent one -func WithTx(f func(ctx context.Context) error, stdCtx ...context.Context) error { - parentCtx := DefaultContext - if len(stdCtx) != 0 && stdCtx[0] != nil { - // TODO: make sure parent context has no open session - parentCtx = stdCtx[0] +// This function will always open a new transaction, if a transaction exist in parentCtx return an error. +func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error { + if InTransaction(parentCtx) { + return ErrAlreadyInTransaction + } + return txWithNoCheck(parentCtx, f) +} + +// AutoTx represents executing database operations on a transaction, if the transaction exist, +// this function will reuse it otherwise will create a new one and close it when finished. +func AutoTx(parentCtx context.Context, f func(ctx context.Context) error) error { + if InTransaction(parentCtx) { + return f(newContext(parentCtx, GetEngine(parentCtx), true)) } + return txWithNoCheck(parentCtx, f) +} +func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -180,3 +195,28 @@ func EstimateCount(ctx context.Context, bean interface{}) (int64, error) { } return rows, err } + +// InTransaction returns true if the engine is in a transaction otherwise return false +func InTransaction(ctx context.Context) bool { + var e Engine + if engined, ok := ctx.(Engined); ok { + e = engined.Engine() + } else { + enginedInterface := ctx.Value(enginedContextKey) + if enginedInterface != nil { + e = enginedInterface.(Engined).Engine() + } + } + if e == nil { + return false + } + + switch t := e.(type) { + case *xorm.Engine: + return false + case *xorm.Session: + return t.IsInTx() + default: + return false + } +} diff --git a/models/db/context_test.go b/models/db/context_test.go new file mode 100644 index 0000000000000..f00e7834ed239 --- /dev/null +++ b/models/db/context_test.go @@ -0,0 +1,33 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db_test + +import ( + "context" + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestInTransaction(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + assert.False(t, db.InTransaction(db.DefaultContext)) + assert.NoError(t, db.WithTx(db.DefaultContext, func(ctx context.Context) error { + assert.True(t, db.InTransaction(ctx)) + return nil + })) + + ctx, committer, err := db.TxContext(db.DefaultContext) + assert.NoError(t, err) + defer committer.Close() + assert.True(t, db.InTransaction(ctx)) + assert.Error(t, db.WithTx(ctx, func(ctx context.Context) error { + assert.True(t, db.InTransaction(ctx)) + return nil + })) +} diff --git a/models/db/error.go b/models/db/error.go index 9577fa55dbb38..85cba5c4e929f 100644 --- a/models/db/error.go +++ b/models/db/error.go @@ -5,11 +5,14 @@ package db import ( + "errors" "fmt" "code.gitea.io/gitea/modules/util" ) +var ErrAlreadyInTransaction = errors.New("database connection has already been in a transaction") + // ErrCancelled represents an error due to context cancellation type ErrCancelled struct { Message string diff --git a/models/db/index_test.go b/models/db/index_test.go index 1ea30e2b60c34..cf777b9d3894c 100644 --- a/models/db/index_test.go +++ b/models/db/index_test.go @@ -59,7 +59,7 @@ func TestSyncMaxResourceIndex(t *testing.T) { assert.EqualValues(t, 62, maxIndex) // commit transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 73) assert.NoError(t, err) maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10) @@ -73,7 +73,7 @@ func TestSyncMaxResourceIndex(t *testing.T) { assert.EqualValues(t, 73, maxIndex) // rollback transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 84) maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10) assert.NoError(t, err) @@ -102,7 +102,7 @@ func TestGetNextResourceIndex(t *testing.T) { assert.EqualValues(t, 2, maxIndex) // commit transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20) assert.NoError(t, err) assert.EqualValues(t, 3, maxIndex) @@ -114,7 +114,7 @@ func TestGetNextResourceIndex(t *testing.T) { assert.EqualValues(t, 3, maxIndex) // rollback transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20) assert.NoError(t, err) assert.EqualValues(t, 4, maxIndex) diff --git a/models/git/branches.go b/models/git/branches.go index b17d762dbe5de..328f6f0f0d00e 100644 --- a/models/git/branches.go +++ b/models/git/branches.go @@ -544,7 +544,7 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist // RenameBranch rename a branch func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/git/branches_test.go b/models/git/branches_test.go index 58c4ad027be10..754c5a4e66dec 100644 --- a/models/git/branches_test.go +++ b/models/git/branches_test.go @@ -102,7 +102,7 @@ func TestRenameBranch(t *testing.T) { repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) _isDefault := false - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) defer committer.Close() assert.NoError(t, err) assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{ diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 620baa036cb15..df6f5ae7ea1bc 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -94,7 +94,7 @@ func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { // getNextCommitStatusIndex return the next index func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { - ctx, commiter, err := db.TxContext() + ctx, commiter, err := db.TxContext(db.DefaultContext) if err != nil { return 0, err } @@ -297,7 +297,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { return fmt.Errorf("generate commit status index failed: %w", err) } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err) } diff --git a/models/git/lfs.go b/models/git/lfs.go index 58042edfdbe1f..74721dabb1b2c 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -137,7 +137,7 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"} func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { var err error - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -185,7 +185,7 @@ func RemoveLFSMetaObjectByOid(repoID int64, oid string) (int64, error) { return 0, ErrLFSObjectNotExist } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return 0, err } @@ -242,7 +242,7 @@ func LFSObjectIsAssociated(oid string) (bool, error) { // LFSAutoAssociate auto associates accessible LFSMetaObjects func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/git/lfs_lock.go b/models/git/lfs_lock.go index 016db899a97f8..c5a8cc245e3c6 100644 --- a/models/git/lfs_lock.go +++ b/models/git/lfs_lock.go @@ -44,7 +44,7 @@ func cleanPath(p string) string { // CreateLFSLock creates a new lock. func CreateLFSLock(repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext() + dbCtx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -137,7 +137,7 @@ func CountLFSLockByRepoID(repoID int64) (int64, error) { // DeleteLFSLockByID deletes a lock by given ID. func DeleteLFSLockByID(id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext() + dbCtx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/issues/assignees.go b/models/issues/assignees.go index d960d5ebafed6..ce497b116dec1 100644 --- a/models/issues/assignees.go +++ b/models/issues/assignees.go @@ -64,7 +64,7 @@ func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.U // ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return false, nil, err } diff --git a/models/issues/comment.go b/models/issues/comment.go index 6877991a9399c..d71c675d23472 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -565,7 +565,7 @@ func (c *Comment) LoadAttachments() error { // UpdateAttachments update attachments by UUIDs for the comment func (c *Comment) UpdateAttachments(uuids []string) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1003,7 +1003,7 @@ type CreateCommentOptions struct { // CreateComment creates comment of issue or commit. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -1135,7 +1135,7 @@ func CountComments(opts *FindCommentsOptions) (int64, error) { // UpdateComment updates information of comment. func UpdateComment(c *Comment, doer *user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/dependency.go b/models/issues/dependency.go index 4754ed0f5f95e..a02280d8a6d1a 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -129,7 +129,7 @@ const ( // CreateIssueDependency creates a new dependency for an issue func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -170,7 +170,7 @@ func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error { // RemoveIssueDependency removes a dependency from an issue func RemoveIssueDependency(user *user_model.User, issue, dep *Issue, depType DependencyType) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue.go b/models/issues/issue.go index ca48f425f2c40..c2f7cb6578006 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -540,7 +540,7 @@ func clearIssueLabels(ctx context.Context, issue *Issue, doer *user_model.User) // ClearIssueLabels removes all issue labels as the given user. // Triggers appropriate WebHooks, if any. func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -588,7 +588,7 @@ func (ts labelSorter) Swap(i, j int) { // ReplaceIssueLabels removes all current labels and add new labels to the issue. // Triggers appropriate WebHooks, if any. func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -760,7 +760,7 @@ func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, // ChangeIssueTitle changes the title of this issue, as the given user. func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -794,7 +794,7 @@ func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err // ChangeIssueRef changes the branch of this issue, as the given user. func ChangeIssueRef(issue *Issue, doer *user_model.User, oldRef string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -844,7 +844,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo * // UpdateIssueAttachments update attachments by UUIDs for the issue func UpdateIssueAttachments(issueID int64, uuids []string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -864,7 +864,7 @@ func UpdateIssueAttachments(issueID int64, uuids []string) (err error) { // ChangeIssueContent changes issue content, as the given user. func ChangeIssueContent(issue *Issue, doer *user_model.User, content string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1069,7 +1069,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue // NewIssue creates new issue with labels for repository. func NewIssue(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1986,7 +1986,7 @@ func SearchIssueIDsByKeyword(ctx context.Context, kw string, repoIDs []int64, li // If the issue status is changed a statusChangeComment is returned // similarly if the title is changed the titleChanged bool is set to true func UpdateIssueByAPI(issue *Issue, doer *user_model.User) (statusChangeComment *Comment, titleChanged bool, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, false, err } @@ -2044,7 +2044,7 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *us if issue.DeadlineUnix == deadlineUnix { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -2436,7 +2436,7 @@ func CountOrphanedIssues() (int64, error) { // DeleteOrphanedIssues delete issues without a repo func DeleteOrphanedIssues() error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_index.go b/models/issues/issue_index.go index f4acc5aa1bc0d..1fa79f494a32b 100644 --- a/models/issues/issue_index.go +++ b/models/issues/issue_index.go @@ -9,7 +9,7 @@ import "code.gitea.io/gitea/models/db" // RecalculateIssueIndexForRepo create issue_index for repo if not exist and // update it based on highest index of existing issues assigned to a repo func RecalculateIssueIndexForRepo(repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_lock.go b/models/issues/issue_lock.go index 7b52429ef79f7..608b5fd140408 100644 --- a/models/issues/issue_lock.go +++ b/models/issues/issue_lock.go @@ -40,7 +40,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error { commentType = CommentTypeUnlock } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_project.go b/models/issues/issue_project.go index 8299087c5b108..0c59f4e82b713 100644 --- a/models/issues/issue_project.go +++ b/models/issues/issue_project.go @@ -110,7 +110,7 @@ func LoadIssuesFromBoardList(bs project_model.BoardList) (map[int64]IssueList, e // ChangeProjectAssign changes the project associated with an issue func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -166,7 +166,7 @@ func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.U // MoveIssueAcrossProjectBoards move a card from one board to another func MoveIssueAcrossProjectBoards(issue *Issue, board *project_model.Board) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_xref_test.go b/models/issues/issue_xref_test.go index 0f72fc7ca6f52..d6d88db894862 100644 --- a/models/issues/issue_xref_test.go +++ b/models/issues/issue_xref_test.go @@ -131,7 +131,7 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo}) d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}) - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) assert.NoError(t, err) defer committer.Close() @@ -174,7 +174,7 @@ func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *i i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue}) c := &issues_model.Comment{Type: issues_model.CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content} - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) assert.NoError(t, err) defer committer.Close() err = db.Insert(ctx, c) diff --git a/models/issues/label.go b/models/issues/label.go index bbdc99e265101..0b0d1419b16b0 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -232,7 +232,7 @@ func NewLabel(ctx context.Context, label *Label) error { // NewLabels creates new labels func NewLabels(labels ...*Label) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -267,7 +267,7 @@ func DeleteLabel(id, labelID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -627,7 +627,7 @@ func NewIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -676,7 +676,7 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us // NewIssueLabels creates a list of issue-label relations. func NewIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/label_test.go b/models/issues/label_test.go index 9ad6fd427b923..5e6cc9a2a0895 100644 --- a/models/issues/label_test.go +++ b/models/issues/label_test.go @@ -370,7 +370,7 @@ func TestDeleteIssueLabel(t *testing.T) { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) defer committer.Close() assert.NoError(t, err) assert.NoError(t, issues_model.DeleteIssueLabel(ctx, issue, label, doer)) diff --git a/models/issues/milestone.go b/models/issues/milestone.go index 3ccade7411e5f..9d1778f732e96 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -111,7 +111,7 @@ func (m *Milestone) State() api.StateType { // NewMilestone creates new milestone of repository. func NewMilestone(m *Milestone) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -161,7 +161,7 @@ func GetMilestoneByRepoIDANDName(repoID int64, name string) (*Milestone, error) // UpdateMilestone updates information of given milestone. func UpdateMilestone(m *Milestone, oldIsClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -219,7 +219,7 @@ func UpdateMilestoneCounters(ctx context.Context, id int64) error { // ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo. func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -246,7 +246,7 @@ func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool // ChangeMilestoneStatus changes the milestone open/closed status. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -290,7 +290,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/pull.go b/models/issues/pull.go index f03cabc3c828d..e906407d312ab 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -368,7 +368,7 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -498,7 +498,7 @@ func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) { // NewPullRequest creates new pull request with labels for repository. func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(outerCtx) if err != nil { return err } diff --git a/models/issues/reaction.go b/models/issues/reaction.go index c7503c23a2422..5cb63795d1d62 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -224,7 +224,7 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) { return nil, ErrForbiddenIssueReaction{opts.Type} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/issues/review.go b/models/issues/review.go index 3d2fceda2d949..f66c70c1fc007 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -374,7 +374,7 @@ func IsContentEmptyErr(err error) bool { // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, content, commitID string, stale bool, attachmentUUIDs []string) (*Review, *Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, nil, err } @@ -622,7 +622,7 @@ func DismissReview(review *Review, isDismiss bool) (err error) { // InsertReviews inserts review and review comments func InsertReviews(reviews []*Review) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -664,7 +664,7 @@ func InsertReviews(reviews []*Review) error { // AddReviewRequest add a review request from one reviewer func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -719,7 +719,7 @@ func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, // RemoveReviewRequest remove a review request from one reviewer func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -772,7 +772,7 @@ func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Commen // AddTeamReviewRequest add a review request from one team func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -831,7 +831,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ // RemoveTeamReviewRequest remove a review request from one team func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -949,7 +949,7 @@ func CanMarkConversation(issue *Issue, doer *user_model.User) (permResult bool, // DeleteReview delete a review and it's code comments func DeleteReview(r *Review) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index a87fbfafa2d21..d1d985b746981 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -261,7 +261,7 @@ func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss // CancelStopwatch removes the given stopwatch and logs it into issue's timeline. func CancelStopwatch(user *user_model.User, issue *Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go index ca21eb5149626..e1da0f32b0ef2 100644 --- a/models/issues/tracked_time.go +++ b/models/issues/tracked_time.go @@ -149,7 +149,7 @@ func GetTrackedSeconds(ctx context.Context, opts FindTrackedTimesOptions) (track // AddTime will add the given time (in seconds) to the issue func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -220,7 +220,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string, // DeleteIssueUserTimes deletes times for issue func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -257,7 +257,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { // DeleteTime delete a specific Time func DeleteTime(t *TrackedTime) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/migrate.go b/models/migrate.go index d842fb967bfb6..2a79640b076e1 100644 --- a/models/migrate.go +++ b/models/migrate.go @@ -20,7 +20,7 @@ func InsertMilestones(ms ...*issues_model.Milestone) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -42,7 +42,7 @@ func InsertMilestones(ms ...*issues_model.Milestone) (err error) { // InsertIssues insert issues to database func InsertIssues(issues ...*issues_model.Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -105,7 +105,7 @@ func InsertIssueComments(comments []*issues_model.Comment) error { issueIDs.Add(comment.IssueID) } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -137,7 +137,7 @@ func InsertIssueComments(comments []*issues_model.Comment) error { // InsertPullRequests inserted pull requests func InsertPullRequests(prs ...*issues_model.PullRequest) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -157,7 +157,7 @@ func InsertPullRequests(prs ...*issues_model.PullRequest) error { // InsertReleases migrates release func InsertReleases(rels ...*repo_model.Release) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/org.go b/models/org.go index 150c41f55da91..b56c80522b83d 100644 --- a/models/org.go +++ b/models/org.go @@ -99,7 +99,7 @@ func removeOrgUser(ctx context.Context, orgID, userID int64) error { // RemoveOrgUser removes user from given organization. func RemoveOrgUser(orgID, userID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/org_team.go b/models/org_team.go index 290b1c8b6ada1..792829563acba 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -76,7 +76,7 @@ func addAllRepositories(ctx context.Context, t *organization.Team) error { // AddAllRepositories adds all repositories to the team func AddAllRepositories(t *organization.Team) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -95,7 +95,7 @@ func RemoveAllRepositories(t *organization.Team) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -219,7 +219,7 @@ func RemoveRepository(t *organization.Team, repoID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -263,7 +263,7 @@ func NewTeam(t *organization.Team) (err error) { return organization.ErrTeamAlreadyExist{OrgID: t.OrgID, Name: t.LowerName} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -308,7 +308,7 @@ func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err t.Description = t.Description[:255] } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -375,7 +375,7 @@ func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err // DeleteTeam deletes given team. // It's caller's responsibility to assign organization ID. func DeleteTeam(t *organization.Team) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -460,7 +460,7 @@ func AddTeamMember(team *organization.Team, userID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -598,7 +598,7 @@ func removeTeamMember(ctx context.Context, team *organization.Team, userID int64 // RemoveTeamMember removes member from given team of given organization. func RemoveTeamMember(team *organization.Team, userID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/organization/org.go b/models/organization/org.go index 993ca3f10d884..af9c1f307c012 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -277,7 +277,7 @@ func CreateOrganization(org *Organization, owner *user_model.User) (err error) { org.NumMembers = 1 org.Type = user_model.UserTypeOrganization - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -564,7 +564,7 @@ func AddOrgUser(orgID, uid int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/organization/team_unit.go b/models/organization/team_unit.go index a712ddb2eb1a4..4ad1ae95dc99a 100644 --- a/models/organization/team_unit.go +++ b/models/organization/team_unit.go @@ -32,7 +32,7 @@ func getUnitsByTeamID(ctx context.Context, teamID int64) (units []*TeamUnit, err // UpdateTeamUnits updates a teams's units func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/project/board.go b/models/project/board.go index be7119ee4db3a..8428fca7220f9 100644 --- a/models/project/board.go +++ b/models/project/board.go @@ -133,7 +133,7 @@ func NewBoard(board *Board) error { // DeleteBoardByID removes all issues references to the project board. func DeleteBoardByID(boardID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/project/issue.go b/models/project/issue.go index 59af7063a546f..da48d4731e652 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -78,7 +78,7 @@ func (p *Project) NumOpenIssues() int { // MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column func MoveIssuesOnProjectBoard(board *Board, sortedIssueIDs map[int64]int64) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { sess := db.GetEngine(ctx) issueIDs := make([]int64, 0, len(sortedIssueIDs)) diff --git a/models/project/project.go b/models/project/project.go index ccdf5342d4ff1..5dee241dcf3dd 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -180,7 +180,7 @@ func NewProject(p *Project) error { return errors.New("project type is not valid") } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -248,7 +248,7 @@ func updateRepositoryProjectCount(ctx context.Context, repoID int64) error { // ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -272,7 +272,7 @@ func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) er // ChangeProjectStatus toggle a project between opened and closed func ChangeProjectStatus(p *Project, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -301,7 +301,7 @@ func changeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { // DeleteProjectByID deletes a project from a repository. func DeleteProjectByID(id int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo.go b/models/repo.go index 569dafee5f73c..2510f0e6bca46 100644 --- a/models/repo.go +++ b/models/repo.go @@ -45,7 +45,7 @@ func Init() error { // DeleteRepository deletes a repository for a user or organization. // make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock) func DeleteRepository(doer *user_model.User, uid, repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -569,7 +569,7 @@ func UpdateRepoStats(ctx context.Context, id int64) error { } func updateUserStarNumbers(users []user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go index 0aaa749210b2f..c558259e46834 100644 --- a/models/repo/collaboration.go +++ b/models/repo/collaboration.go @@ -138,7 +138,7 @@ func ChangeCollaborationAccessModeCtx(ctx context.Context, repo *Repository, uid // ChangeCollaborationAccessMode sets new access mode for the collaboration. func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.AccessMode) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/language_stats.go b/models/repo/language_stats.go index b047046aeb855..5c4cecdca1fd9 100644 --- a/models/repo/language_stats.go +++ b/models/repo/language_stats.go @@ -110,7 +110,7 @@ func GetTopLanguageStats(repo *Repository, limit int) (LanguageStatList, error) // UpdateLanguageStats updates the language statistics for repository func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -182,7 +182,7 @@ func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int // CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo) func CopyLanguageStat(originalRepo, destRepo *Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index dd85ca9186fcf..c21c1e7a79e2a 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -241,7 +241,7 @@ func UpdateRepoUnit(unit *RepoUnit) error { // UpdateRepositoryUnits updates a repository's units func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/star.go b/models/repo/star.go index 113b56f595f50..c7459b3655925 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -26,7 +26,7 @@ func init() { // StarRepo or unstar repository. func StarRepo(userID, repoID int64, star bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/topic.go b/models/repo/topic.go index 33bbb05af9316..db41c85a33c88 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -231,7 +231,7 @@ func GetRepoTopicByName(ctx context.Context, repoID int64, topicName string) (*T // AddTopic adds a topic name to a repository (if it does not already have it) func AddTopic(repoID int64, topicName string) (*Topic, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -293,7 +293,7 @@ func SaveTopics(repoID int64, topicNames ...string) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/update.go b/models/repo/update.go index cc21deb0bc4f1..39c89a7c7c192 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -21,7 +21,7 @@ func UpdateRepositoryOwnerNames(ownerID int64, ownerName string) error { if ownerID == 0 { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -172,7 +172,7 @@ func ChangeRepositoryName(doer *user_model.User, repo *Repository, newRepoName s } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/upload.go b/models/repo/upload.go index e115c8e50eceb..13b6055701fe7 100644 --- a/models/repo/upload.go +++ b/models/repo/upload.go @@ -121,7 +121,7 @@ func DeleteUploads(uploads ...*Upload) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index 58d6b0f488c43..684d0e63a02c0 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -25,7 +25,7 @@ func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) { UserID: uid, } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo_transfer.go b/models/repo_transfer.go index d6a3985fe5f28..067ab4122f0a7 100644 --- a/models/repo_transfer.go +++ b/models/repo_transfer.go @@ -124,7 +124,7 @@ func deleteRepositoryTransfer(ctx context.Context, repoID int64) error { // CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry, // thus cancel the transfer process. func CancelRepositoryTransfer(repo *repo_model.Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -156,7 +156,7 @@ func TestRepositoryReadyForTransfer(status repo_model.RepositoryStatus) error { // CreatePendingRepositoryTransfer transfer a repo from one owner to a new one. // it marks the repository transfer as "pending" func CreatePendingRepositoryTransfer(doer, newOwner *user_model.User, repoID int64, teams []*organization.Team) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -243,7 +243,7 @@ func TransferOwnership(doer *user_model.User, newOwnerName string, repo *repo_mo } }() - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/system/appstate.go b/models/system/appstate.go index c11a2512aba92..bc18c5181df83 100644 --- a/models/system/appstate.go +++ b/models/system/appstate.go @@ -25,7 +25,7 @@ func init() { // SaveAppStateContent saves the app state item to database func SaveAppStateContent(key, content string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { eng := db.GetEngine(ctx) // try to update existing row res, err := eng.Exec("UPDATE app_state SET revision=revision+1, content=? WHERE id=?", content, key) diff --git a/models/system/setting.go b/models/system/setting.go index b4011b1b3ed62..22119ee5e6fa4 100644 --- a/models/system/setting.go +++ b/models/system/setting.go @@ -196,7 +196,7 @@ func SetSetting(setting *Setting) error { } func upsertSettingValue(key, value string, version int) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { e := db.GetEngine(ctx) // here we use a general method to do a safe upsert for different databases (and most transaction levels) diff --git a/models/user/email_address.go b/models/user/email_address.go index d6279b6639ab7..b6d372f04d7bb 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -321,7 +321,7 @@ func DeleteInactiveEmailAddresses(ctx context.Context) error { // ActivateEmail activates the email address to given user. func ActivateEmail(email *EmailAddress) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -372,7 +372,7 @@ func MakeEmailPrimary(email *EmailAddress) error { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -510,7 +510,7 @@ func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error) // ActivateUserEmail will change the activated state of an email address, // either primary or secondary (all in the email_address table) func ActivateUserEmail(userID int64, email string, activate bool) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/user/follow.go b/models/user/follow.go index 5f24f706d16b5..a02ebdb8c2dd0 100644 --- a/models/user/follow.go +++ b/models/user/follow.go @@ -33,7 +33,7 @@ func FollowUser(userID, followID int64) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -59,7 +59,7 @@ func UnfollowUser(userID, followID int64) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/user/setting.go b/models/user/setting.go index 896f3c8da12d0..41de499de97ce 100644 --- a/models/user/setting.go +++ b/models/user/setting.go @@ -163,7 +163,7 @@ func SetUserSetting(userID int64, key, value string) error { } func upsertUserSettingValue(userID int64, key, value string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { e := db.GetEngine(ctx) // here we use a general method to do a safe upsert for different databases (and most transaction levels) diff --git a/models/user/user.go b/models/user/user.go index 84e2c4a9cc6ab..c36fc21c770ec 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -695,7 +695,7 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -814,7 +814,7 @@ func ChangeUserName(u *User, newUserName string) (err error) { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -957,7 +957,7 @@ func UpdateUserCols(ctx context.Context, u *User, cols ...string) error { // UpdateUserSetting updates user's settings. func UpdateUserSetting(u *User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 6426b95202c0b..1daf806c4e877 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -575,7 +575,7 @@ func UpdateWebhookLastStatus(w *Webhook) error { // deleteWebhook uses argument bean as query condition, // ID must be specified and do not assign unnecessary fields. func deleteWebhook(bean *Webhook) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -610,7 +610,7 @@ func DeleteWebhookByOrgID(orgID, id int64) error { // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0) func DeleteDefaultSystemWebhook(id int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/modules/repository/collaborator.go b/modules/repository/collaborator.go index 9d20a25890352..b83d8199b9d7c 100644 --- a/modules/repository/collaborator.go +++ b/modules/repository/collaborator.go @@ -37,7 +37,7 @@ func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_m // AddCollaborator adds new collaboration to a repository with default access mode. func AddCollaborator(repo *repo_model.Repository, u *user_model.User) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { return addCollaborator(ctx, repo, u) }) } diff --git a/modules/repository/create.go b/modules/repository/create.go index 1fec0335a28b4..c43f1e09898a3 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -211,7 +211,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m var rollbackRepo *repo_model.Repository - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { if err := CreateRepositoryByExample(ctx, doer, u, repo, false); err != nil { return err } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 51e1699821c0f..1d3a4658c20ea 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -173,7 +173,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -489,7 +489,7 @@ func pullMirrorReleaseSync(repo *repo_model.Repository, gitRepo *git.Repository) if err != nil { return fmt.Errorf("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err) } - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { // // clear out existing releases // diff --git a/routers/api/packages/conan/conan.go b/routers/api/packages/conan/conan.go index c8c9dc3e384bd..b69a674106ce7 100644 --- a/routers/api/packages/conan/conan.go +++ b/routers/api/packages/conan/conan.go @@ -604,7 +604,7 @@ func DeletePackageV2(ctx *context.Context) { } func deleteRecipeOrPackage(apictx *context.Context, rref *conan_module.RecipeReference, ignoreRecipeRevision bool, pref *conan_module.PackageReference, ignorePackageRevision bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go index 8a9cbd4a15fbf..df6b7aed92d48 100644 --- a/routers/api/packages/container/blob.go +++ b/routers/api/packages/container/blob.go @@ -28,7 +28,7 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pi *packages_servic contentStore := packages_module.NewContentStore() - err := db.WithTx(func(ctx context.Context) error { + err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { created := true p := &packages_model.Package{ OwnerID: pi.Owner.ID, @@ -117,7 +117,7 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pi *packages_servic } func deleteBlob(ownerID int64, image, digest string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { pfds, err := container_model.GetContainerBlobs(ctx, &container_model.BlobSearchOptions{ OwnerID: ownerID, Image: image, diff --git a/routers/api/packages/container/manifest.go b/routers/api/packages/container/manifest.go index 8beed3dbb7296..a48b1de3b2802 100644 --- a/routers/api/packages/container/manifest.go +++ b/routers/api/packages/container/manifest.go @@ -77,7 +77,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -190,7 +190,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go index 6d589bde3a541..af0e9be56eacd 100644 --- a/routers/api/packages/npm/npm.go +++ b/routers/api/packages/npm/npm.go @@ -355,7 +355,7 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo return errInvalidTagName } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/asymkey/deploy_key.go b/services/asymkey/deploy_key.go index aa0925ab13392..4feeadd952513 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 143c807a106a9..06b20001fd2f3 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 557cc808a3e39..cc329f568bc46 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 3ee8af2344d3f..5c38367e3be52 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 c40631359b733..7167219c20ce0 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 47782e50d36a2..8342d7f5d2de7 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 bc5f9b910e643..a9a0c20b01492 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 756a8625de47b..965b07556d1e0 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 39845610d244e..d993f82bbf69f 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 769419d45b63d..88520c819ff90 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 443976e174b75..4513215ae14d9 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 830ff640b52b8..6e91c22f3ea63 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 56ee9c9a734ec..6f37a887db438 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 8ccc222fb261a..cf398debbb8eb 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 a9a063954800d..57a21f500cd56 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 79537ea051678..f1c2693aeb104 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 1cf9e869c0747..e4c8da3ba2d99 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 136d7ccab6520..e597bfa449d4a 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 3a7205d18b6f8..6776ff9f740f1 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 763ef74927ca6..2f2c27ff20bf6 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 b73abdce587f7..bb5e3f46689cd 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 c8b497a5c46f8..4186efe68257a 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 }