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