From b32b4bbbec5dc50fb06c846cf4b11f47cdfc8934 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 30 Apr 2021 17:14:00 +0200 Subject: [PATCH 1/3] Remove DeletedBranch and LFSLocks. --- models/repo.go | 7 ++--- modules/doctor/dbconsistency.go | 55 +++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/models/repo.go b/models/repo.go index 0a0f725f09d91..4873351f0b8dc 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1473,6 +1473,9 @@ func DeleteRepository(doer *User, uid, repoID int64) error { &Comment{RefRepoID: repoID}, &Task{RepoID: repoID}, &ProtectedBranch{RepoID: repoID}, + &DeletedBranch{RepoID: repoID}, + &LFSLock{RepoID: repoID}, + &RepoUnit{RepoID: repoID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } @@ -1488,10 +1491,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error { return err } - if _, err := sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil { - return err - } - if repo.IsFork { if _, err := sess.Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repo.ForkID); err != nil { return fmt.Errorf("decrease fork count: %v", err) diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go index 417a7d4b6b45c..23e8331e774a3 100644 --- a/modules/doctor/dbconsistency.go +++ b/modules/doctor/dbconsistency.go @@ -23,7 +23,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find labels without existing repo or org count, err := models.CountOrphanedLabels() if err != nil { - logger.Critical("Error: %v whilst counting orphaned labels") + logger.Critical("Error: %v whilst counting orphaned labels", err) return err } if count > 0 { @@ -41,7 +41,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find IssueLabels without existing label count, err = models.CountOrphanedIssueLabels() if err != nil { - logger.Critical("Error: %v whilst counting orphaned issue_labels") + logger.Critical("Error: %v whilst counting orphaned issue_labels", err) return err } if count > 0 { @@ -59,7 +59,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find issues without existing repository count, err = models.CountOrphanedIssues() if err != nil { - logger.Critical("Error: %v whilst counting orphaned issues") + logger.Critical("Error: %v whilst counting orphaned issues", err) return err } if count > 0 { @@ -77,7 +77,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find pulls without existing issues count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id") if err != nil { - logger.Critical("Error: %v whilst counting orphaned objects") + logger.Critical("Error: %v whilst counting orphaned objects", err) return err } if count > 0 { @@ -95,7 +95,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find tracked times without existing issues/pulls count, err = models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id") if err != nil { - logger.Critical("Error: %v whilst counting orphaned objects") + logger.Critical("Error: %v whilst counting orphaned objects", err) return err } if count > 0 { @@ -113,7 +113,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find null archived repositories count, err = models.CountNullArchivedRepository() if err != nil { - logger.Critical("Error: %v whilst counting null archived repositories") + logger.Critical("Error: %v whilst counting null archived repositories", err) return err } if count > 0 { @@ -132,7 +132,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find label comments with empty labels count, err = models.CountCommentTypeLabelWithEmptyLabel() if err != nil { - logger.Critical("Error: %v whilst counting label comments with empty labels") + logger.Critical("Error: %v whilst counting label comments with empty labels", err) return err } if count > 0 { @@ -191,7 +191,8 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { if setting.Database.UsePostgreSQL { count, err = models.CountBadSequences() if err != nil { - logger.Critical("Error: %v whilst checking sequence values") + logger.Critical("Error: %v whilst checking sequence values", err) + return err } if count > 0 { if autofix { @@ -210,7 +211,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // find protected branches without existing repository count, err = models.CountOrphanedObjects("protected_branch", "repository", "protected_branch.repo_id=repository.id") if err != nil { - logger.Critical("Error: %v whilst counting orphaned objects") + logger.Critical("Error: %v whilst counting orphaned objects", err) return err } if count > 0 { @@ -225,6 +226,42 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { } } + // find deleted branches without existing repository + count, err = models.CountOrphanedObjects("deleted_branch", "repository", "deleted_branch.repo_id=repository.id") + if err != nil { + logger.Critical("Error: %v whilst counting orphaned objects", err) + return err + } + if count > 0 { + if autofix { + if err = models.DeleteOrphanedObjects("deleted_branch", "repository", "deleted_branch.repo_id=repository.id"); err != nil { + logger.Critical("Error: %v whilst deleting orphaned objects", err) + return err + } + logger.Info("%d deleted branches without existing repository deleted", count) + } else { + logger.Warn("%d deleted branches without existing repository", count) + } + } + + // find LFS locks without existing repository + count, err = models.CountOrphanedObjects("lfs_lock", "repository", "lfs_lock.repo_id=repository.id") + if err != nil { + logger.Critical("Error: %v whilst counting orphaned objects", err) + return err + } + if count > 0 { + if autofix { + if err = models.DeleteOrphanedObjects("lfs_lock", "repository", "lfs_lock.repo_id=repository.id"); err != nil { + logger.Critical("Error: %v whilst deleting orphaned objects", err) + return err + } + logger.Info("%d LFS locks without existing repository deleted", count) + } else { + logger.Warn("%d LFS locks without existing repository", count) + } + } + return nil } From 02e02e88d0bdeb56731f1f998f065fb726eeb006 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 30 Apr 2021 17:15:00 +0200 Subject: [PATCH 2/3] Sort beans. --- models/repo.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/models/repo.go b/models/repo.go index 4873351f0b8dc..e36d614597844 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1455,27 +1455,27 @@ func DeleteRepository(doer *User, uid, repoID int64) error { if err := deleteBeans(sess, &Access{RepoID: repo.ID}, &Action{RepoID: repo.ID}, - &Watch{RepoID: repoID}, - &Star{RepoID: repoID}, - &Mirror{RepoID: repoID}, - &Milestone{RepoID: repoID}, - &Release{RepoID: repoID}, &Collaboration{RepoID: repoID}, - &PullRequest{BaseRepoID: repoID}, - &RepoUnit{RepoID: repoID}, - &RepoRedirect{RedirectRepoID: repoID}, - &Webhook{RepoID: repoID}, - &HookTask{RepoID: repoID}, - &Notification{RepoID: repoID}, - &CommitStatus{RepoID: repoID}, - &RepoIndexerStatus{RepoID: repoID}, - &LanguageStat{RepoID: repoID}, &Comment{RefRepoID: repoID}, - &Task{RepoID: repoID}, - &ProtectedBranch{RepoID: repoID}, + &CommitStatus{RepoID: repoID}, &DeletedBranch{RepoID: repoID}, + &HookTask{RepoID: repoID}, &LFSLock{RepoID: repoID}, + &LanguageStat{RepoID: repoID}, + &Milestone{RepoID: repoID}, + &Mirror{RepoID: repoID}, + &Notification{RepoID: repoID}, + &ProtectedBranch{RepoID: repoID}, + &PullRequest{BaseRepoID: repoID}, + &Release{RepoID: repoID}, + &RepoIndexerStatus{RepoID: repoID}, + &RepoRedirect{RedirectRepoID: repoID}, + &RepoUnit{RepoID: repoID}, &RepoUnit{RepoID: repoID}, + &Star{RepoID: repoID}, + &Task{RepoID: repoID}, + &Watch{RepoID: repoID}, + &Webhook{RepoID: repoID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } From 9b2b1e9e3983059add6bf2d0e2362bd06fa70e87 Mon Sep 17 00:00:00 2001 From: zeripath Date: Fri, 30 Apr 2021 19:30:04 +0100 Subject: [PATCH 3/3] Update models/repo.go --- models/repo.go | 1 - 1 file changed, 1 deletion(-) diff --git a/models/repo.go b/models/repo.go index e36d614597844..daa94c0d508b9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1471,7 +1471,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error { &RepoIndexerStatus{RepoID: repoID}, &RepoRedirect{RedirectRepoID: repoID}, &RepoUnit{RepoID: repoID}, - &RepoUnit{RepoID: repoID}, &Star{RepoID: repoID}, &Task{RepoID: repoID}, &Watch{RepoID: repoID},