Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete references if repository gets deleted #15681

Merged
merged 4 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,24 +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},
&Comment{RefRepoID: 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},
&CommitStatus{RepoID: repoID},
&ProtectedBranch{RepoID: repoID},
&PullRequest{BaseRepoID: repoID},
&Release{RepoID: repoID},
&RepoIndexerStatus{RepoID: repoID},
&LanguageStat{RepoID: repoID},
&Comment{RefRepoID: repoID},
&RepoRedirect{RedirectRepoID: repoID},
&RepoUnit{RepoID: repoID},
zeripath marked this conversation as resolved.
Show resolved Hide resolved
&RepoUnit{RepoID: repoID},
zeripath marked this conversation as resolved.
Show resolved Hide resolved
&Star{RepoID: repoID},
&Task{RepoID: repoID},
&ProtectedBranch{RepoID: repoID},
&Watch{RepoID: repoID},
&Webhook{RepoID: repoID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}
Expand All @@ -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)
Expand Down
55 changes: 46 additions & 9 deletions modules/doctor/dbconsistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down