summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2018-11-30 22:38:35 +0800
committertechknowlogick <hello@techknowlogick.com>2018-11-30 09:38:35 -0500
commite406dc058dc1dacb5c481485644c2e2844170b9a (patch)
treea58dad94562148271f92104c4a9ff45ef8567c3b
parent328e38ebc7b824ab08a21638e32c2c51c4b27d7b (diff)
downloadgitea-e406dc058dc1dacb5c481485644c2e2844170b9a.tar.gz
gitea-e406dc058dc1dacb5c481485644c2e2844170b9a.zip
Fix repository deletion when there is large number of issues in it (#5426) (#5434)
-rw-r--r--models/repo.go79
1 files changed, 40 insertions, 39 deletions
diff --git a/models/repo.go b/models/repo.go
index 7211855323..fbbd334c48 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -32,6 +32,7 @@ import (
"github.com/Unknwon/cae/zip"
"github.com/Unknwon/com"
+ "github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
"github.com/mcuadros/go-version"
"gopkg.in/ini.v1"
@@ -1841,51 +1842,51 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return fmt.Errorf("deleteBeans: %v", err)
}
- // Delete comments and attachments.
- issueIDs := make([]int64, 0, 25)
- attachmentPaths := make([]string, 0, len(issueIDs))
- if err = sess.
- Table("issue").
- Cols("id").
- Where("repo_id=?", repoID).
- Find(&issueIDs); err != nil {
+ deleteCond := builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repoID})
+ // Delete comments and attachments
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&Comment{}); err != nil {
return err
}
- if len(issueIDs) > 0 {
- if _, err = sess.In("issue_id", issueIDs).Delete(&Comment{}); err != nil {
- return err
- }
- if _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{}); err != nil {
- return err
- }
- if _, err = sess.In("issue_id", issueIDs).Delete(&Reaction{}); err != nil {
- return err
- }
- if _, err = sess.In("issue_id", issueIDs).Delete(&IssueWatch{}); err != nil {
- return err
- }
- if _, err = sess.In("issue_id", issueIDs).Delete(&Stopwatch{}); err != nil {
- return err
- }
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&IssueUser{}); err != nil {
+ return err
+ }
- attachments := make([]*Attachment, 0, 5)
- if err = sess.
- In("issue_id", issueIDs).
- Find(&attachments); err != nil {
- return err
- }
- for j := range attachments {
- attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
- }
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&Reaction{}); err != nil {
+ return err
+ }
- if _, err = sess.In("issue_id", issueIDs).Delete(&Attachment{}); err != nil {
- return err
- }
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&IssueWatch{}); err != nil {
+ return err
+ }
- if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
- return err
- }
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&Stopwatch{}); err != nil {
+ return err
+ }
+
+ attachmentPaths := make([]string, 0, 20)
+ attachments := make([]*Attachment, 0, len(attachmentPaths))
+ if err = sess.Join("INNER", "issue", "issue.id = attachment.issue_id").
+ Where("issue.repo_id = ?", repoID).
+ Find(&attachments); err != nil {
+ return err
+ }
+ for j := range attachments {
+ attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
+ }
+
+ if _, err = sess.In("issue_id", deleteCond).
+ Delete(&Attachment{}); err != nil {
+ return err
+ }
+
+ if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
+ return err
}
if _, err = sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil {