From e0e18fc2866da792d36bc4860d3e670b06ccaac9 Mon Sep 17 00:00:00 2001 From: kiara Date: Sat, 3 Oct 2026 11:19:27 +0200 Subject: [PATCH] perf(actions): index `action_run.commit_sha` (#39559) The API filter `head_sha` on `GET /repos/{owner}/{repo}/actions/runs` selects runs by `commit_sha`, so `commit_sha` needs an index. For a action_run table with 212k rows: - Without the index: the query read 212k rows, and the API request took 35-52 s. - With the index: the query read 94 rows in 0.14 s, and the API request took 2-4 s. --------- Co-authored-by: wxiaoguang --- modelmigration/migrations.go | 3 +++ modelmigration/v28/v356.go | 25 +++++++++++++++++++++++++ models/actions/run.go | 6 +++--- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 modelmigration/v28/v356.go diff --git a/modelmigration/migrations.go b/modelmigration/migrations.go index 694cf0257c6..2e58a2468ba 100644 --- a/modelmigration/migrations.go +++ b/modelmigration/migrations.go @@ -429,6 +429,9 @@ func prepareMigrationTasks() []*migration { newMigration(353, "Add audit event table", v28.AddAuditEventTable), newMigration(354, "Add Actions job queue indexes", v28.AddActionQueueIndexes), newMigration(355, "Add AutoMerge merged_commit_id column", v28.AddAutoMergeMergedCommitID), + // Gitea 28.0.0 ends at migration ID number 355 (database version 356) + + newMigration(356, "Add index on action_run commit_sha", v28.AddActionRunCommitSHAIndex), } return preparedMigrations } diff --git a/modelmigration/v28/v356.go b/modelmigration/v28/v356.go new file mode 100644 index 00000000000..4b119672968 --- /dev/null +++ b/modelmigration/v28/v356.go @@ -0,0 +1,25 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v28 + +import ( + "context" + + "gitea.dev/modelmigration/base" + + "xorm.io/xorm" +) + +// AddActionRunCommitSHAIndex indexes the runs lookup by commit, which the API `head_sha` filter uses. +func AddActionRunCommitSHAIndex(_ context.Context, x base.EngineMigration) error { + type ActionRun struct { + CommitSHA string `xorm:"index"` + } + + _, err := x.SyncWithOptions(xorm.SyncOptions{ + IgnoreDropIndices: true, + IgnoreConstrains: true, + }, new(ActionRun)) + return err +} diff --git a/models/actions/run.go b/models/actions/run.go index 2ca797b8f33..e76ccccdb72 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -39,9 +39,9 @@ type ActionRun struct { TriggerUserID int64 `xorm:"index"` TriggerUser *user_model.User `xorm:"-"` ScheduleID int64 - Ref string `xorm:"index"` // the commit/tag/… that caused the run - IsRefDeleted bool `xorm:"-"` - CommitSHA string + Ref string `xorm:"index"` // the commit/tag/… that caused the run + IsRefDeleted bool `xorm:"-"` + CommitSHA string `xorm:"index"` IsForkPullRequest bool // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow. NeedApproval bool // may need approval if it's a fork pull request ApprovedBy int64 `xorm:"index"` // who approved