From 801f4b9e7a49fb6366efbb1bc6fcb31c5f7b8fcc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 29 May 2020 17:41:23 +0200 Subject: [PATCH] Add tracked time fix to doctor (part of #11111) (#11138) Backports the tracked-time fix from #11111 (part of #11111) Fixes tracked time errors following repo deletion (#10280) and adds the fix to the default doctor tasks --- cmd/doctor.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 61ca27cf3..2ffdfdc5f 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -84,7 +84,7 @@ var checklist = []check{ }, { title: "Check Database Version", - name: "check-db", + name: "check-db-version", isDefault: true, f: runDoctorCheckDBVersion, abortIfFailed: true, @@ -113,6 +113,12 @@ var checklist = []check{ isDefault: false, f: runDoctorPRMergeBase, }, + { + title: "Check consistency of database", + name: "check-db-consistency", + isDefault: true, + f: runDoctorCheckDBConsistency, + }, // more checks please append here } @@ -494,3 +500,41 @@ 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) { + _, committer, err := models.TxDBContext() + if err != nil { + return nil, err + } + sess := committer.(models.Engine) + defer committer.Close() + var results []string + + //find tracked times without existing issues/pulls + count, err := sess.Table("tracked_time"). + Join("LEFT", "issue", "tracked_time.issue_id=issue.id"). + Where("issue.id is NULL"). + Count("id") + if err != nil { + return nil, err + } + if count > 0 { + if ctx.Bool("fix") { + if _, err = sess.In("id", builder.Select("tracked_time.id"). + From("tracked_time"). + Join("LEFT", "issue", "tracked_time.issue_id=issue.id"). + Where(builder.IsNull{"issue.id"})). + Delete(models.TrackedTime{}); 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)) + } + } + + if ctx.Bool("fix") { + return results, committer.Commit() + } + return results, nil +}