aboutsummaryrefslogtreecommitdiffstats
path: root/models/git
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-12-02 11:15:36 +0800
committerGitHub <noreply@github.com>2022-12-02 11:15:36 +0800
commitf7ade6de7c1c4991c650d6b96e6407053e6bdae7 (patch)
tree35a935e1d99b8254a892524dfd1ec6d27f492959 /models/git
parent64973cf18fcd21179fb7612b15914691ee24c657 (diff)
downloadgitea-f7ade6de7c1c4991c650d6b96e6407053e6bdae7.tar.gz
gitea-f7ade6de7c1c4991c650d6b96e6407053e6bdae7.zip
Fix generate index failure possibility on postgres (#21998)
@wxiaoguang Please review Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'models/git')
-rw-r--r--models/git/commit_status.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/models/git/commit_status.go b/models/git/commit_status.go
index 6353bb2fa9..928f1771fe 100644
--- a/models/git/commit_status.go
+++ b/models/git/commit_status.go
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"net/url"
+ "strconv"
"strings"
"time"
@@ -49,8 +50,25 @@ func init() {
db.RegisterModel(new(CommitStatusIndex))
}
+func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
+ res, err := db.GetEngine(ctx).Query("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 RETURNING max_index",
+ repoID, sha)
+ if err != nil {
+ return 0, err
+ }
+ if len(res) == 0 {
+ return 0, db.ErrGetResourceIndexFailed
+ }
+ return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
+}
+
// GetNextCommitStatusIndex retried 3 times to generate a resource index
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
+ if setting.Database.UsePostgreSQL {
+ return postgresGetCommitStatusIndex(ctx, repoID, sha)
+ }
+
e := db.GetEngine(ctx)
// try to update the max_index to next value, and acquire the write-lock for the record