diff options
author | singuliere <35190819+singuliere@users.noreply.github.com> | 2022-05-10 01:49:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-10 02:49:01 +0200 |
commit | 443675d18072d2a345bc4644d3f52dee42f58b44 (patch) | |
tree | cecda6866f2918d8fe1fe081eb2cf2eb85a2ba0d /models | |
parent | 9fc194d2d9444e8fdb92abc65af412e06c19afeb (diff) | |
download | gitea-443675d18072d2a345bc4644d3f52dee42f58b44.tar.gz gitea-443675d18072d2a345bc4644d3f52dee42f58b44.zip |
[doctor] Add check/fix for bogus action rows (#19656)
Signed-off-by: Loïc Dachary <loic@dachary.org>
Co-authored-by: Loïc Dachary <loic@dachary.org>
Diffstat (limited to 'models')
-rw-r--r-- | models/consistency.go | 21 | ||||
-rw-r--r-- | models/consistency_test.go | 44 |
2 files changed, 65 insertions, 0 deletions
diff --git a/models/consistency.go b/models/consistency.go index 4a94fe25d4..30cafb7889 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" "xorm.io/builder" ) @@ -246,3 +247,23 @@ func FixIssueLabelWithOutsideLabels() (int64, error) { 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 +} diff --git a/models/consistency_test.go b/models/consistency_test.go index 1593500361..fb946b2fb7 100644 --- a/models/consistency_test.go +++ b/models/consistency_test.go @@ -11,6 +11,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" @@ -103,3 +104,46 @@ func TestUpdateMilestoneCounters(t *testing.T) { assert.NoError(t, issues_model.UpdateMilestoneCounters(db.DefaultContext, issue.MilestoneID)) unittest.CheckConsistencyFor(t, &issues_model.Milestone{}) } + +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{}) +} |