Skip to content

Commit 9cb08a3

Browse files
authored
fix wrong num of user repos because of duplicated click delete button & performance optimization (#1092)
1 parent 19bc2b1 commit 9cb08a3

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

models/repo.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,14 +1504,6 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) {
15041504

15051505
// DeleteRepository deletes a repository for a user or organization.
15061506
func DeleteRepository(uid, repoID int64) error {
1507-
repo := &Repository{ID: repoID, OwnerID: uid}
1508-
has, err := x.Get(repo)
1509-
if err != nil {
1510-
return err
1511-
} else if !has {
1512-
return ErrRepoNotExist{repoID, uid, ""}
1513-
}
1514-
15151507
// In case is a organization.
15161508
org, err := GetUserByID(uid)
15171509
if err != nil {
@@ -1529,6 +1521,20 @@ func DeleteRepository(uid, repoID int64) error {
15291521
return err
15301522
}
15311523

1524+
repo := &Repository{ID: repoID, OwnerID: uid}
1525+
has, err := sess.Get(repo)
1526+
if err != nil {
1527+
return err
1528+
} else if !has {
1529+
return ErrRepoNotExist{repoID, uid, ""}
1530+
}
1531+
1532+
if cnt, err := sess.Id(repoID).Delete(&Repository{}); err != nil {
1533+
return err
1534+
} else if cnt != 1 {
1535+
return ErrRepoNotExist{repoID, uid, ""}
1536+
}
1537+
15321538
if org.IsOrganization() {
15331539
for _, t := range org.Teams {
15341540
if !t.hasRepository(sess, repoID) {
@@ -1540,7 +1546,6 @@ func DeleteRepository(uid, repoID int64) error {
15401546
}
15411547

15421548
if err = deleteBeans(sess,
1543-
&Repository{ID: repoID},
15441549
&Access{RepoID: repo.ID},
15451550
&Action{RepoID: repo.ID},
15461551
&Watch{RepoID: repoID},
@@ -1555,38 +1560,41 @@ func DeleteRepository(uid, repoID int64) error {
15551560
}
15561561

15571562
// Delete comments and attachments.
1558-
issues := make([]*Issue, 0, 25)
1559-
attachmentPaths := make([]string, 0, len(issues))
1563+
issueIDs := make([]int64, 0, 25)
1564+
attachmentPaths := make([]string, 0, len(issueIDs))
15601565
if err = sess.
1566+
Table("issue").
1567+
Cols("id").
15611568
Where("repo_id=?", repoID).
1562-
Find(&issues); err != nil {
1569+
Find(&issueIDs); err != nil {
15631570
return err
15641571
}
1565-
for i := range issues {
1566-
if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil {
1572+
1573+
if len(issueIDs) > 0 {
1574+
if _, err = sess.In("issue_id", issueIDs).Delete(&Comment{}); err != nil {
15671575
return err
15681576
}
1569-
if _, err = sess.Delete(&IssueUser{IssueID: issues[i].ID}); err != nil {
1577+
if _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{}); err != nil {
15701578
return err
15711579
}
15721580

15731581
attachments := make([]*Attachment, 0, 5)
15741582
if err = sess.
1575-
Where("issue_id=?", issues[i].ID).
1583+
In("issue_id=?", issueIDs).
15761584
Find(&attachments); err != nil {
15771585
return err
15781586
}
15791587
for j := range attachments {
15801588
attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
15811589
}
15821590

1583-
if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil {
1591+
if _, err = sess.In("issue_id", issueIDs).Delete(&Attachment{}); err != nil {
15841592
return err
15851593
}
1586-
}
15871594

1588-
if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
1589-
return err
1595+
if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
1596+
return err
1597+
}
15901598
}
15911599

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

0 commit comments

Comments
 (0)