Skip to content

Commit a1c82c6

Browse files
committed
Add a db consistency check to remove runners that do not belong to a repository (go-gitea#30614)
Follow go-gitea#30406
1 parent 1c1c94b commit a1c82c6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

models/actions/runner.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
284284
// Only affect action runners were a owner ID is set, as actions runners
285285
// could also be created on a repository.
286286
return db.GetEngine(ctx).Table("action_runner").
287-
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
287+
Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id").
288288
Where("`action_runner`.owner_id != ?", 0).
289289
And(builder.IsNull{"`user`.id"}).
290290
Count(new(ActionRunner))
@@ -293,7 +293,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
293293
func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
294294
subQuery := builder.Select("`action_runner`.id").
295295
From("`action_runner`").
296-
Join("LEFT", "user", "`action_runner`.owner_id = `user`.id").
296+
Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id").
297297
Where(builder.Neq{"`action_runner`.owner_id": 0}).
298298
And(builder.IsNull{"`user`.id"})
299299
b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
@@ -303,3 +303,25 @@ func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) {
303303
}
304304
return res.RowsAffected()
305305
}
306+
307+
func CountRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
308+
return db.GetEngine(ctx).Table("action_runner").
309+
Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id").
310+
Where("`action_runner`.repo_id != ?", 0).
311+
And(builder.IsNull{"`repository`.id"}).
312+
Count(new(ActionRunner))
313+
}
314+
315+
func FixRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
316+
subQuery := builder.Select("`action_runner`.id").
317+
From("`action_runner`").
318+
Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id").
319+
Where(builder.Neq{"`action_runner`.repo_id": 0}).
320+
And(builder.IsNull{"`repository`.id"})
321+
b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`")
322+
res, err := db.GetEngine(ctx).Exec(b)
323+
if err != nil {
324+
return 0, err
325+
}
326+
return res.RowsAffected()
327+
}

modules/doctor/dbconsistency.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
158158
Fixer: actions_model.FixRunnersWithoutBelongingOwner,
159159
FixedMessage: "Removed",
160160
},
161+
{
162+
Name: "Action Runners without existing repository",
163+
Counter: actions_model.CountRunnersWithoutBelongingRepo,
164+
Fixer: actions_model.FixRunnersWithoutBelongingRepo,
165+
FixedMessage: "Removed",
166+
},
167+
{
168+
Name: "Topics with empty repository count",
169+
Counter: repo_model.CountOrphanedTopics,
170+
Fixer: repo_model.DeleteOrphanedTopics,
171+
FixedMessage: "Removed",
172+
},
161173
}
162174

163175
// TODO: function to recalc all counters

0 commit comments

Comments
 (0)