forked from forgejo/forgejo
Add a new table issue_index to store the max issue index so that issue could be deleted with no duplicated index (#15599)
* Add a new table issue_index to store the max issue index so that issue could be deleted with no duplicated index * Fix pull index * Add tests for concurrent creating issues * Fix lint * Fix tests * Fix postgres test * Add test for migration v180 * Rename wrong test file name Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
a005265718
commit
0393a57511
14 changed files with 354 additions and 82 deletions
|
@ -427,34 +427,23 @@ func (pr *PullRequest) SetMerged() (bool, error) {
|
|||
}
|
||||
|
||||
// NewPullRequest creates new pull request with labels for repository.
|
||||
func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
|
||||
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
|
||||
i := 0
|
||||
for {
|
||||
if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr); err == nil {
|
||||
return nil
|
||||
}
|
||||
if !IsErrNewIssueInsert(err) {
|
||||
return err
|
||||
}
|
||||
if i++; i == issueMaxDupIndexAttempts {
|
||||
break
|
||||
}
|
||||
log.Error("NewPullRequest: error attempting to insert the new issue; will retry. Original error: %v", err)
|
||||
func NewPullRequest(repo *Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
|
||||
idx, err := GetNextResourceIndex("issue_index", repo.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate issue index failed: %v", err)
|
||||
}
|
||||
return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err)
|
||||
}
|
||||
|
||||
func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
|
||||
issue.Index = idx
|
||||
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = newIssue(sess, pull.Poster, NewIssueOptions{
|
||||
if err = newIssue(sess, issue.Poster, NewIssueOptions{
|
||||
Repo: repo,
|
||||
Issue: pull,
|
||||
Issue: issue,
|
||||
LabelIDs: labelIDs,
|
||||
Attachments: uuids,
|
||||
IsPull: true,
|
||||
|
@ -465,10 +454,9 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
|
|||
return fmt.Errorf("newIssue: %v", err)
|
||||
}
|
||||
|
||||
pr.Index = pull.Index
|
||||
pr.Index = issue.Index
|
||||
pr.BaseRepo = repo
|
||||
|
||||
pr.IssueID = pull.ID
|
||||
pr.IssueID = issue.ID
|
||||
if _, err = sess.Insert(pr); err != nil {
|
||||
return fmt.Errorf("insert pull repo: %v", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue