2020-07-08 00:46:34 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-07-08 00:46:34 +05:30
|
|
|
|
2022-11-02 14:24:36 +05:30
|
|
|
package v1_13 //nolint
|
2020-07-08 00:46:34 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
2022-11-02 14:24:36 +05:30
|
|
|
func RecalculateStars(x *xorm.Engine) (err error) {
|
2020-07-08 00:46:34 +05:30
|
|
|
// because of issue https://github.com/go-gitea/gitea/issues/11949,
|
|
|
|
// recalculate Stars number for all users to fully fix it.
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const batchSize = 100
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
for start := 0; ; start += batchSize {
|
|
|
|
users := make([]User, 0, batchSize)
|
2023-07-07 11:01:56 +05:30
|
|
|
if err := sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
|
|
|
|
return err
|
2020-07-08 00:46:34 +05:30
|
|
|
}
|
|
|
|
if len(users) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:01:56 +05:30
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
2020-07-08 00:46:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range users {
|
2023-07-07 11:01:56 +05:30
|
|
|
if _, err := sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
|
|
|
|
return err
|
2020-07-08 00:46:34 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:01:56 +05:30
|
|
|
if err := sess.Commit(); err != nil {
|
|
|
|
return err
|
2020-07-08 00:46:34 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("recalculate Stars number for all user finished")
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return err
|
2020-07-08 00:46:34 +05:30
|
|
|
}
|