Skip to content

Commit f4fb8db

Browse files
authored
[doctor] Add check/fix for bogus action rows (#19656) (#19669)
Co-authored-by: Loïc Dachary <[email protected]> Conflicts: models/consistency_test.go trivial context conflict.
1 parent c7c18e0 commit f4fb8db

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

models/consistency.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"code.gitea.io/gitea/models/db"
1010
repo_model "code.gitea.io/gitea/models/repo"
1111
user_model "code.gitea.io/gitea/models/user"
12+
"code.gitea.io/gitea/modules/setting"
1213

1314
"xorm.io/builder"
1415
)
@@ -247,3 +248,23 @@ func FixIssueLabelWithOutsideLabels() (int64, error) {
247248

248249
return res.RowsAffected()
249250
}
251+
252+
// CountActionCreatedUnixString count actions where created_unix is an empty string
253+
func CountActionCreatedUnixString() (int64, error) {
254+
if setting.Database.UseSQLite3 {
255+
return db.GetEngine(db.DefaultContext).Where(`created_unix = ""`).Count(new(Action))
256+
}
257+
return 0, nil
258+
}
259+
260+
// FixActionCreatedUnixString set created_unix to zero if it is an empty string
261+
func FixActionCreatedUnixString() (int64, error) {
262+
if setting.Database.UseSQLite3 {
263+
res, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
264+
if err != nil {
265+
return 0, err
266+
}
267+
return res.RowsAffected()
268+
}
269+
return 0, nil
270+
}

models/consistency_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models/db"
1111
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/setting"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
@@ -33,3 +34,46 @@ func TestDeleteOrphanedObjects(t *testing.T) {
3334
assert.NoError(t, err)
3435
assert.EqualValues(t, countBefore, countAfter)
3536
}
37+
38+
func TestConsistencyUpdateAction(t *testing.T) {
39+
if !setting.Database.UseSQLite3 {
40+
t.Skip("Test is only for SQLite database.")
41+
}
42+
assert.NoError(t, unittest.PrepareTestDatabase())
43+
id := 8
44+
unittest.AssertExistsAndLoadBean(t, &Action{
45+
ID: int64(id),
46+
})
47+
_, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = "" WHERE id = ?`, id)
48+
assert.NoError(t, err)
49+
actions := make([]*Action, 0, 1)
50+
//
51+
// XORM returns an error when created_unix is a string
52+
//
53+
err = db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions)
54+
if assert.Error(t, err) {
55+
assert.Contains(t, err.Error(), "type string to a int64: invalid syntax")
56+
}
57+
//
58+
// Get rid of incorrectly set created_unix
59+
//
60+
count, err := CountActionCreatedUnixString()
61+
assert.NoError(t, err)
62+
assert.EqualValues(t, 1, count)
63+
count, err = FixActionCreatedUnixString()
64+
assert.NoError(t, err)
65+
assert.EqualValues(t, 1, count)
66+
67+
count, err = CountActionCreatedUnixString()
68+
assert.NoError(t, err)
69+
assert.EqualValues(t, 0, count)
70+
count, err = FixActionCreatedUnixString()
71+
assert.NoError(t, err)
72+
assert.EqualValues(t, 0, count)
73+
74+
//
75+
// XORM must be happy now
76+
//
77+
assert.NoError(t, db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions))
78+
unittest.CheckConsistencyFor(t, &Action{})
79+
}

modules/doctor/dbconsistency.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
142142
Fixer: models.FixIssueLabelWithOutsideLabels,
143143
FixedMessage: "Removed",
144144
},
145+
{
146+
Name: "Action with created_unix set as an empty string",
147+
Counter: models.CountActionCreatedUnixString,
148+
Fixer: models.FixActionCreatedUnixString,
149+
FixedMessage: "Set to zero",
150+
},
145151
}
146152

147153
// TODO: function to recalc all counters
@@ -177,6 +183,9 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
177183
// find access without repository
178184
genericOrphanCheck("Access entries without existing repository",
179185
"access", "repository", "access.repo_id=repository.id"),
186+
// find action without repository
187+
genericOrphanCheck("Action entries without existing repository",
188+
"action", "repository", "action.repo_id=repository.id"),
180189
)
181190

182191
for _, c := range consistencyChecks {

0 commit comments

Comments
 (0)