@@ -84,7 +84,7 @@ var checklist = []check{
84
84
},
85
85
{
86
86
title : "Check Database Version" ,
87
- name : "check-db" ,
87
+ name : "check-db-version " ,
88
88
isDefault : true ,
89
89
f : runDoctorCheckDBVersion ,
90
90
abortIfFailed : true ,
@@ -113,6 +113,12 @@ var checklist = []check{
113
113
isDefault : false ,
114
114
f : runDoctorPRMergeBase ,
115
115
},
116
+ {
117
+ title : "Check consistency of database" ,
118
+ name : "check-db-consistency" ,
119
+ isDefault : true ,
120
+ f : runDoctorCheckDBConsistency ,
121
+ },
116
122
// more checks please append here
117
123
}
118
124
@@ -494,3 +500,41 @@ func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
494
500
}
495
501
return []string {fmt .Sprintf ("ScriptType %s is on the current PATH at %s" , setting .ScriptType , path )}, nil
496
502
}
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