Skip to content

Commit a725d31

Browse files
6543KN4CK3Rzeripath
authored
Delete references if repository gets deleted (#15681) (#15684)
* Remove DeletedBranch and LFSLocks. * Sort beans. Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 8e27f6e commit a725d31

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

models/repo.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,24 +1454,26 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
14541454
if err := deleteBeans(sess,
14551455
&Access{RepoID: repo.ID},
14561456
&Action{RepoID: repo.ID},
1457-
&Watch{RepoID: repoID},
1458-
&Star{RepoID: repoID},
1459-
&Mirror{RepoID: repoID},
1460-
&Milestone{RepoID: repoID},
1461-
&Release{RepoID: repoID},
14621457
&Collaboration{RepoID: repoID},
1463-
&PullRequest{BaseRepoID: repoID},
1464-
&RepoUnit{RepoID: repoID},
1465-
&RepoRedirect{RedirectRepoID: repoID},
1466-
&Webhook{RepoID: repoID},
1458+
&Comment{RefRepoID: repoID},
1459+
&CommitStatus{RepoID: repoID},
1460+
&DeletedBranch{RepoID: repoID},
14671461
&HookTask{RepoID: repoID},
1462+
&LFSLock{RepoID: repoID},
1463+
&LanguageStat{RepoID: repoID},
1464+
&Milestone{RepoID: repoID},
1465+
&Mirror{RepoID: repoID},
14681466
&Notification{RepoID: repoID},
1469-
&CommitStatus{RepoID: repoID},
1467+
&ProtectedBranch{RepoID: repoID},
1468+
&PullRequest{BaseRepoID: repoID},
1469+
&Release{RepoID: repoID},
14701470
&RepoIndexerStatus{RepoID: repoID},
1471-
&LanguageStat{RepoID: repoID},
1472-
&Comment{RefRepoID: repoID},
1471+
&RepoRedirect{RedirectRepoID: repoID},
1472+
&RepoUnit{RepoID: repoID},
1473+
&Star{RepoID: repoID},
14731474
&Task{RepoID: repoID},
1474-
&ProtectedBranch{RepoID: repoID},
1475+
&Watch{RepoID: repoID},
1476+
&Webhook{RepoID: repoID},
14751477
); err != nil {
14761478
return fmt.Errorf("deleteBeans: %v", err)
14771479
}
@@ -1487,10 +1489,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
14871489
return err
14881490
}
14891491

1490-
if _, err := sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil {
1491-
return err
1492-
}
1493-
14941492
if repo.IsFork {
14951493
if _, err := sess.Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repo.ForkID); err != nil {
14961494
return fmt.Errorf("decrease fork count: %v", err)

modules/doctor/dbconsistency.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
2323
// find labels without existing repo or org
2424
count, err := models.CountOrphanedLabels()
2525
if err != nil {
26-
logger.Critical("Error: %v whilst counting orphaned labels")
26+
logger.Critical("Error: %v whilst counting orphaned labels", err)
2727
return err
2828
}
2929
if count > 0 {
@@ -41,7 +41,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
4141
// find IssueLabels without existing label
4242
count, err = models.CountOrphanedIssueLabels()
4343
if err != nil {
44-
logger.Critical("Error: %v whilst counting orphaned issue_labels")
44+
logger.Critical("Error: %v whilst counting orphaned issue_labels", err)
4545
return err
4646
}
4747
if count > 0 {
@@ -59,7 +59,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
5959
// find issues without existing repository
6060
count, err = models.CountOrphanedIssues()
6161
if err != nil {
62-
logger.Critical("Error: %v whilst counting orphaned issues")
62+
logger.Critical("Error: %v whilst counting orphaned issues", err)
6363
return err
6464
}
6565
if count > 0 {
@@ -77,7 +77,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
7777
// find pulls without existing issues
7878
count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
7979
if err != nil {
80-
logger.Critical("Error: %v whilst counting orphaned objects")
80+
logger.Critical("Error: %v whilst counting orphaned objects", err)
8181
return err
8282
}
8383
if count > 0 {
@@ -95,7 +95,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
9595
// find tracked times without existing issues/pulls
9696
count, err = models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
9797
if err != nil {
98-
logger.Critical("Error: %v whilst counting orphaned objects")
98+
logger.Critical("Error: %v whilst counting orphaned objects", err)
9999
return err
100100
}
101101
if count > 0 {
@@ -113,7 +113,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
113113
// find null archived repositories
114114
count, err = models.CountNullArchivedRepository()
115115
if err != nil {
116-
logger.Critical("Error: %v whilst counting null archived repositories")
116+
logger.Critical("Error: %v whilst counting null archived repositories", err)
117117
return err
118118
}
119119
if count > 0 {
@@ -132,7 +132,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
132132
// find label comments with empty labels
133133
count, err = models.CountCommentTypeLabelWithEmptyLabel()
134134
if err != nil {
135-
logger.Critical("Error: %v whilst counting label comments with empty labels")
135+
logger.Critical("Error: %v whilst counting label comments with empty labels", err)
136136
return err
137137
}
138138
if count > 0 {
@@ -191,7 +191,8 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
191191
if setting.Database.UsePostgreSQL {
192192
count, err = models.CountBadSequences()
193193
if err != nil {
194-
logger.Critical("Error: %v whilst checking sequence values")
194+
logger.Critical("Error: %v whilst checking sequence values", err)
195+
return err
195196
}
196197
if count > 0 {
197198
if autofix {
@@ -210,7 +211,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
210211
// find protected branches without existing repository
211212
count, err = models.CountOrphanedObjects("protected_branch", "repository", "protected_branch.repo_id=repository.id")
212213
if err != nil {
213-
logger.Critical("Error: %v whilst counting orphaned objects")
214+
logger.Critical("Error: %v whilst counting orphaned objects", err)
214215
return err
215216
}
216217
if count > 0 {
@@ -225,6 +226,42 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
225226
}
226227
}
227228

229+
// find deleted branches without existing repository
230+
count, err = models.CountOrphanedObjects("deleted_branch", "repository", "deleted_branch.repo_id=repository.id")
231+
if err != nil {
232+
logger.Critical("Error: %v whilst counting orphaned objects", err)
233+
return err
234+
}
235+
if count > 0 {
236+
if autofix {
237+
if err = models.DeleteOrphanedObjects("deleted_branch", "repository", "deleted_branch.repo_id=repository.id"); err != nil {
238+
logger.Critical("Error: %v whilst deleting orphaned objects", err)
239+
return err
240+
}
241+
logger.Info("%d deleted branches without existing repository deleted", count)
242+
} else {
243+
logger.Warn("%d deleted branches without existing repository", count)
244+
}
245+
}
246+
247+
// find LFS locks without existing repository
248+
count, err = models.CountOrphanedObjects("lfs_lock", "repository", "lfs_lock.repo_id=repository.id")
249+
if err != nil {
250+
logger.Critical("Error: %v whilst counting orphaned objects", err)
251+
return err
252+
}
253+
if count > 0 {
254+
if autofix {
255+
if err = models.DeleteOrphanedObjects("lfs_lock", "repository", "lfs_lock.repo_id=repository.id"); err != nil {
256+
logger.Critical("Error: %v whilst deleting orphaned objects", err)
257+
return err
258+
}
259+
logger.Info("%d LFS locks without existing repository deleted", count)
260+
} else {
261+
logger.Warn("%d LFS locks without existing repository", count)
262+
}
263+
}
264+
228265
return nil
229266
}
230267

0 commit comments

Comments
 (0)