[Workaround] doctor xorm.Count nil on sqlite error (#11741)
* make it similar to v1.12&master * workaround from xorm bug * CI.restart()
This commit is contained in:
parent
88008b681d
commit
5d4251eb78
@ -502,33 +502,21 @@ func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
|
func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
|
||||||
|
var results []string
|
||||||
|
|
||||||
// make sure DB version is uptodate
|
// make sure DB version is uptodate
|
||||||
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
|
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
|
||||||
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")
|
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")
|
||||||
}
|
}
|
||||||
_, 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
|
//find tracked times without existing issues/pulls
|
||||||
count, err := sess.Table("tracked_time").
|
count, err := models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
|
||||||
Join("LEFT", "issue", "tracked_time.issue_id=issue.id").
|
|
||||||
Where("issue.id is NULL").
|
|
||||||
Count("id")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
if ctx.Bool("fix") {
|
if ctx.Bool("fix") {
|
||||||
if _, err = sess.In("id", builder.Select("tracked_time.id").
|
if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil {
|
||||||
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
|
return nil, err
|
||||||
}
|
}
|
||||||
results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
|
results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
|
||||||
@ -537,8 +525,5 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Bool("fix") {
|
|
||||||
return results, committer.Commit()
|
|
||||||
}
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
// consistencyCheckable a type that can be tested for database consistency
|
// consistencyCheckable a type that can be tested for database consistency
|
||||||
@ -167,3 +168,23 @@ func (action *Action) checkForConsistency(t *testing.T) {
|
|||||||
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository)
|
||||||
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
|
assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountOrphanedObjects count subjects with have no existing refobject anymore
|
||||||
|
func CountOrphanedObjects(subject, refobject, joinCond string) (int64, error) {
|
||||||
|
var ids []int64
|
||||||
|
|
||||||
|
return int64(len(ids)), x.Table("`"+subject+"`").
|
||||||
|
Join("LEFT", refobject, joinCond).
|
||||||
|
Where(builder.IsNull{"`" + refobject + "`.id"}).
|
||||||
|
Select("id").Find(&ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOrphanedObjects delete subjects with have no existing refobject anymore
|
||||||
|
func DeleteOrphanedObjects(subject, refobject, joinCond string) error {
|
||||||
|
_, err := x.In("id", builder.Select("`"+subject+"`.id").
|
||||||
|
From("`"+subject+"`").
|
||||||
|
Join("LEFT", "`"+refobject+"`", joinCond).
|
||||||
|
Where(builder.IsNull{"`" + refobject + "`.id"})).
|
||||||
|
Delete("`" + subject + "`")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user