aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/doctor.go
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-05-29 15:24:15 +0200
committerGitHub <noreply@github.com>2020-05-29 14:24:15 +0100
commit726e1e5279b6461cad8eee2e461c2c6d8ab0f075 (patch)
tree36ea94e5a435237af1ff7e7badd057892467fc7a /cmd/doctor.go
parentab73b5657597043d0588976ba14a86fb2ce3a413 (diff)
downloadgitea-726e1e5279b6461cad8eee2e461c2c6d8ab0f075.tar.gz
gitea-726e1e5279b6461cad8eee2e461c2c6d8ab0f075.zip
Doctor check & fix db consistency (#11111)
needed to fix issue as described in #10280 * rename check-db to check-db-version * add check-db-consistency: * find issues without existing repository * find pulls without existing issues * find tracked times without existing issues/pulls * find labels without repository or org reference Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'cmd/doctor.go')
-rw-r--r--cmd/doctor.go87
1 files changed, 85 insertions, 2 deletions
diff --git a/cmd/doctor.go b/cmd/doctor.go
index f469496cfd..3456f26f70 100644
--- a/cmd/doctor.go
+++ b/cmd/doctor.go
@@ -85,10 +85,16 @@ var checklist = []check{
},
{
title: "Check Database Version",
- name: "check-db",
+ name: "check-db-version",
isDefault: true,
f: runDoctorCheckDBVersion,
- abortIfFailed: true,
+ abortIfFailed: false,
+ },
+ {
+ title: "Check consistency of database",
+ name: "check-db-consistency",
+ isDefault: false,
+ f: runDoctorCheckDBConsistency,
},
{
title: "Check if OpenSSH authorized_keys file is up-to-date",
@@ -495,3 +501,80 @@ func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
}
return []string{fmt.Sprintf("ScriptType %s is on the current PATH at %s", setting.ScriptType, path)}, nil
}
+
+func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
+ var results []string
+
+ // make sure DB version is uptodate
+ if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
+ return nil, fmt.Errorf("model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
+ }
+
+ //find labels without existing repo or org
+ count, err := models.CountOrphanedLabels()
+ if err != nil {
+ return nil, err
+ }
+ if count > 0 {
+ if ctx.Bool("fix") {
+ if err = models.DeleteOrphanedLabels(); err != nil {
+ return nil, err
+ }
+ results = append(results, fmt.Sprintf("%d labels without existing repository/organisation deleted", count))
+ } else {
+ results = append(results, fmt.Sprintf("%d labels without existing repository/organisation", count))
+ }
+ }
+
+ //find issues without existing repository
+ count, err = models.CountOrphanedIssues()
+ if err != nil {
+ return nil, err
+ }
+ if count > 0 {
+ if ctx.Bool("fix") {
+ if err = models.DeleteOrphanedIssues(); err != nil {
+ return nil, err
+ }
+ results = append(results, fmt.Sprintf("%d issues without existing repository deleted", count))
+ } else {
+ results = append(results, fmt.Sprintf("%d issues without existing repository", count))
+ }
+ }
+
+ //find pulls without existing issues
+ count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
+ if err != nil {
+ return nil, err
+ }
+ if count > 0 {
+ if ctx.Bool("fix") {
+ if err = models.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id"); err != nil {
+ return nil, err
+ }
+ results = append(results, fmt.Sprintf("%d pull requests without existing issue deleted", count))
+ } else {
+ results = append(results, fmt.Sprintf("%d pull requests without existing issue", count))
+ }
+ }
+
+ //find tracked times without existing issues/pulls
+ count, err = models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
+ if err != nil {
+ return nil, err
+ }
+ if count > 0 {
+ if ctx.Bool("fix") {
+ if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil {
+ return nil, err
+ }
+ results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
+ } else {
+ results = append(results, fmt.Sprintf("%d tracked times without existing issue", count))
+ }
+ }
+
+ //ToDo: function to recalc all counters
+
+ return results, nil
+}