Co-authored-by: Loïc Dachary <loic@dachary.org> Conflicts: models/consistency_test.go trivial context conflict.
This commit is contained in:
parent
c7c18e0eb2
commit
f4fb8dbc87
3 changed files with 74 additions and 0 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
@ -247,3 +248,23 @@ func FixIssueLabelWithOutsideLabels() (int64, error) {
|
||||||
|
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountActionCreatedUnixString count actions where created_unix is an empty string
|
||||||
|
func CountActionCreatedUnixString() (int64, error) {
|
||||||
|
if setting.Database.UseSQLite3 {
|
||||||
|
return db.GetEngine(db.DefaultContext).Where(`created_unix = ""`).Count(new(Action))
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FixActionCreatedUnixString set created_unix to zero if it is an empty string
|
||||||
|
func FixActionCreatedUnixString() (int64, error) {
|
||||||
|
if setting.Database.UseSQLite3 {
|
||||||
|
res, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -33,3 +34,46 @@ func TestDeleteOrphanedObjects(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, countBefore, countAfter)
|
assert.EqualValues(t, countBefore, countAfter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConsistencyUpdateAction(t *testing.T) {
|
||||||
|
if !setting.Database.UseSQLite3 {
|
||||||
|
t.Skip("Test is only for SQLite database.")
|
||||||
|
}
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
id := 8
|
||||||
|
unittest.AssertExistsAndLoadBean(t, &Action{
|
||||||
|
ID: int64(id),
|
||||||
|
})
|
||||||
|
_, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = "" WHERE id = ?`, id)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
actions := make([]*Action, 0, 1)
|
||||||
|
//
|
||||||
|
// XORM returns an error when created_unix is a string
|
||||||
|
//
|
||||||
|
err = db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions)
|
||||||
|
if assert.Error(t, err) {
|
||||||
|
assert.Contains(t, err.Error(), "type string to a int64: invalid syntax")
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// Get rid of incorrectly set created_unix
|
||||||
|
//
|
||||||
|
count, err := CountActionCreatedUnixString()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, count)
|
||||||
|
count, err = FixActionCreatedUnixString()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 1, count)
|
||||||
|
|
||||||
|
count, err = CountActionCreatedUnixString()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 0, count)
|
||||||
|
count, err = FixActionCreatedUnixString()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 0, count)
|
||||||
|
|
||||||
|
//
|
||||||
|
// XORM must be happy now
|
||||||
|
//
|
||||||
|
assert.NoError(t, db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions))
|
||||||
|
unittest.CheckConsistencyFor(t, &Action{})
|
||||||
|
}
|
||||||
|
|
|
@ -142,6 +142,12 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
|
||||||
Fixer: models.FixIssueLabelWithOutsideLabels,
|
Fixer: models.FixIssueLabelWithOutsideLabels,
|
||||||
FixedMessage: "Removed",
|
FixedMessage: "Removed",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Action with created_unix set as an empty string",
|
||||||
|
Counter: models.CountActionCreatedUnixString,
|
||||||
|
Fixer: models.FixActionCreatedUnixString,
|
||||||
|
FixedMessage: "Set to zero",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: function to recalc all counters
|
// TODO: function to recalc all counters
|
||||||
|
@ -177,6 +183,9 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
|
||||||
// find access without repository
|
// find access without repository
|
||||||
genericOrphanCheck("Access entries without existing repository",
|
genericOrphanCheck("Access entries without existing repository",
|
||||||
"access", "repository", "access.repo_id=repository.id"),
|
"access", "repository", "access.repo_id=repository.id"),
|
||||||
|
// find action without repository
|
||||||
|
genericOrphanCheck("Action entries without existing repository",
|
||||||
|
"action", "repository", "action.repo_id=repository.id"),
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, c := range consistencyChecks {
|
for _, c := range consistencyChecks {
|
||||||
|
|
Reference in a new issue