Skip to content

Commit 5d4251e

Browse files
authored
[Workaround] doctor xorm.Count nil on sqlite error (#11741)
* make it similar to v1.12&master * workaround from xorm bug * CI.restart()
1 parent 88008b6 commit 5d4251e

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

cmd/doctor.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -502,33 +502,21 @@ func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
502502
}
503503

504504
func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
505+
var results []string
506+
505507
// make sure DB version is uptodate
506508
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
507509
return nil, fmt.Errorf("model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
508510
}
509-
_, committer, err := models.TxDBContext()
510-
if err != nil {
511-
return nil, err
512-
}
513-
sess := committer.(models.Engine)
514-
defer committer.Close()
515-
var results []string
516511

517512
//find tracked times without existing issues/pulls
518-
count, err := sess.Table("tracked_time").
519-
Join("LEFT", "issue", "tracked_time.issue_id=issue.id").
520-
Where("issue.id is NULL").
521-
Count("id")
513+
count, err := models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
522514
if err != nil {
523515
return nil, err
524516
}
525517
if count > 0 {
526518
if ctx.Bool("fix") {
527-
if _, err = sess.In("id", builder.Select("tracked_time.id").
528-
From("tracked_time").
529-
Join("LEFT", "issue", "tracked_time.issue_id=issue.id").
530-
Where(builder.IsNull{"issue.id"})).
531-
Delete(models.TrackedTime{}); err != nil {
519+
if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil {
532520
return nil, err
533521
}
534522
results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
@@ -537,8 +525,5 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
537525
}
538526
}
539527

540-
if ctx.Bool("fix") {
541-
return results, committer.Commit()
542-
}
543528
return results, nil
544529
}

models/consistency.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/stretchr/testify/assert"
13+
"xorm.io/builder"
1314
)
1415

1516
// consistencyCheckable a type that can be tested for database consistency
@@ -167,3 +168,23 @@ func (action *Action) checkForConsistency(t *testing.T) {
167168
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
168169
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
169170
}
171+
172+
// CountOrphanedObjects count subjects with have no existing refobject anymore
173+
func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
174+
var ids []int64
175+
176+
return int64(len(ids)), x.Table("`"+subject+"`").
177+
Join("LEFT", refobject, joinCond).
178+
Where(builder.IsNull{"`" + refobject + "`.id"}).
179+
Select("id").Find(&ids)
180+
}
181+
182+
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
183+
func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
184+
_, err := x.In("id", builder.Select("`"+subject+"`.id").
185+
From("`"+subject+"`").
186+
Join("LEFT", "`"+refobject+"`", joinCond).
187+
Where(builder.IsNull{"`" + refobject + "`.id"})).
188+
Delete("`" + subject + "`")
189+
return err
190+
}

0 commit comments

Comments
 (0)