]> source.dussan.org Git - gitea.git/commitdiff
Decrease the num_stars when deleting a repo (#11954) (#12188)
author赵智超 <1012112796@qq.com>
Wed, 8 Jul 2020 21:52:40 +0000 (05:52 +0800)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2020 21:52:40 +0000 (17:52 -0400)
* 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>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
cmd/doctor.go
models/repo.go
models/repo_test.go

index 45a50c82660d4fbb78859b762c4b5ededb43be5f..48fd3158a49f4c853dfa08c3ea51fe6028d5ae35 100644 (file)
@@ -120,6 +120,12 @@ var checklist = []check{
                isDefault: false,
                f:         runDoctorPRMergeBase,
        },
+       {
+               title:     "Recalculate Stars number for all user",
+               name:      "recalculate_stars_number",
+               isDefault: false,
+               f:         runDoctorUserStarNum,
+       },
        // more checks please append here
 }
 
@@ -494,6 +500,10 @@ func runDoctorPRMergeBase(ctx *cli.Context) ([]string, error) {
        return results, err
 }
 
+func runDoctorUserStarNum(ctx *cli.Context) ([]string, error) {
+       return nil, models.DoctorUserStarNum()
+}
+
 func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
        path, err := exec.LookPath(setting.ScriptType)
        if err != nil {
index a11c54fb00f797c1f311d33c31b8745f1290a120..51ea558c3728f738d117a4e4070d70d737cb4db4 100644 (file)
@@ -1567,6 +1567,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},
@@ -2332,3 +2336,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
+}
index 20da43fbbfc916812983818d0f1f870f24fa0be0..045f94670bde08c725d87957a26f60e2b0c9eb08 100644 (file)
@@ -187,3 +187,9 @@ func TestDeleteAvatar(t *testing.T) {
 
        assert.Equal(t, "", repo.Avatar)
 }
+
+func TestDoctorUserStarNum(t *testing.T) {
+       assert.NoError(t, PrepareTestDatabase())
+
+       assert.NoError(t, DoctorUserStarNum())
+}