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>
This commit is contained in:
Lunny Xiao 2022-11-13 04:18:50 +08:00 committed by GitHub
parent a0a425a13b
commit 34283a74e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
91 changed files with 252 additions and 176 deletions

View file

@ -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
}