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
This commit is contained in:
6543 2020-05-29 17:41:23 +02:00 committed by GitHub
parent c0c3a533a0
commit 801f4b9e7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}