From 5af01b412e8b7e52ba7092e0919d89ac7db73b30 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 17 Apr 2020 16:29:57 +0200 Subject: [PATCH 1/6] rename check-db to check-db-version to have multible db checks --- cmd/doctor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 61ca27cf3f4cd..16ce7f7915d1a 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, From 48fdc79dd8099a97820befbfe7659cfd47a5c00d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 17 Apr 2020 17:23:51 +0200 Subject: [PATCH 2/6] add runDoctorCheckDBConsistency --- cmd/doctor.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/cmd/doctor.go b/cmd/doctor.go index 16ce7f7915d1a..5bdf14000d2d3 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -113,6 +113,12 @@ var checklist = []check{ isDefault: false, f: runDoctorPRMergeBase, }, + { + title: "Check consistency of database", + name: "check-db-consistency", + isDefault: false, + f: runDoctorCheckDBConsistency, + }, // more checks please append here } @@ -494,3 +500,64 @@ 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 issues without existing repository + count, err := sess.Table("issue"). + Join("LEFT", "repository", "issue.repo_id=repository.id"). + Where("repository.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("issue.id"). + From("issue"). + Join("LEFT", "repository", "issue.repo_id=repository.id"). + Where(builder.IsNull{"repository.id"})). + Delete(models.Issue{}); err != nil { + return nil, err + } + results = append(results, fmt.Sprintf("%d issues without existing repository deleted", count)) + } else { + results = append(results, fmt.Sprintf("%d issues without existing repository", count)) + } + } + + //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 +} From 82deca5202c73905ef1c8234bdb7a264863e790e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 17 Apr 2020 17:29:22 +0200 Subject: [PATCH 3/6] add find pulls without existing issues --- cmd/doctor.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/doctor.go b/cmd/doctor.go index 5bdf14000d2d3..0f738e969522b 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -533,6 +533,29 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) { } } + //find pulls without existing issues + count, err = sess.Table("pull_request"). + Join("LEFT", "issue", "pull_request.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("pull_request.id"). + From("pull_request"). + Join("LEFT", "issue", "pull_request.issue_id=issue.id"). + Where(builder.IsNull{"issue.id"})). + Delete(models.PullRequest{}); err != nil { + return nil, err + } + results = append(results, fmt.Sprintf("%d pull requests without existing issue deleted", count)) + } else { + results = append(results, fmt.Sprintf("%d pull requests without existing issue", count)) + } + } + //find tracked times without existing issues/pulls count, err = sess.Table("tracked_time"). Join("LEFT", "issue", "tracked_time.issue_id=issue.id"). From 95c5df04140e1561a9f7f132114f9d1a1b060047 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 19 Apr 2020 18:43:24 +0200 Subject: [PATCH 4/6] prepare for v1.11.5 --- cmd/doctor.go | 48 +----------------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 0f738e969522b..ead0b7d8fcec2 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -510,54 +510,8 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) { defer committer.Close() var results []string - //find issues without existing repository - count, err := sess.Table("issue"). - Join("LEFT", "repository", "issue.repo_id=repository.id"). - Where("repository.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("issue.id"). - From("issue"). - Join("LEFT", "repository", "issue.repo_id=repository.id"). - Where(builder.IsNull{"repository.id"})). - Delete(models.Issue{}); err != nil { - return nil, err - } - results = append(results, fmt.Sprintf("%d issues without existing repository deleted", count)) - } else { - results = append(results, fmt.Sprintf("%d issues without existing repository", count)) - } - } - - //find pulls without existing issues - count, err = sess.Table("pull_request"). - Join("LEFT", "issue", "pull_request.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("pull_request.id"). - From("pull_request"). - Join("LEFT", "issue", "pull_request.issue_id=issue.id"). - Where(builder.IsNull{"issue.id"})). - Delete(models.PullRequest{}); err != nil { - return nil, err - } - results = append(results, fmt.Sprintf("%d pull requests without existing issue deleted", count)) - } else { - results = append(results, fmt.Sprintf("%d pull requests without existing issue", count)) - } - } - //find tracked times without existing issues/pulls - count, err = sess.Table("tracked_time"). + count, err := sess.Table("tracked_time"). Join("LEFT", "issue", "tracked_time.issue_id=issue.id"). Where("issue.id is NULL"). Count("id") From f4eb8e056e98ce026f88e35d2879a4cfafb0346c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 19 Apr 2020 18:47:04 +0200 Subject: [PATCH 5/6] make this check ture for v1.11 gitea's --- cmd/doctor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index ead0b7d8fcec2..2ffdfdc5f7ee2 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -116,7 +116,7 @@ var checklist = []check{ { title: "Check consistency of database", name: "check-db-consistency", - isDefault: false, + isDefault: true, f: runDoctorCheckDBConsistency, }, // more checks please append here From 6fc24b87d7818a96adf08b996e29e43d89a0d329 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 29 May 2020 17:12:15 +0200 Subject: [PATCH 6/6] CI.restart()