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 5 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
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
46 changes: 46 additions & 0 deletions models/migrations/v143.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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/models"
"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.

const batchSize = 100
sess := x.NewSession()
defer sess.Close()

for start := 0; ; start += batchSize {
userIDs := make([]int64, 0, batchSize)
if err = sess.Table("user").Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&userIDs); err != nil {
return err
}
if len(userIDs) == 0 {
break
}

var number int64

for _, uid := range userIDs {
if number, err = x.Where("uid = ?", uid).Count(new(models.Star)); err != nil {
return
}

if _, err = x.Exec("UPDATE `user` SET num_stars=? WHERE id = ?", number, uid); err != nil {
return err
}
}
}

log.Debug("recalculate Stars number for all user finished")

return sess.Commit()
}
4 changes: 4 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