Fix doctor --run check-db-consistency --fix with label fix (#16094)

* Add doctor for wrong label and issue_label data

* Fix labels and issue labels check

* Remove unnecessary functions
This commit is contained in:
Lunny Xiao 2021-06-08 12:36:23 +08:00 committed by GitHub
parent b9d611e917
commit 44f8c812ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -180,17 +180,21 @@ func CountOrphanedLabels() (int64, error) {
} }
norepo, err := x.Table("label"). norepo, err := x.Table("label").
Join("LEFT", "repository", "label.repo_id=repository.id"). Where(builder.And(
Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0}). builder.Gt{"repo_id": 0},
Count("id") builder.NotIn("repo_id", builder.Select("id").From("repository")),
)).
Count()
if err != nil { if err != nil {
return 0, err return 0, err
} }
noorg, err := x.Table("label"). noorg, err := x.Table("label").
Join("LEFT", "`user`", "label.org_id=`user`.id"). Where(builder.And(
Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0}). builder.Gt{"org_id": 0},
Count("id") builder.NotIn("org_id", builder.Select("id").From("user")),
)).
Count()
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -206,17 +210,21 @@ func DeleteOrphanedLabels() error {
} }
// delete labels with none existing repos // delete labels with none existing repos
if _, err := x.In("id", builder.Select("label.id").From("label"). if _, err := x.
Join("LEFT", "repository", "label.repo_id=repository.id"). Where(builder.And(
Where(builder.IsNull{"repository.id"}).And(builder.Gt{"label.repo_id": 0})). builder.Gt{"repo_id": 0},
builder.NotIn("repo_id", builder.Select("id").From("repository")),
)).
Delete(Label{}); err != nil { Delete(Label{}); err != nil {
return err return err
} }
// delete labels with none existing orgs // delete labels with none existing orgs
if _, err := x.In("id", builder.Select("label.id").From("label"). if _, err := x.
Join("LEFT", "`user`", "label.org_id=`user`.id"). Where(builder.And(
Where(builder.IsNull{"`user`.id"}).And(builder.Gt{"label.org_id": 0})). builder.Gt{"org_id": 0},
builder.NotIn("org_id", builder.Select("id").From("user")),
)).
Delete(Label{}); err != nil { Delete(Label{}); err != nil {
return err return err
} }
@ -227,15 +235,14 @@ func DeleteOrphanedLabels() error {
// CountOrphanedIssueLabels return count of IssueLabels witch have no label behind anymore // CountOrphanedIssueLabels return count of IssueLabels witch have no label behind anymore
func CountOrphanedIssueLabels() (int64, error) { func CountOrphanedIssueLabels() (int64, error) {
return x.Table("issue_label"). return x.Table("issue_label").
Join("LEFT", "label", "issue_label.label_id = label.id"). NotIn("label_id", builder.Select("id").From("label")).
Where(builder.IsNull{"label.id"}).Count() Count()
} }
// DeleteOrphanedIssueLabels delete IssueLabels witch have no label behind anymore // DeleteOrphanedIssueLabels delete IssueLabels witch have no label behind anymore
func DeleteOrphanedIssueLabels() error { func DeleteOrphanedIssueLabels() error {
_, err := x.In("id", builder.Select("issue_label.id").From("issue_label"). _, err := x.
Join("LEFT", "label", "issue_label.label_id = label.id"). NotIn("label_id", builder.Select("id").From("label")).
Where(builder.IsNull{"label.id"})).
Delete(IssueLabel{}) Delete(IssueLabel{})
return err return err