From e2f0ab3343cd4ed710221d96c253566c20cfab04 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 15 Sep 2021 03:41:40 +0800 Subject: Add doctor dbconsistency check for release and attachment (#16978) --- models/attachment.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'models/attachment.go') diff --git a/models/attachment.go b/models/attachment.go index 330e965bb1..96048db00a 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -272,3 +272,16 @@ func IterateAttachment(f func(attach *Attachment) error) error { } } } + +// CountOrphanedAttachments returns the number of bad attachments +func CountOrphanedAttachments() (int64, error) { + 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`))"). + Count(new(Attachment)) +} + +// DeleteOrphanedAttachments delete all bad attachments +func DeleteOrphanedAttachments() error { + _, 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`))"). + Delete(new(Attachment)) + return err +} -- cgit v1.2.3