Update topics repo count when deleting repository (#10081)

This commit is contained in:
Lauris BH 2020-01-31 10:04:49 +02:00 committed by GitHub
parent b3549bb5ec
commit 912ce27421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -1910,6 +1910,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(sess)
removeAllWithNotice(sess, "Delete repository files", repoPath)

View File

@ -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 {
RepoID int64
@ -217,7 +235,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
}
@ -278,7 +296,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
}