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

Update topics repo count when deleting repository #10051

Merged
merged 4 commits into from
Jan 31, 2020
Merged
Changes from 1 commit
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
38 changes: 7 additions & 31 deletions models/migrations/v126.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,16 @@ import (
)

func fixTopicRepositoryCount(x *xorm.Engine) error {
type Topic struct {
ID int64
RepoCount int
}

_, err := x.Exec(builder.Delete(builder.NotIn("`repo_id`", builder.Select("`id`").From("`repository`"))).From("`repo_topic`"))
if err != nil {
return err
}

var last int
const batchSize = 50
for {
var results = make([]Topic, 0, batchSize)
err := x.OrderBy("id").
Limit(batchSize, last).
Find(&results)
if err != nil {
return err
}
if len(results) == 0 {
break
}
last += len(results)

for _, res := range results {
_, err := x.ID(res.ID).Cols("repo_count").
SetExpr("repo_count", builder.Select("count(*)").From("repo_topic").Where(
builder.Eq{"topic_id": res.ID},
)).Update(res)
if err != nil {
return err
}
}
}
return nil
_, err = x.Exec(builder.Update(
builder.Eq{
"`repo_count`": builder.Select("count(*)").From("`repo_topic`").Where(builder.Eq{
"`repo_topic`.`topic_id`": builder.Expr("`topic`.`id`"),
}),
}).From("`topic`").Where(builder.Eq{"'1'": "1"}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such cunning evilness

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to see if it can be done 😆

return err
}