Skip to content

Commit 30dd4be

Browse files
authored
Add a db consistency check to remove runners that do not belong to a repository (#30614)
Follow #30406
1 parent e94864e commit 30dd4be

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

models/actions/runner.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
270270
// Only affect action runners were a owner ID is set, as actions runners
271271
// could also be created on a repository.
272272
return db.GetEngine(ctx).Table("action_runner").
273-
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
273+
Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id").
274274
Where("`action_runner`.owner_id != ?", 0).
275275
And(builder.IsNull{"`user`.id"}).
276276
Count(new(ActionRunner))
@@ -279,7 +279,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
279279
func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
280280
subQuery := builder.Select("`action_runner`.id").
281281
From("`action_runner`").
282-
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
282+
Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id").
283283
Where(builder.Neq{"`action_runner`.owner_id": 0}).
284284
And(builder.IsNull{"`user`.id"})
285285
b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
@@ -289,3 +289,25 @@ func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
289289
}
290290
return res.RowsAffected()
291291
}
292+
293+
func CountRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
294+
return db.GetEngine(ctx).Table("action_runner").
295+
Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id").
296+
Where("`action_runner`.repo_id != ?", 0).
297+
And(builder.IsNull{"`repository`.id"}).
298+
Count(new(ActionRunner))
299+
}
300+
301+
func FixRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
302+
subQuery := builder.Select("`action_runner`.id").
303+
From("`action_runner`").
304+
Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id").
305+
Where(builder.Neq{"`action_runner`.repo_id": 0}).
306+
And(builder.IsNull{"`repository`.id"})
307+
b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
308+
res, err := db.GetEngine(ctx).Exec(b)
309+
if err != nil {
310+
return 0, err
311+
}
312+
return res.RowsAffected()
313+
}

services/doctor/dbconsistency.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ func prepareDBConsistencyChecks() []consistencyCheck {
152152
Fixer: actions_model.FixRunnersWithoutBelongingOwner,
153153
FixedMessage: "Removed",
154154
},
155+
{
156+
Name: "Action Runners without existing repository",
157+
Counter: actions_model.CountRunnersWithoutBelongingRepo,
158+
Fixer: actions_model.FixRunnersWithoutBelongingRepo,
159+
FixedMessage: "Removed",
160+
},
155161
{
156162
Name: "Topics with empty repository count",
157163
Counter: repo_model.CountOrphanedTopics,

0 commit comments

Comments
 (0)