From 12f9dd8fa9c02be1c38f7994066c986e50dfd2e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E6=99=BA=E8=B6=85?= <1012112796@qq.com>
Date: Wed, 8 Jul 2020 03:16:34 +0800
Subject: [PATCH] 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>
---
 cmd/doctor.go                   | 10 +++++++
 models/migrations/migrations.go |  2 ++
 models/migrations/v143.go       | 52 +++++++++++++++++++++++++++++++++
 models/repo.go                  | 39 +++++++++++++++++++++++++
 models/repo_test.go             |  6 ++++
 5 files changed, 109 insertions(+)
 create mode 100644 models/migrations/v143.go

diff --git a/cmd/doctor.go b/cmd/doctor.go
index 45a50c8266..48fd3158a4 100644
--- a/cmd/doctor.go
+++ b/cmd/doctor.go
@@ -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 {
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index 6cc80249a3..d205794e1f 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -218,6 +218,8 @@ var migrations = []Migration{
 	NewMigration("Add KeepActivityPrivate to User table", addKeepActivityPrivateUserColumn),
 	// v142 -> v143
 	NewMigration("Ensure Repository.IsArchived is not null", setIsArchivedToFalse),
+	// v143 -> v144
+	NewMigration("recalculate Stars number for all user", recalculateStars),
 }
 
 // GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v143.go b/models/migrations/v143.go
new file mode 100644
index 0000000000..93237ebfcd
--- /dev/null
+++ b/models/migrations/v143.go
@@ -0,0 +1,52 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+	"code.gitea.io/gitea/modules/log"
+
+	"xorm.io/xorm"
+)
+
+func recalculateStars(x *xorm.Engine) (err error) {
+	// 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)
+		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
+}
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
+}
diff --git a/models/repo_test.go b/models/repo_test.go
index 20da43fbbf..045f94670b 100644
--- a/models/repo_test.go
+++ b/models/repo_test.go
@@ -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())
+}