You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "code.gitea.io/gitea/modules/log"
  7. "xorm.io/xorm"
  8. )
  9. func recalculateStars(x *xorm.Engine) (err error) {
  10. // because of issue https://github.com/go-gitea/gitea/issues/11949,
  11. // recalculate Stars number for all users to fully fix it.
  12. type User struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. }
  15. const batchSize = 100
  16. sess := x.NewSession()
  17. defer sess.Close()
  18. for start := 0; ; start += batchSize {
  19. users := make([]User, 0, batchSize)
  20. if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
  21. return
  22. }
  23. if len(users) == 0 {
  24. break
  25. }
  26. if err = sess.Begin(); err != nil {
  27. return
  28. }
  29. for _, user := range users {
  30. if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
  31. return
  32. }
  33. }
  34. if err = sess.Commit(); err != nil {
  35. return
  36. }
  37. }
  38. log.Debug("recalculate Stars number for all user finished")
  39. return
  40. }