From 3c5116ea790aaf9d1b8b9282d82f0a0bdc2d29dd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 7 Jun 2021 14:55:12 +0800 Subject: [PATCH 1/3] Add doctor for wrong label and issue_label data --- models/issue_label.go | 33 +++++++++++++++++++++++++ modules/doctor/label.go | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 modules/doctor/label.go diff --git a/models/issue_label.go b/models/issue_label.go index d1ff4692366d0..97c8b1a1f15da 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -779,3 +779,36 @@ func deleteLabelsByRepoID(sess Engine, repoID int64) error { _, err := sess.Delete(&Label{RepoID: repoID}) return err } + +// CountWrongLabels counts wrong data in issue_label and label +func CountWrongLabels() (int64, int64, error) { + wrongLabels, err := x.Where("repo_id > 0 and repo_id not in (select id from repository)").Count(new(Label)) + if err != nil { + return 0, 0, err + } + + wrongIssueLabels, err := x.Where("label_id IN (select id from label where repo_id > 0 and repo_id not in (select id from repository))").Count(new(IssueLabel)) + if err != nil { + return 0, 0, err + } + + return wrongLabels, wrongIssueLabels, nil +} + +// FixWrongLabels fix wrong data in label +func FixWrongLabels() (int64, error) { + res, err := x.Exec("DELETE FROM label WHERE repo_id > 0 and repo_id not in (select id from repository)") + if err != nil { + return 0, err + } + return res.RowsAffected() +} + +// FixWrongIssueLables fix wrong label data +func FixWrongIssueLables() (int64, error) { + res, err := x.Exec("DELETE FROM issue_label WHERE label_id in (select id FROM label WHERE repo_id > 0 and repo_id not in (select id from repository))") + if err != nil { + return 0, err + } + return res.RowsAffected() +} diff --git a/modules/doctor/label.go b/modules/doctor/label.go new file mode 100644 index 0000000000000..79ffef33c6ddc --- /dev/null +++ b/modules/doctor/label.go @@ -0,0 +1,54 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package doctor + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" +) + +func checkLabels(logger log.Logger, autofix bool) error { + lablesCnt, issueLabelsCnt, err := models.CountWrongLabels() + if err != nil { + logger.Critical("Error: %v whilst counting wrong labels and issue labels") + return err + } + var count int64 + if issueLabelsCnt > 0 { // We should check issue_label before label + if autofix { + if count, err = models.FixWrongIssueLables(); err != nil { + logger.Critical("Error: %v whilst fixing wrong issue labels") + return err + } + logger.Info("%d issue labels missing repositories fixed", count) + } else { + logger.Warn("%d issue labels missing repositories exist", issueLabelsCnt) + } + } + if lablesCnt > 0 { + if autofix { + if count, err = models.FixWrongLabels(); err != nil { + logger.Critical("Error: %v whilst fixing wrong labels") + return err + } + logger.Info("%d labels missing repositories fixed", count) + } else { + logger.Warn("%d labels missing repositories exist", lablesCnt) + } + } + return nil +} + +func init() { + Register(&Check{ + Title: "Check wrong labels and issue_labels where repositories deleted", + Name: "labels", + IsDefault: true, + Run: checkLabels, + AbortIfFailed: true, + SkipDatabaseInitialization: false, + Priority: 1, + }) +} From 175ec7515ee62219ec22be4efee087e5e8e720c6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 7 Jun 2021 15:34:16 +0800 Subject: [PATCH 2/3] Fix labels and issue labels check --- models/consistency.go | 41 ++++++++++++++++++------------- modules/doctor/label.go | 54 ----------------------------------------- 2 files changed, 24 insertions(+), 71 deletions(-) delete mode 100644 modules/doctor/label.go diff --git a/models/consistency.go b/models/consistency.go index 77a8018266e51..9cfd02195effd 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -180,17 +180,21 @@ func CountOrphanedLabels() (int64, error) { } norepo, err := x.Table("label"). - Join("LEFT", "repository", "label.repo_id=repository.id"). - Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0}). - Count("id") + Where(builder.And( + builder.Gt{"repo_id": 0}, + builder.NotIn("repo_id", builder.Select("id").From("repository")), + )). + Count() if err != nil { return 0, err } noorg, err := x.Table("label"). - Join("LEFT", "`user`", "label.org_id=`user`.id"). - Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0}). - Count("id") + Where(builder.And( + builder.Gt{"org_id": 0}, + builder.NotIn("org_id", builder.Select("id").From("user")), + )). + Count() if err != nil { return 0, err } @@ -206,17 +210,21 @@ func DeleteOrphanedLabels() error { } // delete labels with none existing repos - if _, err := x.In("id", builder.Select("label.id").From("label"). - Join("LEFT", "repository", "label.repo_id=repository.id"). - Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0})). + if _, err := x. + Where(builder.And( + builder.Gt{"repo_id": 0}, + builder.NotIn("repo_id", builder.Select("id").From("repository")), + )). Delete(Label{}); err != nil { return err } // delete labels with none existing orgs - if _, err := x.In("id", builder.Select("label.id").From("label"). - Join("LEFT", "`user`", "label.org_id=`user`.id"). - Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0})). + if _, err := x. + Where(builder.And( + builder.Gt{"org_id": 0}, + builder.NotIn("org_id", builder.Select("id").From("user")), + )). Delete(Label{}); err != nil { return err } @@ -227,15 +235,14 @@ func DeleteOrphanedLabels() error { // CountOrphanedIssueLabels return count of IssueLabels witch have no label behind anymore func CountOrphanedIssueLabels() (int64, error) { return x.Table("issue_label"). - Join("LEFT", "label", "issue_label.label_id = label.id"). - Where(builder.IsNull{"label.id"}).Count() + NotIn("label_id", builder.Select("id").From("label")). + Count() } // DeleteOrphanedIssueLabels delete IssueLabels witch have no label behind anymore func DeleteOrphanedIssueLabels() error { - _, err := x.In("id", builder.Select("issue_label.id").From("issue_label"). - Join("LEFT", "label", "issue_label.label_id = label.id"). - Where(builder.IsNull{"label.id"})). + _, err := x. + NotIn("label_id", builder.Select("id").From("label")). Delete(IssueLabel{}) return err diff --git a/modules/doctor/label.go b/modules/doctor/label.go deleted file mode 100644 index 79ffef33c6ddc..0000000000000 --- a/modules/doctor/label.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package doctor - -import ( - "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/log" -) - -func checkLabels(logger log.Logger, autofix bool) error { - lablesCnt, issueLabelsCnt, err := models.CountWrongLabels() - if err != nil { - logger.Critical("Error: %v whilst counting wrong labels and issue labels") - return err - } - var count int64 - if issueLabelsCnt > 0 { // We should check issue_label before label - if autofix { - if count, err = models.FixWrongIssueLables(); err != nil { - logger.Critical("Error: %v whilst fixing wrong issue labels") - return err - } - logger.Info("%d issue labels missing repositories fixed", count) - } else { - logger.Warn("%d issue labels missing repositories exist", issueLabelsCnt) - } - } - if lablesCnt > 0 { - if autofix { - if count, err = models.FixWrongLabels(); err != nil { - logger.Critical("Error: %v whilst fixing wrong labels") - return err - } - logger.Info("%d labels missing repositories fixed", count) - } else { - logger.Warn("%d labels missing repositories exist", lablesCnt) - } - } - return nil -} - -func init() { - Register(&Check{ - Title: "Check wrong labels and issue_labels where repositories deleted", - Name: "labels", - IsDefault: true, - Run: checkLabels, - AbortIfFailed: true, - SkipDatabaseInitialization: false, - Priority: 1, - }) -} From 818dd7daafb226d1018da7cd3d48188730f3643e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 7 Jun 2021 15:39:54 +0800 Subject: [PATCH 3/3] Remove unnecessary functions --- models/issue_label.go | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/models/issue_label.go b/models/issue_label.go index 97c8b1a1f15da..d1ff4692366d0 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -779,36 +779,3 @@ func deleteLabelsByRepoID(sess Engine, repoID int64) error { _, err := sess.Delete(&Label{RepoID: repoID}) return err } - -// CountWrongLabels counts wrong data in issue_label and label -func CountWrongLabels() (int64, int64, error) { - wrongLabels, err := x.Where("repo_id > 0 and repo_id not in (select id from repository)").Count(new(Label)) - if err != nil { - return 0, 0, err - } - - wrongIssueLabels, err := x.Where("label_id IN (select id from label where repo_id > 0 and repo_id not in (select id from repository))").Count(new(IssueLabel)) - if err != nil { - return 0, 0, err - } - - return wrongLabels, wrongIssueLabels, nil -} - -// FixWrongLabels fix wrong data in label -func FixWrongLabels() (int64, error) { - res, err := x.Exec("DELETE FROM label WHERE repo_id > 0 and repo_id not in (select id from repository)") - if err != nil { - return 0, err - } - return res.RowsAffected() -} - -// FixWrongIssueLables fix wrong label data -func FixWrongIssueLables() (int64, error) { - res, err := x.Exec("DELETE FROM issue_label WHERE label_id in (select id FROM label WHERE repo_id > 0 and repo_id not in (select id from repository))") - if err != nil { - return 0, err - } - return res.RowsAffected() -}