Skip to content

Commit 34283a7

Browse files
lunnydelvh6543
authored
Allow detect whether it's in a database transaction for a context.Context (#21756)
Fix #19513 This PR introduce a new db method `InTransaction(context.Context)`, and also builtin check on `db.TxContext` and `db.WithTx`. There is also a new method `db.AutoTx` has been introduced but could be used by other PRs. `WithTx` will always open a new transaction, if a transaction exist in context, return an error. `AutoTx` will try to open a new transaction if no transaction exist in context. That means it will always enter a transaction if there is no error. Co-authored-by: delvh <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent a0a425a commit 34283a7

Some content is hidden

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

91 files changed

+252
-176
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
}

0 commit comments

Comments
 (0)