Skip to content

Commit 1e35e98

Browse files
a1012112796lafrikszeripathlunny
authored and
Yohann Delafollye
committed
Decrease the num_stars when deleting a repo (go-gitea#11954)
* Decrease the num_stars when deleting a repo fix go-gitea#11949 Signed-off-by: a1012112796 <[email protected]> * Add migration * use batch * Apply suggestions from code review Co-authored-by: Lauris BH <[email protected]> * fix lint * fix lint * fix ci * fix ci2 * add doctor * duplicate code * fix migration * fix some nits * add start Co-authored-by: Lauris BH <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 409a8ae commit 1e35e98

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

cmd/doctor.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ var checklist = []check{
120120
isDefault: false,
121121
f: runDoctorPRMergeBase,
122122
},
123+
{
124+
title: "Recalculate Stars number for all user",
125+
name: "recalculate_stars_number",
126+
isDefault: false,
127+
f: runDoctorUserStarNum,
128+
},
123129
// more checks please append here
124130
}
125131

@@ -494,6 +500,10 @@ func runDoctorPRMergeBase(ctx *cli.Context) ([]string, error) {
494500
return results, err
495501
}
496502

503+
func runDoctorUserStarNum(ctx *cli.Context) ([]string, error) {
504+
return nil, models.DoctorUserStarNum()
505+
}
506+
497507
func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
498508
path, err := exec.LookPath(setting.ScriptType)
499509
if err != nil {

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ var migrations = []Migration{
218218
NewMigration("Add KeepActivityPrivate to User table", addKeepActivityPrivateUserColumn),
219219
// v142 -> v143
220220
NewMigration("Ensure Repository.IsArchived is not null", setIsArchivedToFalse),
221+
// v143 -> v144
222+
NewMigration("recalculate Stars number for all user", recalculateStars),
221223
}
222224

223225
// GetCurrentDBVersion returns the current db version

models/migrations/v143.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
5+
package migrations
6+
7+
import (
8+
"code.gitea.io/gitea/modules/log"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func recalculateStars(x *xorm.Engine) (err error) {
14+
// because of issue https://github.com/go-gitea/gitea/issues/11949,
15+
// recalculate Stars number for all users to fully fix it.
16+
17+
type User struct {
18+
ID int64 `xorm:"pk autoincr"`
19+
}
20+
21+
const batchSize = 100
22+
sess := x.NewSession()
23+
defer sess.Close()
24+
25+
for start := 0; ; start += batchSize {
26+
users := make([]User, 0, batchSize)
27+
if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
28+
return
29+
}
30+
if len(users) == 0 {
31+
break
32+
}
33+
34+
if err = sess.Begin(); err != nil {
35+
return
36+
}
37+
38+
for _, user := range users {
39+
if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
40+
return
41+
}
42+
}
43+
44+
if err = sess.Commit(); err != nil {
45+
return
46+
}
47+
}
48+
49+
log.Debug("recalculate Stars number for all user finished")
50+
51+
return
52+
}

models/repo.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
15761576
releaseAttachments = append(releaseAttachments, attachments[i].LocalPath())
15771577
}
15781578

1579+
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 {
1580+
return err
1581+
}
1582+
15791583
if err = deleteBeans(sess,
15801584
&Access{RepoID: repo.ID},
15811585
&Action{RepoID: repo.ID},
@@ -2341,3 +2345,38 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
23412345
func UpdateRepositoryCols(repo *Repository, cols ...string) error {
23422346
return updateRepositoryCols(x, repo, cols...)
23432347
}
2348+
2349+
// DoctorUserStarNum recalculate Stars number for all user
2350+
func DoctorUserStarNum() (err error) {
2351+
const batchSize = 100
2352+
sess := x.NewSession()
2353+
defer sess.Close()
2354+
2355+
for start := 0; ; start += batchSize {
2356+
users := make([]User, 0, batchSize)
2357+
if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
2358+
return
2359+
}
2360+
if len(users) == 0 {
2361+
break
2362+
}
2363+
2364+
if err = sess.Begin(); err != nil {
2365+
return
2366+
}
2367+
2368+
for _, user := range users {
2369+
if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
2370+
return
2371+
}
2372+
}
2373+
2374+
if err = sess.Commit(); err != nil {
2375+
return
2376+
}
2377+
}
2378+
2379+
log.Debug("recalculate Stars number for all user finished")
2380+
2381+
return
2382+
}

models/repo_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ func TestDeleteAvatar(t *testing.T) {
187187

188188
assert.Equal(t, "", repo.Avatar)
189189
}
190+
191+
func TestDoctorUserStarNum(t *testing.T) {
192+
assert.NoError(t, PrepareTestDatabase())
193+
194+
assert.NoError(t, DoctorUserStarNum())
195+
}

0 commit comments

Comments
 (0)