From a1c82c6108b690d102e2d04748b73b53d87a74e2 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 23 Apr 2024 11:51:52 +0800 Subject: [PATCH 1/2] Add a db consistency check to remove runners that do not belong to a repository (#30614) Follow #30406 --- models/actions/runner.go | 26 ++++++++++++++++++++++++-- modules/doctor/dbconsistency.go | 12 ++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/models/actions/runner.go b/models/actions/runner.go index aaeb80ce63964..70400df015904 100644 --- a/models/actions/runner.go +++ b/models/actions/runner.go @@ -284,7 +284,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) { // Only affect action runners were a owner ID is set, as actions runners // could also be created on a repository. return db.GetEngine(ctx).Table("action_runner"). - Join("LEFT", "user", "`action_runner`.owner_id = `user`.id"). + Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id"). Where("`action_runner`.owner_id != ?", 0). And(builder.IsNull{"`user`.id"}). Count(new(ActionRunner)) @@ -293,7 +293,7 @@ func CountRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) { func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) { subQuery := builder.Select("`action_runner`.id"). From("`action_runner`"). - Join("LEFT", "user", "`action_runner`.owner_id = `user`.id"). + Join("LEFT", "`user`", "`action_runner`.owner_id = `user`.id"). Where(builder.Neq{"`action_runner`.owner_id": 0}). And(builder.IsNull{"`user`.id"}) b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`") @@ -303,3 +303,25 @@ func FixRunnersWithoutBelongingOwner(ctx context.Context) (int64, error) { } return res.RowsAffected() } + +func CountRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) { + return db.GetEngine(ctx).Table("action_runner"). + Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id"). + Where("`action_runner`.repo_id != ?", 0). + And(builder.IsNull{"`repository`.id"}). + Count(new(ActionRunner)) +} + +func FixRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) { + subQuery := builder.Select("`action_runner`.id"). + From("`action_runner`"). + Join("LEFT", "`repository`", "`action_runner`.repo_id = `repository`.id"). + Where(builder.Neq{"`action_runner`.repo_id": 0}). + And(builder.IsNull{"`repository`.id"}) + b := builder.Delete(builder.In("id", subQuery)).From("`action_runner`") + res, err := db.GetEngine(ctx).Exec(b) + if err != nil { + return 0, err + } + return res.RowsAffected() +} diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go index 455fbb5c76a0c..93affbf1ff046 100644 --- a/modules/doctor/dbconsistency.go +++ b/modules/doctor/dbconsistency.go @@ -158,6 +158,18 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er Fixer: actions_model.FixRunnersWithoutBelongingOwner, FixedMessage: "Removed", }, + { + Name: "Action Runners without existing repository", + Counter: actions_model.CountRunnersWithoutBelongingRepo, + Fixer: actions_model.FixRunnersWithoutBelongingRepo, + FixedMessage: "Removed", + }, + { + Name: "Topics with empty repository count", + Counter: repo_model.CountOrphanedTopics, + Fixer: repo_model.DeleteOrphanedTopics, + FixedMessage: "Removed", + }, } // TODO: function to recalc all counters From de8902077b1b0eb427429d125cf9b73972c1c8c1 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Mon, 22 Apr 2024 21:46:18 +0800 Subject: [PATCH 2/2] fix cherry-pick --- modules/doctor/dbconsistency.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go index 93affbf1ff046..89c90367d26ee 100644 --- a/modules/doctor/dbconsistency.go +++ b/modules/doctor/dbconsistency.go @@ -164,12 +164,6 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er Fixer: actions_model.FixRunnersWithoutBelongingRepo, FixedMessage: "Removed", }, - { - Name: "Topics with empty repository count", - Counter: repo_model.CountOrphanedTopics, - Fixer: repo_model.DeleteOrphanedTopics, - FixedMessage: "Removed", - }, } // TODO: function to recalc all counters