diff options
author | Lauris BH <lauris@nix.lv> | 2020-01-31 08:57:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-31 08:57:19 +0200 |
commit | b3d8e2d4f7a0ebf768ab6bcb102755839a6b9311 (patch) | |
tree | d06d78f4a74aa5887360442397531d61199bf805 /models/topic.go | |
parent | 159732dcb7420699245ef8410fff22002653d5b0 (diff) | |
download | gitea-b3d8e2d4f7a0ebf768ab6bcb102755839a6b9311.tar.gz gitea-b3d8e2d4f7a0ebf768ab6bcb102755839a6b9311.zip |
Update topics repo count when deleting repository (#10051)
* Update topics repo count when deleting repository
* Add migration to fix incorrect data
* Optimize to use single update to recalculate values
Diffstat (limited to 'models/topic.go')
-rw-r--r-- | models/topic.go | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/models/topic.go b/models/topic.go index 1a76c49156..4a5bffa08a 100644 --- a/models/topic.go +++ b/models/topic.go @@ -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 @@ -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 @@ -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 } @@ -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 } |