aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author65432021-03-19 22:13:39 +0100
committerGitHub2021-03-19 22:13:39 +0100
commit70e4134130331bda3bb1873c6099d8e63c468a2d (patch)
tree8f4966a9c664f7f940ea537c6df2a4a2d66683d1
parent909f2be99d0c699f506f463bb6238378f12c0dc1 (diff)
Delete Labels & IssueLabels on Repo Delete too (#15039) (#15051)
* Doctor: find IssueLabels without existing label * Repo Delete: delete labels & issue_labels too
-rw-r--r--cmd/doctor.go16
-rw-r--r--models/consistency.go18
-rw-r--r--models/issue_label.go12
-rw-r--r--models/repo.go4
4 files changed, 50 insertions, 0 deletions
diff --git a/cmd/doctor.go b/cmd/doctor.go
index 5ba045111..3c271f131 100644
--- a/cmd/doctor.go
+++ b/cmd/doctor.go
@@ -606,6 +606,22 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
}
}
+ // find IssueLabels without existing label
+ count, err = models.CountOrphanedIssueLabels()
+ if err != nil {
+ return nil, err
+ }
+ if count > 0 {
+ if ctx.Bool("fix") {
+ if err = models.DeleteOrphanedIssueLabels(); err != nil {
+ return nil, err
+ }
+ results = append(results, fmt.Sprintf("%d issue_labels without existing label deleted", count))
+ } else {
+ results = append(results, fmt.Sprintf("%d issue_labels without existing label", count))
+ }
+ }
+
//find issues without existing repository
count, err = models.CountOrphanedIssues()
if err != nil {
diff --git a/models/consistency.go b/models/consistency.go
index 0c62d4dc2..3a5c4cae5 100644
--- a/models/consistency.go
+++ b/models/consistency.go
@@ -224,6 +224,24 @@ func DeleteOrphanedLabels() error {
return nil
}
+// 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()
+}
+
+// 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"})).
+ Delete(IssueLabel{})
+
+ return err
+}
+
// CountOrphanedIssues count issues without a repo
func CountOrphanedIssues() (int64, error) {
return x.Table("issue").
diff --git a/models/issue_label.go b/models/issue_label.go
index 54b286fe7..6d519aa8c 100644
--- a/models/issue_label.go
+++ b/models/issue_label.go
@@ -764,3 +764,15 @@ func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
return sess.Commit()
}
+
+func deleteLabelsByRepoID(sess Engine, repoID int64) error {
+ deleteCond := builder.Select("id").From("label").Where(builder.Eq{"label.repo_id": repoID})
+
+ if _, err := sess.In("label_id", deleteCond).
+ Delete(&IssueLabel{}); err != nil {
+ return err
+ }
+
+ _, err := sess.Delete(&Label{RepoID: repoID})
+ return err
+}
diff --git a/models/repo.go b/models/repo.go
index 8c6d8b9ee..6d6f61597 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1729,6 +1729,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return fmt.Errorf("deleteBeans: %v", err)
}
+ if err := deleteLabelsByRepoID(sess, repoID); err != nil {
+ return err
+ }
+
// Delete Issues and related objects
var attachmentPaths []string
if attachmentPaths, err = deleteIssuesByRepoID(sess, repoID); err != nil {