Skip to content

Commit e4a155c

Browse files
lunnysilverwind
andcommitted
Fix generate index failure possibility on postgres (go-gitea#21998)
@wxiaoguang Please review Co-authored-by: silverwind <[email protected]>
1 parent b7db20f commit e4a155c

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

models/db/index.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"strconv"
12+
13+
"code.gitea.io/gitea/modules/setting"
1114
)
1215

1316
// ResourceIndex represents a resource index which could be used as issue/release and others
@@ -56,8 +59,25 @@ func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxInd
5659
return nil
5760
}
5861

62+
func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
63+
res, err := GetEngine(ctx).Query(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
64+
"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1 RETURNING max_index",
65+
tableName, tableName), groupID)
66+
if err != nil {
67+
return 0, err
68+
}
69+
if len(res) == 0 {
70+
return 0, ErrGetResourceIndexFailed
71+
}
72+
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
73+
}
74+
5975
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
6076
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
77+
if setting.Database.UsePostgreSQL {
78+
return postgresGetNextResourceIndex(ctx, tableName, groupID)
79+
}
80+
6181
e := GetEngine(ctx)
6282

6383
// try to update the max_index to next value, and acquire the write-lock for the record

models/git/commit_status.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"net/url"
13+
"strconv"
1314
"strings"
1415
"time"
1516

@@ -50,8 +51,25 @@ func init() {
5051
db.RegisterModel(new(CommitStatusIndex))
5152
}
5253

54+
func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
55+
res, err := db.GetEngine(ctx).Query("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+
56+
"VALUES (?,?,1) ON CONFLICT (repo_id, sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1 RETURNING max_index",
57+
repoID, sha)
58+
if err != nil {
59+
return 0, err
60+
}
61+
if len(res) == 0 {
62+
return 0, db.ErrGetResourceIndexFailed
63+
}
64+
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
65+
}
66+
5367
// GetNextCommitStatusIndex retried 3 times to generate a resource index
5468
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
69+
if setting.Database.UsePostgreSQL {
70+
return postgresGetCommitStatusIndex(ctx, repoID, sha)
71+
}
72+
5573
e := db.GetEngine(ctx)
5674

5775
// try to update the max_index to next value, and acquire the write-lock for the record

tests/integration/repo_commits_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func TestRepoCommitsStatusParallel(t *testing.T) {
136136
var wg sync.WaitGroup
137137
for i := 0; i < 10; i++ {
138138
wg.Add(1)
139-
go func(t *testing.T, i int) {
140-
t.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
139+
go func(parentT *testing.T, i int) {
140+
parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
141141
runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending"))
142142
runBody(t)
143143
wg.Done()

0 commit comments

Comments
 (0)