Skip to content

Commit 801f4b9

Browse files
authored
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
1 parent c0c3a53 commit 801f4b9

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

cmd/doctor.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var checklist = []check{
8484
},
8585
{
8686
title: "Check Database Version",
87-
name: "check-db",
87+
name: "check-db-version",
8888
isDefault: true,
8989
f: runDoctorCheckDBVersion,
9090
abortIfFailed: true,
@@ -113,6 +113,12 @@ var checklist = []check{
113113
isDefault: false,
114114
f: runDoctorPRMergeBase,
115115
},
116+
{
117+
title: "Check consistency of database",
118+
name: "check-db-consistency",
119+
isDefault: true,
120+
f: runDoctorCheckDBConsistency,
121+
},
116122
// more checks please append here
117123
}
118124

@@ -494,3 +500,41 @@ func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
494500
}
495501
return []string{fmt.Sprintf("ScriptType %s is on the current PATH at %s", setting.ScriptType, path)}, nil
496502
}
503+
504+
func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
505+
_, committer, err := models.TxDBContext()
506+
if err != nil {
507+
return nil, err
508+
}
509+
sess := committer.(models.Engine)
510+
defer committer.Close()
511+
var results []string
512+
513+
//find tracked times without existing issues/pulls
514+
count, err := sess.Table("tracked_time").
515+
Join("LEFT", "issue", "tracked_time.issue_id=issue.id").
516+
Where("issue.id is NULL").
517+
Count("id")
518+
if err != nil {
519+
return nil, err
520+
}
521+
if count > 0 {
522+
if ctx.Bool("fix") {
523+
if _, err = sess.In("id", builder.Select("tracked_time.id").
524+
From("tracked_time").
525+
Join("LEFT", "issue", "tracked_time.issue_id=issue.id").
526+
Where(builder.IsNull{"issue.id"})).
527+
Delete(models.TrackedTime{}); err != nil {
528+
return nil, err
529+
}
530+
results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
531+
} else {
532+
results = append(results, fmt.Sprintf("%d tracked times without existing issue", count))
533+
}
534+
}
535+
536+
if ctx.Bool("fix") {
537+
return results, committer.Commit()
538+
}
539+
return results, nil
540+
}

0 commit comments

Comments
 (0)