-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add commit status summary table to reduce query from commit status table #30223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9e7185b
Add commit status summary table to reduce query from commit status ta…
lunny 6236384
Correct some names
lunny ebad824
Merge branch 'main' into lunny/commit_status_summary
lunny 7b7a5b5
Merge branch 'main' into lunny/commit_status_summary
lunny 28d357e
Fix upset for mysql
lunny 6b5a0b4
Merge branch 'main' into lunny/commit_status_summary
lunny 8db71d0
Add a comment for upset
lunny 1beb182
Add test for commit status summary
lunny a653809
Apply suggestions from code review
lunny 4a5c310
Merge branch 'main' into lunny/commit_status_summary
lunny 32b6790
Rename index name on commist_status_summary table
lunny 76762a7
Merge branch 'main' into lunny/commit_status_summary
lunny eabaf20
Merge branch 'lunny/commit_status_summary' of github.com:lunny/gitea …
lunny 027ae02
Fix variable name
lunny d46bdd1
revert unnecessary removed blank line
lunny 23a143d
Merge branch 'main' into lunny/commit_status_summary
lunny 3da3a5e
Merge branch 'main' into lunny/commit_status_summary
GiteaBot a9e1af7
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 5288036
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 5fcddae
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 85facdd
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 4602ebd
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 3556715
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 923bb0a
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 52a22a0
Merge branch 'main' into lunny/commit_status_summary
GiteaBot 0ae1d48
Merge branch 'main' into lunny/commit_status_summary
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024 Gitea. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
// CommitStatusSummary holds the latest commit Status of a single Commit | ||
type CommitStatusSummary struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX UNIQUE(repo_id_sha)"` | ||
SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_id_sha)"` | ||
State api.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"` | ||
} | ||
|
||
func init() { | ||
db.RegisterModel(new(CommitStatusSummary)) | ||
} | ||
|
||
type RepoSHA struct { | ||
RepoID int64 | ||
SHA string | ||
} | ||
|
||
func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA) ([]*CommitStatus, error) { | ||
cond := builder.NewCond() | ||
for _, rs := range repoSHAs { | ||
cond = cond.Or(builder.Eq{"repo_id": rs.RepoID, "sha": rs.SHA}) | ||
} | ||
|
||
var summaries []CommitStatusSummary | ||
if err := db.GetEngine(ctx).Where(cond).Find(&summaries); err != nil { | ||
return nil, err | ||
} | ||
|
||
commitStatuses := make([]*CommitStatus, 0, len(repoSHAs)) | ||
for _, summary := range summaries { | ||
commitStatuses = append(commitStatuses, &CommitStatus{ | ||
RepoID: summary.RepoID, | ||
SHA: summary.SHA, | ||
State: summary.State, | ||
}) | ||
} | ||
return commitStatuses, nil | ||
} | ||
|
||
func UpdateCommitStatusSummary(ctx context.Context, repoID int64, sha string) error { | ||
commitStatuses, _, err := GetLatestCommitStatus(ctx, repoID, sha, db.ListOptionsAll) | ||
if err != nil { | ||
return err | ||
} | ||
state := CalcCommitStatus(commitStatuses) | ||
// 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) VALUES (?,?,?) ON DUPLICATE KEY UPDATE state=?", | ||
repoID, sha, state.State, state.State) | ||
return err | ||
} | ||
|
||
if cnt, err := db.GetEngine(ctx).Where("repo_id=? AND sha=?", repoID, sha). | ||
Cols("state"). | ||
Update(&CommitStatusSummary{ | ||
State: state.State, | ||
}); err != nil { | ||
return err | ||
} else if cnt == 0 { | ||
_, err = db.GetEngine(ctx).Insert(&CommitStatusSummary{ | ||
RepoID: repoID, | ||
SHA: sha, | ||
State: state.State, | ||
}) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_23 //nolint | ||
|
||
import "xorm.io/xorm" | ||
|
||
func AddCommitStatusSummary(x *xorm.Engine) error { | ||
type CommitStatusSummary struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX UNIQUE(repo_id_sha)"` | ||
SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_id_sha)"` | ||
State string `xorm:"VARCHAR(7) NOT NULL"` | ||
} | ||
// there is no migrations because if there is no data on this table, it will fall back to get data | ||
// from commit status | ||
return x.Sync2(new(CommitStatusSummary)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.