@@ -63,18 +63,40 @@ func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string)
63
63
return strconv .ParseInt (string (res [0 ]["max_index" ]), 10 , 64 )
64
64
}
65
65
66
+ func mysqlGetCommitStatusIndex (ctx context.Context , repoID int64 , sha string ) (int64 , error ) {
67
+ if _ , err := db .GetEngine (ctx ).Exec ("INSERT INTO `commit_status_index` (repo_id, sha, max_index) " +
68
+ "VALUES (?,?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1" ,
69
+ repoID , sha ); err != nil {
70
+ return 0 , err
71
+ }
72
+
73
+ var idx int64
74
+ _ , err := db .GetEngine (ctx ).SQL ("SELECT max_index FROM `commit_status_index` WHERE repo_id = ? AND sha = ?" ,
75
+ repoID , sha ).Get (& idx )
76
+ if err != nil {
77
+ return 0 , err
78
+ }
79
+ if idx == 0 {
80
+ return 0 , errors .New ("cannot get the correct index" )
81
+ }
82
+ return idx , nil
83
+ }
84
+
66
85
// GetNextCommitStatusIndex retried 3 times to generate a resource index
67
86
func GetNextCommitStatusIndex (ctx context.Context , repoID int64 , sha string ) (int64 , error ) {
68
- if setting .Database .Type .IsPostgreSQL () {
87
+ switch {
88
+ case setting .Database .Type .IsPostgreSQL ():
69
89
return postgresGetCommitStatusIndex (ctx , repoID , sha )
90
+ case setting .Database .Type .IsMySQL ():
91
+ return mysqlGetCommitStatusIndex (ctx , repoID , sha )
70
92
}
71
93
72
94
e := db .GetEngine (ctx )
73
95
74
96
// try to update the max_index to next value, and acquire the write-lock for the record
75
97
res , err := e .Exec ("UPDATE `commit_status_index` SET max_index=max_index+1 WHERE repo_id=? AND sha=?" , repoID , sha )
76
98
if err != nil {
77
- return 0 , err
99
+ return 0 , fmt . Errorf ( "update failed: %w" , err )
78
100
}
79
101
affected , err := res .RowsAffected ()
80
102
if err != nil {
@@ -85,26 +107,26 @@ func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (in
85
107
_ , errIns := e .Exec ("INSERT INTO `commit_status_index` (repo_id, sha, max_index) VALUES (?, ?, 0)" , repoID , sha )
86
108
res , err = e .Exec ("UPDATE `commit_status_index` SET max_index=max_index+1 WHERE repo_id=? AND sha=?" , repoID , sha )
87
109
if err != nil {
88
- return 0 , err
110
+ return 0 , fmt . Errorf ( "update2 failed: %w" , err )
89
111
}
90
112
affected , err = res .RowsAffected ()
91
113
if err != nil {
92
- return 0 , err
114
+ return 0 , fmt . Errorf ( "RowsAffected failed: %w" , err )
93
115
}
94
116
// if the update still can not update any records, the record must not exist and there must be some errors (insert error)
95
117
if affected == 0 {
96
118
if errIns == nil {
97
119
return 0 , errors .New ("impossible error when GetNextCommitStatusIndex, insert and update both succeeded but no record is updated" )
98
120
}
99
- return 0 , errIns
121
+ return 0 , fmt . Errorf ( "insert failed: %w" , errIns )
100
122
}
101
123
}
102
124
103
125
// now, the new index is in database (protected by the transaction and write-lock)
104
126
var newIdx int64
105
127
has , err := e .SQL ("SELECT max_index FROM `commit_status_index` WHERE repo_id=? AND sha=?" , repoID , sha ).Get (& newIdx )
106
128
if err != nil {
107
- return 0 , err
129
+ return 0 , fmt . Errorf ( "select failed: %w" , err )
108
130
}
109
131
if ! has {
110
132
return 0 , errors .New ("impossible error when GetNextCommitStatusIndex, upsert succeeded but no record can be selected" )
0 commit comments