From 1b2e083422a8625d2f6f3c61f6211896c96ec7c4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Sep 2021 00:16:15 +0800 Subject: [PATCH 1/9] Fix commit status index problem --- models/commit_status.go | 112 +++++++++++++++++++++++++++----- models/db/index.go | 5 +- models/migrations/migrations.go | 2 + models/migrations/v195.go | 47 ++++++++++++++ 4 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 models/migrations/v195.go diff --git a/models/commit_status.go b/models/commit_status.go index f3639e819ea7e..740b864917ad1 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -142,6 +142,95 @@ func sortCommitStatusesSession(sess *xorm.Session, sortType string) { } } +// CommitStatusIndex represents a table for commit status index +type CommitStatusIndex struct { + ID int64 + RepoID int64 `xorm:"unique(repo_sha)"` + SHA string `xorm:"unique(repo_sha)"` + MaxIndex int64 `xorm:"index"` +} + +// upsertCommitStatusIndex the function will not return until it acquires the lock or receives an error. +func upsertCommitStatusIndex(e db.Engine, repoID int64, sha string) (err error) { + // An atomic UPSERT operation (INSERT/UPDATE) is the only operation + // that ensures that the key is actually locked. + switch { + case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL: + _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ + "VALUES (?,?,1) ON CONFLICT (repo_id,sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1", + repoID, sha) + case setting.Database.UseMySQL: + _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ + "VALUES (?,?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", + repoID, sha) + case setting.Database.UseMSSQL: + // https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/ + _, err = e.Exec("MERGE `commit_status_index` WITH (HOLDLOCK) as target "+ + "USING (SELECT ? AS repo_id, ? AS sha) AS src "+ + "ON src.repo_id = target.repo_id AND src.sha = target.sha "+ + "WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+ + "WHEN NOT MATCHED THEN INSERT (repo_id, sha, max_index) "+ + "VALUES (src.repo_id, src.sha, 1);", + repoID, sha) + default: + return fmt.Errorf("database type not supported") + } + return +} + +// GetNextResourceIndex retried 3 times to generate a resource index +func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { + for i := 0; i < db.MaxDupIndexAttempts; i++ { + idx, err := getNextCommitStatusIndex(repoID, sha) + if err == db.ErrResouceOutdated { + continue + } + if err != nil { + return 0, err + } + return idx, nil + } + return 0, db.ErrGetResourceIndexFailed +} + +// deleteResouceIndex delete resource index +func deleteCommitStatusIndex(e db.Engine, repoID int64, sha string) error { + _, err := e.Exec("DELETE FROM `commit_status_index` WHERE repo_id=? AND sha=?", repoID, sha) + return err +} + +// getNextCommitStatusIndex return the next index +func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { + ctx, commiter, err := db.TxContext() + if err != nil { + return 0, err + } + defer commiter.Close() + + var preIdx int64 + _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE group_id = ?", repoID).Get(&preIdx) + if err != nil { + return 0, err + } + + if err := upsertCommitStatusIndex(ctx.Engine(), repoID, sha); err != nil { + return 0, err + } + + var curIdx int64 + has, err := ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ? AND max_index=?", repoID, sha, preIdx+1).Get(&curIdx) + if err != nil { + return 0, err + } + if !has { + return 0, db.ErrResouceOutdated + } + if err := commiter.Commit(); err != nil { + return 0, err + } + return curIdx, nil +} + // GetLatestCommitStatus returns all statuses with a unique context for a given commit. func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { return getLatestCommitStatus(db.DefaultContext().Engine(), repoID, sha, listOptions) @@ -206,6 +295,12 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA) } + // Get the next Status Index + idx, err := GetNextCommitStatusIndex(opts.Repo.ID, opts.SHA) + if err != nil { + return fmt.Errorf("generate issue index failed: %v", err) + } + ctx, committer, err := db.TxContext() if err != nil { return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", opts.Repo.ID, opts.Creator.ID, opts.SHA, err) @@ -218,22 +313,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { opts.CommitStatus.SHA = opts.SHA opts.CommitStatus.CreatorID = opts.Creator.ID opts.CommitStatus.RepoID = opts.Repo.ID - - // Get the next Status Index - var nextIndex int64 - lastCommitStatus := &CommitStatus{ - SHA: opts.SHA, - RepoID: opts.Repo.ID, - } - has, err := ctx.Engine().Desc("index").Limit(1).Get(lastCommitStatus) - if err != nil { - return fmt.Errorf("NewCommitStatus[%s, %s]: %v", repoPath, opts.SHA, err) - } - if has { - log.Debug("NewCommitStatus[%s, %s]: found", repoPath, opts.SHA) - nextIndex = lastCommitStatus.Index - } - opts.CommitStatus.Index = nextIndex + 1 + opts.CommitStatus.Index = idx log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index) opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context) diff --git a/models/db/index.go b/models/db/index.go index 873289db54e16..0086a8f548061 100644 --- a/models/db/index.go +++ b/models/db/index.go @@ -54,12 +54,13 @@ var ( ) const ( - maxDupIndexAttempts = 3 + // MaxDupIndexAttempts max retry times to create index + MaxDupIndexAttempts = 3 ) // GetNextResourceIndex retried 3 times to generate a resource index func GetNextResourceIndex(tableName string, groupID int64) (int64, error) { - for i := 0; i < maxDupIndexAttempts; i++ { + for i := 0; i < MaxDupIndexAttempts; i++ { idx, err := getNextResourceIndex(tableName, groupID) if err == ErrResouceOutdated { continue diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index fb6958f2da611..3f90e5e74a3f8 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -342,6 +342,8 @@ var migrations = []Migration{ NewMigration("Add repo id column for attachment table", addRepoIDForAttachment), // v194 -> v195 NewMigration("Add Branch Protection Unprotected Files Column", addBranchProtectionUnprotectedFilesColumn), + // v196 -> v197 + NewMigration("Add table commit_status_index", addTableCommitStatusIndex), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v195.go b/models/migrations/v195.go new file mode 100644 index 0000000000000..06694eb57df9f --- /dev/null +++ b/models/migrations/v195.go @@ -0,0 +1,47 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "fmt" + + "xorm.io/xorm" +) + +func addTableCommitStatusIndex(x *xorm.Engine) error { + // CommitStatusIndex represents a table for commit status index + type CommitStatusIndex struct { + ID int64 + RepoID int64 `xorm:"unique(repo_sha)"` + SHA string `xorm:"unique(repo_sha)"` + MaxIndex int64 `xorm:"index"` + } + + if err := x.Sync2(new(CommitStatusIndex)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + + sess := x.NewSession() + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + // Remove data we're goint to rebuild + if _, err := sess.Table("commit_status_index").Where("1=1").Delete(&CommitStatusIndex{}); err != nil { + return err + } + + // Create current data for all repositories with issues and PRs + if _, err := sess.Exec("INSERT INTO commit_status_index (repo_id, sha, max_index) " + + "SELECT max_data.repo_id, max_data.sha, max_data.max_index " + + "FROM ( SELECT commit_status.repo_id AS repo_id, commit_status.sha AS sha, max(commit_status.`index`) AS max_index " + + "FROM commit_status GROUP BY commit_status.repo_id, commit_status.sha) AS max_data"); err != nil { + return err + } + + return sess.Commit() +} From bd5947adb95ef069c477bd23a1560f624f476296 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Sep 2021 23:38:52 +0800 Subject: [PATCH 2/9] remove unused functions --- models/commit_status.go | 150 ++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index 740b864917ad1..18defed950b3c 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -42,6 +42,81 @@ func init() { db.RegisterModel(new(CommitStatus)) } +// upsertCommitStatusIndex the function will not return until it acquires the lock or receives an error. +func upsertCommitStatusIndex(e db.Engine, repoID int64, sha string) (err error) { + // An atomic UPSERT operation (INSERT/UPDATE) is the only operation + // that ensures that the key is actually locked. + switch { + case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL: + _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ + "VALUES (?,?,1) ON CONFLICT (repo_id,sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1", + repoID, sha) + case setting.Database.UseMySQL: + _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ + "VALUES (?,?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", + repoID, sha) + case setting.Database.UseMSSQL: + // https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/ + _, err = e.Exec("MERGE `commit_status_index` WITH (HOLDLOCK) as target "+ + "USING (SELECT ? AS repo_id, ? AS sha) AS src "+ + "ON src.repo_id = target.repo_id AND src.sha = target.sha "+ + "WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+ + "WHEN NOT MATCHED THEN INSERT (repo_id, sha, max_index) "+ + "VALUES (src.repo_id, src.sha, 1);", + repoID, sha) + default: + return fmt.Errorf("database type not supported") + } + return +} + +// GetNextResourceIndex retried 3 times to generate a resource index +func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { + for i := 0; i < db.MaxDupIndexAttempts; i++ { + idx, err := getNextCommitStatusIndex(repoID, sha) + if err == db.ErrResouceOutdated { + continue + } + if err != nil { + return 0, err + } + return idx, nil + } + return 0, db.ErrGetResourceIndexFailed +} + +// getNextCommitStatusIndex return the next index +func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { + ctx, commiter, err := db.TxContext() + if err != nil { + return 0, err + } + defer commiter.Close() + + var preIdx int64 + _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE group_id = ?", repoID).Get(&preIdx) + if err != nil { + return 0, err + } + + if err := upsertCommitStatusIndex(ctx.Engine(), repoID, sha); err != nil { + return 0, err + } + + var curIdx int64 + has, err := ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ? AND max_index=?", repoID, sha, preIdx+1).Get(&curIdx) + if err != nil { + return 0, err + } + if !has { + return 0, db.ErrResouceOutdated + } + if err := commiter.Commit(); err != nil { + return 0, err + } + return curIdx, nil +} + func (status *CommitStatus) loadAttributes(e db.Engine) (err error) { if status.Repo == nil { status.Repo, err = getRepositoryByID(e, status.RepoID) @@ -150,87 +225,12 @@ type CommitStatusIndex struct { MaxIndex int64 `xorm:"index"` } -// upsertCommitStatusIndex the function will not return until it acquires the lock or receives an error. -func upsertCommitStatusIndex(e db.Engine, repoID int64, sha string) (err error) { - // An atomic UPSERT operation (INSERT/UPDATE) is the only operation - // that ensures that the key is actually locked. - switch { - case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL: - _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ - "VALUES (?,?,1) ON CONFLICT (repo_id,sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1", - repoID, sha) - case setting.Database.UseMySQL: - _, err = e.Exec("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ - "VALUES (?,?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", - repoID, sha) - case setting.Database.UseMSSQL: - // https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/ - _, err = e.Exec("MERGE `commit_status_index` WITH (HOLDLOCK) as target "+ - "USING (SELECT ? AS repo_id, ? AS sha) AS src "+ - "ON src.repo_id = target.repo_id AND src.sha = target.sha "+ - "WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+ - "WHEN NOT MATCHED THEN INSERT (repo_id, sha, max_index) "+ - "VALUES (src.repo_id, src.sha, 1);", - repoID, sha) - default: - return fmt.Errorf("database type not supported") - } - return -} - -// GetNextResourceIndex retried 3 times to generate a resource index -func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { - for i := 0; i < db.MaxDupIndexAttempts; i++ { - idx, err := getNextCommitStatusIndex(repoID, sha) - if err == db.ErrResouceOutdated { - continue - } - if err != nil { - return 0, err - } - return idx, nil - } - return 0, db.ErrGetResourceIndexFailed -} - // deleteResouceIndex delete resource index func deleteCommitStatusIndex(e db.Engine, repoID int64, sha string) error { _, err := e.Exec("DELETE FROM `commit_status_index` WHERE repo_id=? AND sha=?", repoID, sha) return err } -// getNextCommitStatusIndex return the next index -func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { - ctx, commiter, err := db.TxContext() - if err != nil { - return 0, err - } - defer commiter.Close() - - var preIdx int64 - _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE group_id = ?", repoID).Get(&preIdx) - if err != nil { - return 0, err - } - - if err := upsertCommitStatusIndex(ctx.Engine(), repoID, sha); err != nil { - return 0, err - } - - var curIdx int64 - has, err := ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ? AND max_index=?", repoID, sha, preIdx+1).Get(&curIdx) - if err != nil { - return 0, err - } - if !has { - return 0, db.ErrResouceOutdated - } - if err := commiter.Commit(); err != nil { - return 0, err - } - return curIdx, nil -} - // GetLatestCommitStatus returns all statuses with a unique context for a given commit. func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { return getLatestCommitStatus(db.DefaultContext().Engine(), repoID, sha, listOptions) From 0068d746a5a01ba41af0b0f9f7fc7da1194d57dc Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 16 Sep 2021 23:45:32 +0800 Subject: [PATCH 3/9] Add fixture and test for migration --- models/fixtures/commit_status_index.yml | 5 ++ models/migrations/v195_test.go | 62 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 models/fixtures/commit_status_index.yml create mode 100644 models/migrations/v195_test.go diff --git a/models/fixtures/commit_status_index.yml b/models/fixtures/commit_status_index.yml new file mode 100644 index 0000000000000..0d5bbc015a2b4 --- /dev/null +++ b/models/fixtures/commit_status_index.yml @@ -0,0 +1,5 @@ +- + id: 1 + repo_id: 1 + sha: "1234123412341234123412341234123412341234" + max_idx: 5 \ No newline at end of file diff --git a/models/migrations/v195_test.go b/models/migrations/v195_test.go new file mode 100644 index 0000000000000..baf9cb61c2a0c --- /dev/null +++ b/models/migrations/v195_test.go @@ -0,0 +1,62 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_addTableCommitStatusIndex(t *testing.T) { + // Create the models used in the migration + type CommitStatus struct { + ID int64 `xorm:"pk autoincr"` + Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"` + RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"` + SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"` + } + + // Prepare and load the testing database + x, deferable := prepareTestEnv(t, 0, new(CommitStatus)) + if x == nil || t.Failed() { + defer deferable() + return + } + defer deferable() + + // Run the migration + if err := addTableCommitStatusIndex(x); err != nil { + assert.NoError(t, err) + return + } + + type CommitStatusIndex struct { + ID int64 + RepoID int64 `xorm:"unique(repo_sha)"` + SHA string `xorm:"unique(repo_sha)"` + MaxIndex int64 `xorm:"index"` + } + + var start = 0 + const batchSize = 1000 + for { + var indexes = make([]CommitStatusIndex, 0, batchSize) + err := x.Table("commit_status_index").Limit(batchSize, start).Find(&indexes) + assert.NoError(t, err) + + for _, idx := range indexes { + var maxIndex int + has, err := x.SQL("SELECT max(`index`) FROM commit_status WHERE repo_id = ? AND sha = ?", idx.RepoID, idx.SHA).Get(&maxIndex) + assert.NoError(t, err) + assert.True(t, has) + assert.EqualValues(t, maxIndex, idx.MaxIndex) + } + if len(indexes) < batchSize { + break + } + start += len(indexes) + } +} From 26c02e7a2a7d8e4b1fc71a0272c5c5e27e9b1df0 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 17 Sep 2021 00:06:18 +0800 Subject: [PATCH 4/9] Fix lint --- models/commit_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/commit_status.go b/models/commit_status.go index 18defed950b3c..c86b92a4cdbaf 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -70,7 +70,7 @@ func upsertCommitStatusIndex(e db.Engine, repoID int64, sha string) (err error) return } -// GetNextResourceIndex retried 3 times to generate a resource index +// GetNextCommitStatusIndex retried 3 times to generate a resource index func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { for i := 0; i < db.MaxDupIndexAttempts; i++ { idx, err := getNextCommitStatusIndex(repoID, sha) From 09cf74282f7c1f5e700e0cf2449dd38f761b0759 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 17 Sep 2021 16:24:52 +0800 Subject: [PATCH 5/9] Fix fixture --- models/fixtures/commit_status_index.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/fixtures/commit_status_index.yml b/models/fixtures/commit_status_index.yml index 0d5bbc015a2b4..3f252e87ef092 100644 --- a/models/fixtures/commit_status_index.yml +++ b/models/fixtures/commit_status_index.yml @@ -2,4 +2,4 @@ id: 1 repo_id: 1 sha: "1234123412341234123412341234123412341234" - max_idx: 5 \ No newline at end of file + max_index: 5 \ No newline at end of file From 32e6f9939d197469b6bd4c27af68e5f09e6e2469 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 22 Sep 2021 21:14:44 +0800 Subject: [PATCH 6/9] Fix lint --- models/commit_status.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index c86b92a4cdbaf..a63321bc05e76 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -225,12 +225,6 @@ type CommitStatusIndex struct { MaxIndex int64 `xorm:"index"` } -// deleteResouceIndex delete resource index -func deleteCommitStatusIndex(e db.Engine, repoID int64, sha string) error { - _, err := e.Exec("DELETE FROM `commit_status_index` WHERE repo_id=? AND sha=?", repoID, sha) - return err -} - // GetLatestCommitStatus returns all statuses with a unique context for a given commit. func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { return getLatestCommitStatus(db.DefaultContext().Engine(), repoID, sha, listOptions) From c82dde954ccd5833ebac9ed3e68ea16aed32f20b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 22 Sep 2021 21:31:07 +0800 Subject: [PATCH 7/9] Fix test --- models/commit_status.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/commit_status.go b/models/commit_status.go index a63321bc05e76..c6ed0c8eb77c9 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -40,6 +40,7 @@ type CommitStatus struct { func init() { db.RegisterModel(new(CommitStatus)) + db.RegisterModel(new(CommitStatusIndex)) } // upsertCommitStatusIndex the function will not return until it acquires the lock or receives an error. From 5ada23f0fea84be0886aa7d7b6950e98067936ba Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 22 Sep 2021 23:18:57 +0800 Subject: [PATCH 8/9] Fix bug --- models/commit_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/commit_status.go b/models/commit_status.go index c6ed0c8eb77c9..b1a53a0377597 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -95,7 +95,7 @@ func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { defer commiter.Close() var preIdx int64 - _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE group_id = ?", repoID).Get(&preIdx) + _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ?", repoID).Get(&preIdx) if err != nil { return 0, err } From ebaa9a6935487e05aec94e5791cfce9a5a41d458 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 23 Sep 2021 13:16:37 +0800 Subject: [PATCH 9/9] Fix bug --- models/commit_status.go | 4 ++-- models/pull.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index b1a53a0377597..7ec233e80d02c 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -95,7 +95,7 @@ func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { defer commiter.Close() var preIdx int64 - _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ?", repoID).Get(&preIdx) + _, err = ctx.Engine().SQL("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ?", repoID, sha).Get(&preIdx) if err != nil { return 0, err } @@ -293,7 +293,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { // Get the next Status Index idx, err := GetNextCommitStatusIndex(opts.Repo.ID, opts.SHA) if err != nil { - return fmt.Errorf("generate issue index failed: %v", err) + return fmt.Errorf("generate commit status index failed: %v", err) } ctx, committer, err := db.TxContext() diff --git a/models/pull.go b/models/pull.go index 92516735761d8..5cb7b57286f6a 100644 --- a/models/pull.go +++ b/models/pull.go @@ -450,7 +450,7 @@ func (pr *PullRequest) SetMerged() (bool, error) { func NewPullRequest(repo *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 issue index failed: %v", err) + return fmt.Errorf("generate pull request index failed: %v", err) } issue.Index = idx