Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracked time fix to doctor (part of #11111) #11138

Merged
merged 10 commits into from
May 29, 2020
46 changes: 45 additions & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var checklist = []check{
},
{
title: "Check Database Version",
name: "check-db",
name: "check-db-version",
isDefault: true,
f: runDoctorCheckDBVersion,
abortIfFailed: true,
Expand Down Expand Up @@ -113,6 +113,12 @@ var checklist = []check{
isDefault: false,
f: runDoctorPRMergeBase,
},
{
title: "Check consistency of database",
name: "check-db-consistency",
isDefault: false,
6543 marked this conversation as resolved.
Show resolved Hide resolved
f: runDoctorCheckDBConsistency,
},
// more checks please append here
}

Expand Down Expand Up @@ -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
}