Skip to content

Commit 6a081f9

Browse files
a1012112796lafrikszeripathlunny
authoredJul 8, 2020
Decrease the num_stars when deleting a repo (#11954) (#12188)
* 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>
1 parent c3c246c commit 6a081f9

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
 

‎cmd/doctor.go

+10
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/repo.go

+39
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
15671567
releaseAttachments = append(releaseAttachments, attachments[i].LocalPath())
15681568
}
15691569

1570+
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 {
1571+
return err
1572+
}
1573+
15701574
if err = deleteBeans(sess,
15711575
&Access{RepoID: repo.ID},
15721576
&Action{RepoID: repo.ID},
@@ -2332,3 +2336,38 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
23322336
func UpdateRepositoryCols(repo *Repository, cols ...string) error {
23332337
return updateRepositoryCols(x, repo, cols...)
23342338
}
2339+
2340+
// DoctorUserStarNum recalculate Stars number for all user
2341+
func DoctorUserStarNum() (err error) {
2342+
const batchSize = 100
2343+
sess := x.NewSession()
2344+
defer sess.Close()
2345+
2346+
for start := 0; ; start += batchSize {
2347+
users := make([]User, 0, batchSize)
2348+
if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
2349+
return
2350+
}
2351+
if len(users) == 0 {
2352+
break
2353+
}
2354+
2355+
if err = sess.Begin(); err != nil {
2356+
return
2357+
}
2358+
2359+
for _, user := range users {
2360+
if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
2361+
return
2362+
}
2363+
}
2364+
2365+
if err = sess.Commit(); err != nil {
2366+
return
2367+
}
2368+
}
2369+
2370+
log.Debug("recalculate Stars number for all user finished")
2371+
2372+
return
2373+
}

‎models/repo_test.go

+6
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)
Please sign in to comment.