Skip to content

Commit 443675d

Browse files
singuliereLoïc Dachary
andauthored
[doctor] Add check/fix for bogus action rows (#19656)
Signed-off-by: Loïc Dachary <[email protected]> Co-authored-by: Loïc Dachary <[email protected]>
1 parent 9fc194d commit 443675d

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
)
@@ -246,3 +247,23 @@ func FixIssueLabelWithOutsideLabels() (int64, error) {
246247

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

models/consistency_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
issues_model "code.gitea.io/gitea/models/issues"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
"code.gitea.io/gitea/models/unittest"
14+
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/timeutil"
1516

1617
"github.com/stretchr/testify/assert"
@@ -103,3 +104,46 @@ func TestUpdateMilestoneCounters(t *testing.T) {
103104
assert.NoError(t, issues_model.UpdateMilestoneCounters(db.DefaultContext, issue.MilestoneID))
104105
unittest.CheckConsistencyFor(t, &issues_model.Milestone{})
105106
}
107+
108+
func TestConsistencyUpdateAction(t *testing.T) {
109+
if !setting.Database.UseSQLite3 {
110+
t.Skip("Test is only for SQLite database.")
111+
}
112+
assert.NoError(t, unittest.PrepareTestDatabase())
113+
id := 8
114+
unittest.AssertExistsAndLoadBean(t, &Action{
115+
ID: int64(id),
116+
})
117+
_, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = "" WHERE id = ?`, id)
118+
assert.NoError(t, err)
119+
actions := make([]*Action, 0, 1)
120+
//
121+
// XORM returns an error when created_unix is a string
122+
//
123+
err = db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions)
124+
if assert.Error(t, err) {
125+
assert.Contains(t, err.Error(), "type string to a int64: invalid syntax")
126+
}
127+
//
128+
// Get rid of incorrectly set created_unix
129+
//
130+
count, err := CountActionCreatedUnixString()
131+
assert.NoError(t, err)
132+
assert.EqualValues(t, 1, count)
133+
count, err = FixActionCreatedUnixString()
134+
assert.NoError(t, err)
135+
assert.EqualValues(t, 1, count)
136+
137+
count, err = CountActionCreatedUnixString()
138+
assert.NoError(t, err)
139+
assert.EqualValues(t, 0, count)
140+
count, err = FixActionCreatedUnixString()
141+
assert.NoError(t, err)
142+
assert.EqualValues(t, 0, count)
143+
144+
//
145+
// XORM must be happy now
146+
//
147+
assert.NoError(t, db.GetEngine(db.DefaultContext).Where("id = ?", id).Find(&actions))
148+
unittest.CheckConsistencyFor(t, &Action{})
149+
}

modules/doctor/dbconsistency.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
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(ctx context.Context, logger log.Logger, autofix bool) er
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)