diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-11-13 04:18:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-12 21:18:50 +0100 |
commit | 34283a74e85278fed2c9b70d6f8749dc6a4001ca (patch) | |
tree | f8fa43399cb723d55efc34dacf7947faf0ed5acf /models/user | |
parent | a0a425a13ba587829a831aaecd8469d39d372111 (diff) | |
download | gitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.tar.gz gitea-34283a74e85278fed2c9b70d6f8749dc6a4001ca.zip |
Allow detect whether it's in a database transaction for a context.Context (#21756)
Fix #19513
This PR introduce a new db method `InTransaction(context.Context)`,
and also builtin check on `db.TxContext` and `db.WithTx`.
There is also a new method `db.AutoTx` has been introduced but could be used by other PRs.
`WithTx` will always open a new transaction, if a transaction exist in context, return an error.
`AutoTx` will try to open a new transaction if no transaction exist in context.
That means it will always enter a transaction if there is no error.
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'models/user')
-rw-r--r-- | models/user/email_address.go | 6 | ||||
-rw-r--r-- | models/user/follow.go | 4 | ||||
-rw-r--r-- | models/user/setting.go | 2 | ||||
-rw-r--r-- | models/user/user.go | 6 |
4 files changed, 9 insertions, 9 deletions
diff --git a/models/user/email_address.go b/models/user/email_address.go index d6279b6639..b6d372f04d 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 5f24f706d1..a02ebdb8c2 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 896f3c8da1..41de499de9 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 84e2c4a9cc..c36fc21c77 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 } |