Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease the num_stars when deleting a repo #11954

Merged
merged 24 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions models/migrations/v143.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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"
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
)

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
}

a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
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
}
35 changes: 35 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,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},
Expand Down Expand Up @@ -2351,3 +2355,34 @@ 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
}

for _, user := range users {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
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
}
6 changes: 6 additions & 0 deletions models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}