diff options
author | 赵智超 <1012112796@qq.com> | 2020-07-08 03:16:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 15:16:34 -0400 |
commit | 12f9dd8fa9c02be1c38f7994066c986e50dfd2e9 (patch) | |
tree | 3113d3d93797315f5baf5790a02f69c09e98808a /models/repo.go | |
parent | 88ef04dbe9e1d4065df98e71a5914674a3bb40a3 (diff) | |
download | gitea-12f9dd8fa9c02be1c38f7994066c986e50dfd2e9.tar.gz gitea-12f9dd8fa9c02be1c38f7994066c986e50dfd2e9.zip |
Decrease the num_stars when deleting a repo (#11954)
* Decrease the num_stars when deleting a repo
fix #11949
Signed-off-by: a1012112796 <1012112796@qq.com>
* Add migration
* use batch
* Apply suggestions from code review
Co-authored-by: Lauris BH <lauris@nix.lv>
* fix lint
* fix lint
* fix ci
* fix ci2
* add doctor
* duplicate code
* fix migration
* fix some nits
* add start
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/models/repo.go b/models/repo.go index c7d2ef467c..5c6aae9557 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1576,6 +1576,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error { releaseAttachments = append(releaseAttachments, attachments[i].LocalPath()) } + if _, err = sess.Exec("UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repo.ID); err != nil { + return err + } + if err = deleteBeans(sess, &Access{RepoID: repo.ID}, &Action{RepoID: repo.ID}, @@ -2341,3 +2345,38 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error { func UpdateRepositoryCols(repo *Repository, cols ...string) error { return updateRepositoryCols(x, repo, cols...) } + +// DoctorUserStarNum recalculate Stars number for all user +func DoctorUserStarNum() (err error) { + const batchSize = 100 + sess := x.NewSession() + defer sess.Close() + + for start := 0; ; start += batchSize { + users := make([]User, 0, batchSize) + if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil { + return + } + if len(users) == 0 { + break + } + + if err = sess.Begin(); err != nil { + return + } + + for _, user := range users { + if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil { + return + } + } + + if err = sess.Commit(); err != nil { + return + } + } + + log.Debug("recalculate Stars number for all user finished") + + return +} |