From 2d84f79e3b5bdf8146ae019f0774dedbbbe55a7e Mon Sep 17 00:00:00 2001 From: Giteabot Date: Fri, 20 Nov 2026 11:41:12 +0000 Subject: [PATCH] perf: avoid commit status summary full table scan (#40451) (#40459) Backport #40451 by @silverwind The dashboard repo list read the entire `commit_status_summary` table on every load when the page held an empty repo and the other repos' statuses were cached, about 1.2 s with 1M rows. Those other repos also showed and cached the status of an arbitrary older commit. Also fixes the MySQL upsert not updating `target_url`. Assisted-by: Claude Code:claude-opus-5-5 Co-authored-by: silverwind --- models/git/commit_status_summary.go | 7 +++++-- models/git/commit_status_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/models/git/commit_status_summary.go b/models/git/commit_status_summary.go index 4ab1beec923..f50fa04c5e8 100644 --- a/models/git/commit_status_summary.go +++ b/models/git/commit_status_summary.go @@ -32,6 +32,9 @@ type RepoSHA struct { } func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA) ([]*CommitStatus, error) { + if len(repoSHAs) == 0 { + return nil, nil + } cond := builder.NewCond() for _, rs := range repoSHAs { cond = cond.Or(builder.Eq{"repo_id": rs.RepoID, "sha": rs.SHA}) @@ -67,8 +70,8 @@ func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) er // mysql will return 0 when update a record which state hasn't been changed which behaviour is different from other database, // so we need to use insert in on duplicate if setting.Database.Type.IsMySQL() { - _, err := db.GetEngine(ctx).Exec("INSERT INTO commit_status_summary (repo_id,sha,state,target_url) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?", - repoID, sha, state.State, state.TargetURL, state.State) + _, err := db.GetEngine(ctx).Exec("INSERT INTO commit_status_summary (repo_id,sha,state,target_url) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?, target_url=?", + repoID, sha, state.State, state.TargetURL, state.State, state.TargetURL) return err } diff --git a/models/git/commit_status_test.go b/models/git/commit_status_test.go index 954e444e8da..20ec0d9dc37 100644 --- a/models/git/commit_status_test.go +++ b/models/git/commit_status_test.go @@ -89,6 +89,13 @@ func TestGetCommitStatuses(t *testing.T) { "deploy/awesomeness": 5, }, latestIndexes) }) + + t.Run("GetLatestCommitStatusForRepoAndSHAsEmpty", func(t *testing.T) { + assert.NoError(t, git_model.UpdateCommitStatusSummary(t.Context(), repo1.ID, sha1)) + statuses, err := git_model.GetLatestCommitStatusForRepoAndSHAs(t.Context(), nil) + assert.NoError(t, err) + assert.Empty(t, statuses) + }) } func Test_CalcCommitStatus(t *testing.T) {