summaryrefslogtreecommitdiffstats
path: root/models/issues
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-10-16 18:44:16 +0800
committerGitHub <noreply@github.com>2022-10-16 18:44:16 +0800
commit6f48a36227b7427afd2020aa416afe49d4c81015 (patch)
treeacf275dd1daec784c756014d6aba0225fb60558f /models/issues
parent0647df3e831c320436d42a5572a6ec6a9d23d8b1 (diff)
downloadgitea-6f48a36227b7427afd2020aa416afe49d4c81015.tar.gz
gitea-6f48a36227b7427afd2020aa416afe49d4c81015.zip
Refactor GetNextResourceIndex to make it work properly with transaction (#21469)
Related: * #21362 This PR uses a general and stable method to generate resource index (eg: Issue Index, PR Index) If the code looks good, I can add more tests ps: please skip the diff, only have a look at the new code. It's entirely re-written. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/issues')
-rw-r--r--models/issues/issue.go14
-rw-r--r--models/issues/issue_index.go8
-rw-r--r--models/issues/issue_xref_test.go9
-rw-r--r--models/issues/pull.go14
4 files changed, 21 insertions, 24 deletions
diff --git a/models/issues/issue.go b/models/issues/issue.go
index 49bc229c6b..737b625abc 100644
--- a/models/issues/issue.go
+++ b/models/issues/issue.go
@@ -1064,19 +1064,19 @@ 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) {
- idx, err := db.GetNextResourceIndex("issue_index", repo.ID)
- if err != nil {
- return fmt.Errorf("generate issue index failed: %v", err)
- }
-
- issue.Index = idx
-
ctx, committer, err := db.TxContext()
if err != nil {
return err
}
defer committer.Close()
+ idx, err := db.GetNextResourceIndex(ctx, "issue_index", repo.ID)
+ if err != nil {
+ return fmt.Errorf("generate issue index failed: %w", err)
+ }
+
+ issue.Index = idx
+
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Issue: issue,
diff --git a/models/issues/issue_index.go b/models/issues/issue_index.go
index 100e814317..f4acc5aa1b 100644
--- a/models/issues/issue_index.go
+++ b/models/issues/issue_index.go
@@ -15,16 +15,12 @@ func RecalculateIssueIndexForRepo(repoID int64) error {
}
defer committer.Close()
- if err := db.UpsertResourceIndex(ctx, "issue_index", repoID); err != nil {
- return err
- }
-
var max int64
- if _, err := db.GetEngine(ctx).Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max); err != nil {
+ if _, err = db.GetEngine(ctx).Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&max); err != nil {
return err
}
- if _, err := db.GetEngine(ctx).Exec("UPDATE `issue_index` SET max_index=? WHERE group_id=?", max, repoID); err != nil {
+ if err = db.SyncMaxResourceIndex(ctx, "issue_index", repoID, max); err != nil {
return err
}
diff --git a/models/issues/issue_xref_test.go b/models/issues/issue_xref_test.go
index af1c8bc685..0f72fc7ca6 100644
--- a/models/issues/issue_xref_test.go
+++ b/models/issues/issue_xref_test.go
@@ -131,7 +131,11 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu
r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo})
d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer})
- idx, err := db.GetNextResourceIndex("issue_index", r.ID)
+ ctx, committer, err := db.TxContext()
+ assert.NoError(t, err)
+ defer committer.Close()
+
+ idx, err := db.GetNextResourceIndex(ctx, "issue_index", r.ID)
assert.NoError(t, err)
i := &issues_model.Issue{
RepoID: r.ID,
@@ -143,9 +147,6 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu
Index: idx,
}
- ctx, committer, err := db.TxContext()
- assert.NoError(t, err)
- defer committer.Close()
err = issues_model.NewIssueWithIndex(ctx, d, issues_model.NewIssueOptions{
Repo: r,
Issue: i,
diff --git a/models/issues/pull.go b/models/issues/pull.go
index f96b03445e..69259c269f 100644
--- a/models/issues/pull.go
+++ b/models/issues/pull.go
@@ -490,13 +490,6 @@ func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) {
// NewPullRequest creates new pull request with labels for repository.
func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
- idx, err := db.GetNextResourceIndex("issue_index", repo.ID)
- if err != nil {
- return fmt.Errorf("generate pull request index failed: %v", err)
- }
-
- issue.Index = idx
-
ctx, committer, err := db.TxContext()
if err != nil {
return err
@@ -504,6 +497,13 @@ func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue
defer committer.Close()
ctx.WithContext(outerCtx)
+ idx, err := db.GetNextResourceIndex(ctx, "issue_index", repo.ID)
+ if err != nil {
+ return fmt.Errorf("generate pull request index failed: %v", err)
+ }
+
+ issue.Index = idx
+
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
Repo: repo,
Issue: issue,