Skip to content

Commit e406dc0

Browse files
lunnytechknowlogick
authored andcommitted
Fix repository deletion when there is large number of issues in it (#5426) (#5434)
1 parent 328e38e commit e406dc0

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

models/repo.go

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/Unknwon/cae/zip"
3434
"github.com/Unknwon/com"
35+
"github.com/go-xorm/builder"
3536
"github.com/go-xorm/xorm"
3637
"github.com/mcuadros/go-version"
3738
"gopkg.in/ini.v1"
@@ -1841,51 +1842,51 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
18411842
return fmt.Errorf("deleteBeans: %v", err)
18421843
}
18431844

1844-
// Delete comments and attachments.
1845-
issueIDs := make([]int64, 0, 25)
1846-
attachmentPaths := make([]string, 0, len(issueIDs))
1847-
if err = sess.
1848-
Table("issue").
1849-
Cols("id").
1850-
Where("repo_id=?", repoID).
1851-
Find(&issueIDs); err != nil {
1845+
deleteCond := builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repoID})
1846+
// Delete comments and attachments
1847+
if _, err = sess.In("issue_id", deleteCond).
1848+
Delete(&Comment{}); err != nil {
18521849
return err
18531850
}
18541851

1855-
if len(issueIDs) > 0 {
1856-
if _, err = sess.In("issue_id", issueIDs).Delete(&Comment{}); err != nil {
1857-
return err
1858-
}
1859-
if _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{}); err != nil {
1860-
return err
1861-
}
1862-
if _, err = sess.In("issue_id", issueIDs).Delete(&Reaction{}); err != nil {
1863-
return err
1864-
}
1865-
if _, err = sess.In("issue_id", issueIDs).Delete(&IssueWatch{}); err != nil {
1866-
return err
1867-
}
1868-
if _, err = sess.In("issue_id", issueIDs).Delete(&Stopwatch{}); err != nil {
1869-
return err
1870-
}
1852+
if _, err = sess.In("issue_id", deleteCond).
1853+
Delete(&IssueUser{}); err != nil {
1854+
return err
1855+
}
18711856

1872-
attachments := make([]*Attachment, 0, 5)
1873-
if err = sess.
1874-
In("issue_id", issueIDs).
1875-
Find(&attachments); err != nil {
1876-
return err
1877-
}
1878-
for j := range attachments {
1879-
attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
1880-
}
1857+
if _, err = sess.In("issue_id", deleteCond).
1858+
Delete(&Reaction{}); err != nil {
1859+
return err
1860+
}
18811861

1882-
if _, err = sess.In("issue_id", issueIDs).Delete(&Attachment{}); err != nil {
1883-
return err
1884-
}
1862+
if _, err = sess.In("issue_id", deleteCond).
1863+
Delete(&IssueWatch{}); err != nil {
1864+
return err
1865+
}
18851866

1886-
if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
1887-
return err
1888-
}
1867+
if _, err = sess.In("issue_id", deleteCond).
1868+
Delete(&Stopwatch{}); err != nil {
1869+
return err
1870+
}
1871+
1872+
attachmentPaths := make([]string, 0, 20)
1873+
attachments := make([]*Attachment, 0, len(attachmentPaths))
1874+
if err = sess.Join("INNER", "issue", "issue.id = attachment.issue_id").
1875+
Where("issue.repo_id = ?", repoID).
1876+
Find(&attachments); err != nil {
1877+
return err
1878+
}
1879+
for j := range attachments {
1880+
attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
1881+
}
1882+
1883+
if _, err = sess.In("issue_id", deleteCond).
1884+
Delete(&Attachment{}); err != nil {
1885+
return err
1886+
}
1887+
1888+
if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
1889+
return err
18891890
}
18901891

18911892
if _, err = sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil {

0 commit comments

Comments
 (0)