fix delete repo will hang on postgres (#1044)

release/v1.15
Lunny Xiao 2017-02-24 23:19:13 +08:00 committed by GitHub
parent 0602a44b27
commit c0ea3963be
2 changed files with 22 additions and 16 deletions

View File

@ -55,27 +55,30 @@ func (n *Notice) TrStr() string {
// CreateNotice creates new system notice.
func CreateNotice(tp NoticeType, desc string) error {
// prevent panic if database connection is not available at this point
if x == nil {
return fmt.Errorf("Could not save notice due database connection not being available: %d %s", tp, desc)
}
return createNotice(x, tp, desc)
}
func createNotice(e Engine, tp NoticeType, desc string) error {
n := &Notice{
Type: tp,
Description: desc,
}
_, err := x.Insert(n)
_, err := e.Insert(n)
return err
}
// CreateRepositoryNotice creates new system notice with type NoticeRepository.
func CreateRepositoryNotice(desc string) error {
return CreateNotice(NoticeRepository, desc)
return createNotice(x, NoticeRepository, desc)
}
// RemoveAllWithNotice removes all directories in given path and
// creates a system notice when error occurs.
func RemoveAllWithNotice(title, path string) {
removeAllWithNotice(x, title, path)
}
func removeAllWithNotice(e Engine, title, path string) {
var err error
// workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
// this bug should be fixed on Go 1.7, so the workaround should be removed when Gogs don't support Go 1.6 anymore:
@ -91,7 +94,7 @@ func RemoveAllWithNotice(title, path string) {
if err != nil {
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
log.Warn(desc)
if err = CreateRepositoryNotice(desc); err != nil {
if err = createNotice(e, NoticeRepository, desc); err != nil {
log.Error(4, "CreateRepositoryNotice: %v", err)
}
}

View File

@ -412,13 +412,18 @@ func (repo *Repository) ComposeMetas() map[string]string {
}
// DeleteWiki removes the actual and local copy of repository wiki.
func (repo *Repository) DeleteWiki() {
func (repo *Repository) DeleteWiki() error {
return repo.deleteWiki(x)
}
func (repo *Repository) deleteWiki(e Engine) error {
wikiPaths := []string{repo.WikiPath(), repo.LocalWikiPath()}
for _, wikiPath := range wikiPaths {
RemoveAllWithNotice("Delete repository wiki", wikiPath)
removeAllWithNotice(e, "Delete repository wiki", wikiPath)
}
x.Where("repo_id = ?", repo.ID).And("type = ?", UnitTypeWiki).Delete(new(RepoUnit))
_, err := e.Where("repo_id = ?", repo.ID).And("type = ?", UnitTypeWiki).Delete(new(RepoUnit))
return err
}
func (repo *Repository) getAssignees(e Engine) (_ []*User, err error) {
@ -1620,27 +1625,25 @@ func DeleteRepository(uid, repoID int64) error {
return err
}
// Remove repository files.
// FIXME: Remove repository files should be executed after transaction succeed.
repoPath := repo.repoPath(sess)
RemoveAllWithNotice("Delete repository files", repoPath)
removeAllWithNotice(sess, "Delete repository files", repoPath)
repo.DeleteWiki()
repo.deleteWiki(sess)
// Remove attachment files.
for i := range attachmentPaths {
RemoveAllWithNotice("Delete attachment", attachmentPaths[i])
removeAllWithNotice(sess, "Delete attachment", attachmentPaths[i])
}
// Remove LFS objects
var lfsObjects []*LFSMetaObject
if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil {
return err
}
for _, v := range lfsObjects {
count, err := sess.Count(&LFSMetaObject{Oid: v.Oid})
if err != nil {
return err
}