diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-06-05 18:33:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-05 10:33:47 +0000 |
commit | 315124b4693dd56d94d52cda3015611184a49a27 (patch) | |
tree | 273ea4fcb2c1bff7409834477d2a7287c7580ce1 /models/db | |
parent | 3d1fda737bc36e80a670f856fd85f6b884872b73 (diff) | |
download | gitea-315124b4693dd56d94d52cda3015611184a49a27.tar.gz gitea-315124b4693dd56d94d52cda3015611184a49a27.zip |
Fix parallelly generating index failure with Mysql (#24567)
Diffstat (limited to 'models/db')
-rw-r--r-- | models/db/index.go | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/models/db/index.go b/models/db/index.go index 7609d8fb6e..259ddd6ade 100644 --- a/models/db/index.go +++ b/models/db/index.go @@ -71,10 +71,31 @@ func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID return strconv.ParseInt(string(res[0]["max_index"]), 10, 64) } +func mysqlGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) { + if _, err := GetEngine(ctx).Exec(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ + "VALUES (?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", + tableName), groupID); err != nil { + return 0, err + } + + var idx int64 + _, err := GetEngine(ctx).SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&idx) + if err != nil { + return 0, err + } + if idx == 0 { + return 0, errors.New("cannot get the correct index") + } + return idx, nil +} + // GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) { - if setting.Database.Type.IsPostgreSQL() { + switch { + case setting.Database.Type.IsPostgreSQL(): return postgresGetNextResourceIndex(ctx, tableName, groupID) + case setting.Database.Type.IsMySQL(): + return mysqlGetNextResourceIndex(ctx, tableName, groupID) } e := GetEngine(ctx) |