Skip to content

Commit 58d15a8

Browse files
committed
refactor db.TxContext
1 parent 1211d80 commit 58d15a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+147
-143
lines changed

models/activities/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func NotifyWatchers(actions ...*Action) error {
572572

573573
// NotifyWatchersActions creates batch of actions for every watcher.
574574
func NotifyWatchersActions(acts []*Action) error {
575-
ctx, committer, err := db.TxContext()
575+
ctx, committer, err := db.TxContext(db.DefaultContext)
576576
if err != nil {
577577
return err
578578
}

models/activities/notification.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func CountNotifications(opts *FindNotificationOptions) (int64, error) {
142142

143143
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
144144
func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error {
145-
ctx, committer, err := db.TxContext()
145+
ctx, committer, err := db.TxContext(db.DefaultContext)
146146
if err != nil {
147147
return err
148148
}
@@ -185,7 +185,7 @@ func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_
185185
// for each watcher, or updates it if already exists
186186
// receiverID > 0 just send to receiver, else send to all watcher
187187
func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error {
188-
ctx, committer, err := db.TxContext()
188+
ctx, committer, err := db.TxContext(db.DefaultContext)
189189
if err != nil {
190190
return err
191191
}

models/asymkey/gpg_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
234234
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
235235
}
236236

237-
ctx, committer, err := db.TxContext()
237+
ctx, committer, err := db.TxContext(db.DefaultContext)
238238
if err != nil {
239239
return err
240240
}

models/asymkey/gpg_key_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro
7373
return nil, err
7474
}
7575

76-
ctx, committer, err := db.TxContext()
76+
ctx, committer, err := db.TxContext(db.DefaultContext)
7777
if err != nil {
7878
return nil, err
7979
}

models/asymkey/gpg_key_verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
// VerifyGPGKey marks a GPG key as verified
3333
func VerifyGPGKey(ownerID int64, keyID, token, signature string) (string, error) {
34-
ctx, committer, err := db.TxContext()
34+
ctx, committer, err := db.TxContext(db.DefaultContext)
3535
if err != nil {
3636
return "", err
3737
}

models/asymkey/ssh_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub
100100
return nil, err
101101
}
102102

103-
ctx, committer, err := db.TxContext()
103+
ctx, committer, err := db.TxContext(db.DefaultContext)
104104
if err != nil {
105105
return nil, err
106106
}
@@ -321,7 +321,7 @@ func PublicKeyIsExternallyManaged(id int64) (bool, error) {
321321
// deleteKeysMarkedForDeletion returns true if ssh keys needs update
322322
func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
323323
// Start session
324-
ctx, committer, err := db.TxContext()
324+
ctx, committer, err := db.TxContext(db.DefaultContext)
325325
if err != nil {
326326
return false, err
327327
}

models/asymkey/ssh_key_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
126126
accessMode = perm.AccessModeWrite
127127
}
128128

129-
ctx, committer, err := db.TxContext()
129+
ctx, committer, err := db.TxContext(db.DefaultContext)
130130
if err != nil {
131131
return nil, err
132132
}

models/asymkey/ssh_key_principals.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// AddPrincipalKey adds new principal to database and authorized_principals file.
2828
func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) {
29-
ctx, committer, err := db.TxContext()
29+
ctx, committer, err := db.TxContext(db.DefaultContext)
3030
if err != nil {
3131
return nil, err
3232
}

models/asymkey/ssh_key_verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// VerifySSHKey marks a SSH key as verified
1717
func VerifySSHKey(ownerID int64, fingerprint, token, signature string) (string, error) {
18-
ctx, committer, err := db.TxContext()
18+
ctx, committer, err := db.TxContext(db.DefaultContext)
1919
if err != nil {
2020
return "", err
2121
}

models/auth/oauth2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ type UpdateOAuth2ApplicationOptions struct {
201201

202202
// UpdateOAuth2Application updates an oauth2 application
203203
func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) {
204-
ctx, committer, err := db.TxContext()
204+
ctx, committer, err := db.TxContext(db.DefaultContext)
205205
if err != nil {
206206
return nil, err
207207
}
@@ -265,7 +265,7 @@ func deleteOAuth2Application(ctx context.Context, id, userid int64) error {
265265

266266
// 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.
267267
func DeleteOAuth2Application(id, userid int64) error {
268-
ctx, committer, err := db.TxContext()
268+
ctx, committer, err := db.TxContext(db.DefaultContext)
269269
if err != nil {
270270
return err
271271
}

models/auth/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ReadSession(key string) (*Session, error) {
3737
Key: key,
3838
}
3939

40-
ctx, committer, err := db.TxContext()
40+
ctx, committer, err := db.TxContext(db.DefaultContext)
4141
if err != nil {
4242
return nil, err
4343
}
@@ -73,7 +73,7 @@ func DestroySession(key string) error {
7373

7474
// RegenerateSession regenerates a session from the old id
7575
func RegenerateSession(oldKey, newKey string) (*Session, error) {
76-
ctx, committer, err := db.TxContext()
76+
ctx, committer, err := db.TxContext(db.DefaultContext)
7777
if err != nil {
7878
return nil, err
7979
}

models/db/context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ type Committer interface {
8888
}
8989

9090
// TxContext represents a transaction Context
91-
func TxContext() (*Context, Committer, error) {
91+
func TxContext(parentCtx context.Context) (*Context, Committer, error) {
92+
if InTransaction(parentCtx) {
93+
return nil, nil, ErrAlreadyInTransaction
94+
}
95+
9296
sess := x.NewSession()
9397
if err := sess.Begin(); err != nil {
9498
sess.Close()

models/db/context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestInTransaction(t *testing.T) {
2222
return nil
2323
}))
2424

25-
ctx, committer, err := db.TxContext()
25+
ctx, committer, err := db.TxContext(db.DefaultContext)
2626
assert.NoError(t, err)
2727
defer committer.Close()
2828
assert.True(t, db.InTransaction(ctx))

models/git/branches.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist
544544

545545
// RenameBranch rename a branch
546546
func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
547-
ctx, committer, err := db.TxContext()
547+
ctx, committer, err := db.TxContext(db.DefaultContext)
548548
if err != nil {
549549
return err
550550
}

models/git/branches_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestRenameBranch(t *testing.T) {
102102
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
103103
_isDefault := false
104104

105-
ctx, committer, err := db.TxContext()
105+
ctx, committer, err := db.TxContext(db.DefaultContext)
106106
defer committer.Close()
107107
assert.NoError(t, err)
108108
assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{

models/git/commit_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) {
9494

9595
// getNextCommitStatusIndex return the next index
9696
func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) {
97-
ctx, commiter, err := db.TxContext()
97+
ctx, commiter, err := db.TxContext(db.DefaultContext)
9898
if err != nil {
9999
return 0, err
100100
}
@@ -297,7 +297,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
297297
return fmt.Errorf("generate commit status index failed: %w", err)
298298
}
299299

300-
ctx, committer, err := db.TxContext()
300+
ctx, committer, err := db.TxContext(db.DefaultContext)
301301
if err != nil {
302302
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
303303
}

models/git/lfs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"}
137137
func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
138138
var err error
139139

140-
ctx, committer, err := db.TxContext()
140+
ctx, committer, err := db.TxContext(db.DefaultContext)
141141
if err != nil {
142142
return nil, err
143143
}
@@ -185,7 +185,7 @@ func RemoveLFSMetaObjectByOid(repoID int64, oid string) (int64, error) {
185185
return 0, ErrLFSObjectNotExist
186186
}
187187

188-
ctx, committer, err := db.TxContext()
188+
ctx, committer, err := db.TxContext(db.DefaultContext)
189189
if err != nil {
190190
return 0, err
191191
}
@@ -242,7 +242,7 @@ func LFSObjectIsAssociated(oid string) (bool, error) {
242242

243243
// LFSAutoAssociate auto associates accessible LFSMetaObjects
244244
func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int64) error {
245-
ctx, committer, err := db.TxContext()
245+
ctx, committer, err := db.TxContext(db.DefaultContext)
246246
if err != nil {
247247
return err
248248
}

models/git/lfs_lock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func cleanPath(p string) string {
4444

4545
// CreateLFSLock creates a new lock.
4646
func CreateLFSLock(repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) {
47-
dbCtx, committer, err := db.TxContext()
47+
dbCtx, committer, err := db.TxContext(db.DefaultContext)
4848
if err != nil {
4949
return nil, err
5050
}
@@ -137,7 +137,7 @@ func CountLFSLockByRepoID(repoID int64) (int64, error) {
137137

138138
// DeleteLFSLockByID deletes a lock by given ID.
139139
func DeleteLFSLockByID(id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) {
140-
dbCtx, committer, err := db.TxContext()
140+
dbCtx, committer, err := db.TxContext(db.DefaultContext)
141141
if err != nil {
142142
return nil, err
143143
}

models/issues/assignees.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.U
6464

6565
// ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
6666
func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) {
67-
ctx, committer, err := db.TxContext()
67+
ctx, committer, err := db.TxContext(db.DefaultContext)
6868
if err != nil {
6969
return false, nil, err
7070
}

models/issues/comment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func (c *Comment) LoadAttachments() error {
565565

566566
// UpdateAttachments update attachments by UUIDs for the comment
567567
func (c *Comment) UpdateAttachments(uuids []string) error {
568-
ctx, committer, err := db.TxContext()
568+
ctx, committer, err := db.TxContext(db.DefaultContext)
569569
if err != nil {
570570
return err
571571
}
@@ -1003,7 +1003,7 @@ type CreateCommentOptions struct {
10031003

10041004
// CreateComment creates comment of issue or commit.
10051005
func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
1006-
ctx, committer, err := db.TxContext()
1006+
ctx, committer, err := db.TxContext(db.DefaultContext)
10071007
if err != nil {
10081008
return nil, err
10091009
}
@@ -1135,7 +1135,7 @@ func CountComments(opts *FindCommentsOptions) (int64, error) {
11351135

11361136
// UpdateComment updates information of comment.
11371137
func UpdateComment(c *Comment, doer *user_model.User) error {
1138-
ctx, committer, err := db.TxContext()
1138+
ctx, committer, err := db.TxContext(db.DefaultContext)
11391139
if err != nil {
11401140
return err
11411141
}

models/issues/dependency.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const (
129129

130130
// CreateIssueDependency creates a new dependency for an issue
131131
func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error {
132-
ctx, committer, err := db.TxContext()
132+
ctx, committer, err := db.TxContext(db.DefaultContext)
133133
if err != nil {
134134
return err
135135
}
@@ -170,7 +170,7 @@ func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error {
170170

171171
// RemoveIssueDependency removes a dependency from an issue
172172
func RemoveIssueDependency(user *user_model.User, issue, dep *Issue, depType DependencyType) (err error) {
173-
ctx, committer, err := db.TxContext()
173+
ctx, committer, err := db.TxContext(db.DefaultContext)
174174
if err != nil {
175175
return err
176176
}

models/issues/issue.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func clearIssueLabels(ctx context.Context, issue *Issue, doer *user_model.User)
540540
// ClearIssueLabels removes all issue labels as the given user.
541541
// Triggers appropriate WebHooks, if any.
542542
func ClearIssueLabels(issue *Issue, doer *user_model.User) (err error) {
543-
ctx, committer, err := db.TxContext()
543+
ctx, committer, err := db.TxContext(db.DefaultContext)
544544
if err != nil {
545545
return err
546546
}
@@ -588,7 +588,7 @@ func (ts labelSorter) Swap(i, j int) {
588588
// ReplaceIssueLabels removes all current labels and add new labels to the issue.
589589
// Triggers appropriate WebHooks, if any.
590590
func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err error) {
591-
ctx, committer, err := db.TxContext()
591+
ctx, committer, err := db.TxContext(db.DefaultContext)
592592
if err != nil {
593593
return err
594594
}
@@ -760,7 +760,7 @@ func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User,
760760

761761
// ChangeIssueTitle changes the title of this issue, as the given user.
762762
func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err error) {
763-
ctx, committer, err := db.TxContext()
763+
ctx, committer, err := db.TxContext(db.DefaultContext)
764764
if err != nil {
765765
return err
766766
}
@@ -794,7 +794,7 @@ func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err
794794

795795
// ChangeIssueRef changes the branch of this issue, as the given user.
796796
func ChangeIssueRef(issue *Issue, doer *user_model.User, oldRef string) (err error) {
797-
ctx, committer, err := db.TxContext()
797+
ctx, committer, err := db.TxContext(db.DefaultContext)
798798
if err != nil {
799799
return err
800800
}
@@ -844,7 +844,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo *
844844

845845
// UpdateIssueAttachments update attachments by UUIDs for the issue
846846
func UpdateIssueAttachments(issueID int64, uuids []string) (err error) {
847-
ctx, committer, err := db.TxContext()
847+
ctx, committer, err := db.TxContext(db.DefaultContext)
848848
if err != nil {
849849
return err
850850
}
@@ -864,7 +864,7 @@ func UpdateIssueAttachments(issueID int64, uuids []string) (err error) {
864864

865865
// ChangeIssueContent changes issue content, as the given user.
866866
func ChangeIssueContent(issue *Issue, doer *user_model.User, content string) (err error) {
867-
ctx, committer, err := db.TxContext()
867+
ctx, committer, err := db.TxContext(db.DefaultContext)
868868
if err != nil {
869869
return err
870870
}
@@ -1069,7 +1069,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
10691069

10701070
// NewIssue creates new issue with labels for repository.
10711071
func NewIssue(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
1072-
ctx, committer, err := db.TxContext()
1072+
ctx, committer, err := db.TxContext(db.DefaultContext)
10731073
if err != nil {
10741074
return err
10751075
}
@@ -1986,7 +1986,7 @@ func SearchIssueIDsByKeyword(ctx context.Context, kw string, repoIDs []int64, li
19861986
// If the issue status is changed a statusChangeComment is returned
19871987
// similarly if the title is changed the titleChanged bool is set to true
19881988
func UpdateIssueByAPI(issue *Issue, doer *user_model.User) (statusChangeComment *Comment, titleChanged bool, err error) {
1989-
ctx, committer, err := db.TxContext()
1989+
ctx, committer, err := db.TxContext(db.DefaultContext)
19901990
if err != nil {
19911991
return nil, false, err
19921992
}
@@ -2044,7 +2044,7 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *us
20442044
if issue.DeadlineUnix == deadlineUnix {
20452045
return nil
20462046
}
2047-
ctx, committer, err := db.TxContext()
2047+
ctx, committer, err := db.TxContext(db.DefaultContext)
20482048
if err != nil {
20492049
return err
20502050
}
@@ -2436,7 +2436,7 @@ func CountOrphanedIssues() (int64, error) {
24362436

24372437
// DeleteOrphanedIssues delete issues without a repo
24382438
func DeleteOrphanedIssues() error {
2439-
ctx, committer, err := db.TxContext()
2439+
ctx, committer, err := db.TxContext(db.DefaultContext)
24402440
if err != nil {
24412441
return err
24422442
}

models/issues/issue_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import "code.gitea.io/gitea/models/db"
99
// RecalculateIssueIndexForRepo create issue_index for repo if not exist and
1010
// update it based on highest index of existing issues assigned to a repo
1111
func RecalculateIssueIndexForRepo(repoID int64) error {
12-
ctx, committer, err := db.TxContext()
12+
ctx, committer, err := db.TxContext(db.DefaultContext)
1313
if err != nil {
1414
return err
1515
}

models/issues/issue_lock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error {
4040
commentType = CommentTypeUnlock
4141
}
4242

43-
ctx, committer, err := db.TxContext()
43+
ctx, committer, err := db.TxContext(db.DefaultContext)
4444
if err != nil {
4545
return err
4646
}

0 commit comments

Comments
 (0)