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
Show file tree
Hide file tree
Changes from 3 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 @@ -306,6 +306,8 @@ var migrations = []Migration{
NewMigration("Add columns to user and repository", addUserRepoMissingColumns),
// v125 -> v126
NewMigration("Add some columns on review for migration", addReviewMigrateInfo),
// v126 -> v127
NewMigration("Fix topic repository count", fixTopicRepositoryCount),
}

// Migrate database to current version
Expand Down
25 changes: 25 additions & 0 deletions models/migrations/v126.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 (
"xorm.io/builder"
"xorm.io/xorm"
)

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

_, 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
}
6 changes: 6 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,12 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return err
}

if len(repo.Topics) > 0 {
if err = removeTopicsFromRepo(sess, repo.ID); err != nil {
return err
}
}

// FIXME: Remove repository files should be executed after transaction succeed.
repoPath := repo.RepoPath()
removeAllWithNotice(sess, "Delete repository files", repoPath)
Expand Down
24 changes: 21 additions & 3 deletions models/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func addTopicByNameToRepo(e Engine, repoID int64, topicName string) (*Topic, err
}

// removeTopicFromRepo remove a topic from a repo and decrements the topic repo count
func removeTopicFromRepo(repoID int64, topic *Topic, e Engine) error {
func removeTopicFromRepo(e Engine, repoID int64, topic *Topic) error {
topic.RepoCount--
if _, err := e.ID(topic.ID).Cols("repo_count").Update(topic); err != nil {
return err
Expand All @@ -145,6 +145,24 @@ func removeTopicFromRepo(repoID int64, topic *Topic, e Engine) error {
return nil
}

// removeTopicsFromRepo remove all topics from the repo and decrements respective topics repo count
func removeTopicsFromRepo(e Engine, repoID int64) error {
_, err := e.Where(
builder.In("id",
builder.Select("topic_id").From("repo_topic").Where(builder.Eq{"repo_id": repoID}),
),
).Cols("repo_count").SetExpr("repo_count", "repo_count-1").Update(&Topic{})
if err != nil {
return err
}

if _, err = e.Delete(&RepoTopic{RepoID: repoID}); err != nil {
return err
}

return nil
}

// FindTopicOptions represents the options when fdin topics
type FindTopicOptions struct {
ListOptions
Expand Down Expand Up @@ -216,7 +234,7 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
return nil, nil
}

err = removeTopicFromRepo(repoID, topic, x)
err = removeTopicFromRepo(x, repoID, topic)

return topic, err
}
Expand Down Expand Up @@ -277,7 +295,7 @@ func SaveTopics(repoID int64, topicNames ...string) error {
}

for _, topic := range removeTopics {
err := removeTopicFromRepo(repoID, topic, sess)
err := removeTopicFromRepo(sess, repoID, topic)
if err != nil {
return err
}
Expand Down