| @@ -1504,14 +1504,6 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) { | |||||
| // DeleteRepository deletes a repository for a user or organization. | // DeleteRepository deletes a repository for a user or organization. | ||||
| func DeleteRepository(uid, repoID int64) error { | func DeleteRepository(uid, repoID int64) error { | ||||
| repo := &Repository{ID: repoID, OwnerID: uid} | |||||
| has, err := x.Get(repo) | |||||
| if err != nil { | |||||
| return err | |||||
| } else if !has { | |||||
| return ErrRepoNotExist{repoID, uid, ""} | |||||
| } | |||||
| // In case is a organization. | // In case is a organization. | ||||
| org, err := GetUserByID(uid) | org, err := GetUserByID(uid) | ||||
| if err != nil { | if err != nil { | ||||
| @@ -1529,6 +1521,20 @@ func DeleteRepository(uid, repoID int64) error { | |||||
| return err | return err | ||||
| } | } | ||||
| repo := &Repository{ID: repoID, OwnerID: uid} | |||||
| has, err := sess.Get(repo) | |||||
| if err != nil { | |||||
| return err | |||||
| } else if !has { | |||||
| return ErrRepoNotExist{repoID, uid, ""} | |||||
| } | |||||
| if cnt, err := sess.Id(repoID).Delete(&Repository{}); err != nil { | |||||
| return err | |||||
| } else if cnt != 1 { | |||||
| return ErrRepoNotExist{repoID, uid, ""} | |||||
| } | |||||
| if org.IsOrganization() { | if org.IsOrganization() { | ||||
| for _, t := range org.Teams { | for _, t := range org.Teams { | ||||
| if !t.hasRepository(sess, repoID) { | if !t.hasRepository(sess, repoID) { | ||||
| @@ -1540,7 +1546,6 @@ func DeleteRepository(uid, repoID int64) error { | |||||
| } | } | ||||
| if err = deleteBeans(sess, | if err = deleteBeans(sess, | ||||
| &Repository{ID: repoID}, | |||||
| &Access{RepoID: repo.ID}, | &Access{RepoID: repo.ID}, | ||||
| &Action{RepoID: repo.ID}, | &Action{RepoID: repo.ID}, | ||||
| &Watch{RepoID: repoID}, | &Watch{RepoID: repoID}, | ||||
| @@ -1555,24 +1560,27 @@ func DeleteRepository(uid, repoID int64) error { | |||||
| } | } | ||||
| // Delete comments and attachments. | // Delete comments and attachments. | ||||
| issues := make([]*Issue, 0, 25) | |||||
| attachmentPaths := make([]string, 0, len(issues)) | |||||
| issueIDs := make([]int64, 0, 25) | |||||
| attachmentPaths := make([]string, 0, len(issueIDs)) | |||||
| if err = sess. | if err = sess. | ||||
| Table("issue"). | |||||
| Cols("id"). | |||||
| Where("repo_id=?", repoID). | Where("repo_id=?", repoID). | ||||
| Find(&issues); err != nil { | |||||
| Find(&issueIDs); err != nil { | |||||
| return err | return err | ||||
| } | } | ||||
| for i := range issues { | |||||
| if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil { | |||||
| if len(issueIDs) > 0 { | |||||
| if _, err = sess.In("issue_id", issueIDs).Delete(&Comment{}); err != nil { | |||||
| return err | return err | ||||
| } | } | ||||
| if _, err = sess.Delete(&IssueUser{IssueID: issues[i].ID}); err != nil { | |||||
| if _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{}); err != nil { | |||||
| return err | return err | ||||
| } | } | ||||
| attachments := make([]*Attachment, 0, 5) | attachments := make([]*Attachment, 0, 5) | ||||
| if err = sess. | if err = sess. | ||||
| Where("issue_id=?", issues[i].ID). | |||||
| In("issue_id=?", issueIDs). | |||||
| Find(&attachments); err != nil { | Find(&attachments); err != nil { | ||||
| return err | return err | ||||
| } | } | ||||
| @@ -1580,13 +1588,13 @@ func DeleteRepository(uid, repoID int64) error { | |||||
| attachmentPaths = append(attachmentPaths, attachments[j].LocalPath()) | attachmentPaths = append(attachmentPaths, attachments[j].LocalPath()) | ||||
| } | } | ||||
| if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil { | |||||
| if _, err = sess.In("issue_id", issueIDs).Delete(&Attachment{}); err != nil { | |||||
| return err | return err | ||||
| } | } | ||||
| } | |||||
| if _, err = sess.Delete(&Issue{RepoID: repoID}); 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 { | if _, err = sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil { | ||||