From c0ea3963be91dbd1f436d390ce5223c7ad116479 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 24 Feb 2017 23:19:13 +0800 Subject: [PATCH] fix delete repo will hang on postgres (#1044) --- models/admin.go | 17 ++++++++++------- models/repo.go | 21 ++++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/models/admin.go b/models/admin.go index 94dc9b7a8..e573c9ba7 100644 --- a/models/admin.go +++ b/models/admin.go @@ -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) } } diff --git a/models/repo.go b/models/repo.go index e276a87ae..3a1f14919 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 }