Skip to content

Commit e2f0ab3

Browse files
authored
Add doctor dbconsistency check for release and attachment (#16978)
1 parent 87505a9 commit e2f0ab3

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

models/attachment.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,16 @@ func IterateAttachment(f func(attach *Attachment) error) error {
272272
}
273273
}
274274
}
275+
276+
// CountOrphanedAttachments returns the number of bad attachments
277+
func CountOrphanedAttachments() (int64, error) {
278+
return x.Where("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))").
279+
Count(new(Attachment))
280+
}
281+
282+
// DeleteOrphanedAttachments delete all bad attachments
283+
func DeleteOrphanedAttachments() error {
284+
_, err := x.Where("(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))").
285+
Delete(new(Attachment))
286+
return err
287+
}

modules/doctor/dbconsistency.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
7474
}
7575
}
7676

77+
// find releases without existing repository
78+
count, err = models.CountOrphanedObjects("release", "repository", "release.repo_id=repository.id")
79+
if err != nil {
80+
logger.Critical("Error: %v whilst counting orphaned objects", err)
81+
return err
82+
}
83+
if count > 0 {
84+
if autofix {
85+
if err = models.DeleteOrphanedObjects("release", "repository", "release.repo_id=repository.id"); err != nil {
86+
logger.Critical("Error: %v whilst deleting orphaned objects", err)
87+
return err
88+
}
89+
logger.Info("%d releases without existing repository deleted", count)
90+
} else {
91+
logger.Warn("%d releases without existing repository", count)
92+
}
93+
}
94+
7795
// find pulls without existing issues
7896
count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
7997
if err != nil {
@@ -110,6 +128,24 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
110128
}
111129
}
112130

131+
// find attachments without existing issues or releases
132+
count, err = models.CountOrphanedAttachments()
133+
if err != nil {
134+
logger.Critical("Error: %v whilst counting orphaned objects", err)
135+
return err
136+
}
137+
if count > 0 {
138+
if autofix {
139+
if err = models.DeleteOrphanedAttachments(); err != nil {
140+
logger.Critical("Error: %v whilst deleting orphaned objects", err)
141+
return err
142+
}
143+
logger.Info("%d attachments without existing issue or release deleted", count)
144+
} else {
145+
logger.Warn("%d attachments without existing issue or release", count)
146+
}
147+
}
148+
113149
// find null archived repositories
114150
count, err = models.CountNullArchivedRepository()
115151
if err != nil {

0 commit comments

Comments
 (0)