@@ -1504,14 +1504,6 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) {
1504
1504
1505
1505
// DeleteRepository deletes a repository for a user or organization.
1506
1506
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
-
1515
1507
// In case is a organization.
1516
1508
org , err := GetUserByID (uid )
1517
1509
if err != nil {
@@ -1529,6 +1521,20 @@ func DeleteRepository(uid, repoID int64) error {
1529
1521
return err
1530
1522
}
1531
1523
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
+
1532
1538
if org .IsOrganization () {
1533
1539
for _ , t := range org .Teams {
1534
1540
if ! t .hasRepository (sess , repoID ) {
@@ -1540,7 +1546,6 @@ func DeleteRepository(uid, repoID int64) error {
1540
1546
}
1541
1547
1542
1548
if err = deleteBeans (sess ,
1543
- & Repository {ID : repoID },
1544
1549
& Access {RepoID : repo .ID },
1545
1550
& Action {RepoID : repo .ID },
1546
1551
& Watch {RepoID : repoID },
@@ -1555,38 +1560,41 @@ func DeleteRepository(uid, repoID int64) error {
1555
1560
}
1556
1561
1557
1562
// 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 ))
1560
1565
if err = sess .
1566
+ Table ("issue" ).
1567
+ Cols ("id" ).
1561
1568
Where ("repo_id=?" , repoID ).
1562
- Find (& issues ); err != nil {
1569
+ Find (& issueIDs ); err != nil {
1563
1570
return err
1564
1571
}
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 {
1567
1575
return err
1568
1576
}
1569
- if _ , err = sess .Delete (& IssueUser {IssueID : issues [ i ]. ID }); err != nil {
1577
+ if _ , err = sess .In ( "issue_id" , issueIDs ). Delete (& IssueUser {}); err != nil {
1570
1578
return err
1571
1579
}
1572
1580
1573
1581
attachments := make ([]* Attachment , 0 , 5 )
1574
1582
if err = sess .
1575
- Where ("issue_id=?" , issues [ i ]. ID ).
1583
+ In ("issue_id=?" , issueIDs ).
1576
1584
Find (& attachments ); err != nil {
1577
1585
return err
1578
1586
}
1579
1587
for j := range attachments {
1580
1588
attachmentPaths = append (attachmentPaths , attachments [j ].LocalPath ())
1581
1589
}
1582
1590
1583
- if _ , err = sess .Delete (& Attachment {IssueID : issues [ i ]. ID }); err != nil {
1591
+ if _ , err = sess .In ( "issue_id" , issueIDs ). Delete (& Attachment {}); err != nil {
1584
1592
return err
1585
1593
}
1586
- }
1587
1594
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
+ }
1590
1598
}
1591
1599
1592
1600
if _ , err = sess .Where ("repo_id = ?" , repoID ).Delete (new (RepoUnit )); err != nil {
0 commit comments