1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

more context for models (#19511)

make more usage of context, to have more db transaction in one session

(make diff of  #9307 smaller)
This commit is contained in:
6543
2022-04-28 13:48:48 +02:00
committed by GitHub
parent 332b2ecd21
commit 06e4687cec
54 changed files with 275 additions and 245 deletions

View File

@@ -130,7 +130,8 @@ func (pr *PullRequest) LoadAttributes() error {
return pr.loadAttributes(db.GetEngine(db.DefaultContext))
}
func (pr *PullRequest) loadHeadRepo(ctx context.Context) (err error) {
// LoadHeadRepoCtx loads the head repository
func (pr *PullRequest) LoadHeadRepoCtx(ctx context.Context) (err error) {
if !pr.isHeadRepoLoaded && pr.HeadRepo == nil && pr.HeadRepoID > 0 {
if pr.HeadRepoID == pr.BaseRepoID {
if pr.BaseRepo != nil {
@@ -153,15 +154,16 @@ func (pr *PullRequest) loadHeadRepo(ctx context.Context) (err error) {
// LoadHeadRepo loads the head repository
func (pr *PullRequest) LoadHeadRepo() error {
return pr.loadHeadRepo(db.DefaultContext)
return pr.LoadHeadRepoCtx(db.DefaultContext)
}
// LoadBaseRepo loads the target repository
func (pr *PullRequest) LoadBaseRepo() error {
return pr.loadBaseRepo(db.DefaultContext)
return pr.LoadBaseRepoCtx(db.DefaultContext)
}
func (pr *PullRequest) loadBaseRepo(ctx context.Context) (err error) {
// LoadBaseRepoCtx loads the target repository
func (pr *PullRequest) LoadBaseRepoCtx(ctx context.Context) (err error) {
if pr.BaseRepo != nil {
return nil
}
@@ -185,15 +187,16 @@ func (pr *PullRequest) loadBaseRepo(ctx context.Context) (err error) {
// LoadIssue loads issue information from database
func (pr *PullRequest) LoadIssue() (err error) {
return pr.loadIssue(db.GetEngine(db.DefaultContext))
return pr.LoadIssueCtx(db.DefaultContext)
}
func (pr *PullRequest) loadIssue(e db.Engine) (err error) {
// LoadIssueCtx loads issue information from database
func (pr *PullRequest) LoadIssueCtx(ctx context.Context) (err error) {
if pr.Issue != nil {
return nil
}
pr.Issue, err = getIssueByID(e, pr.IssueID)
pr.Issue, err = getIssueByID(db.GetEngine(ctx), pr.IssueID)
if err == nil {
pr.Issue.PullRequest = pr
}
@@ -202,10 +205,11 @@ func (pr *PullRequest) loadIssue(e db.Engine) (err error) {
// LoadProtectedBranch loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranch() (err error) {
return pr.loadProtectedBranch(db.DefaultContext)
return pr.LoadProtectedBranchCtx(db.DefaultContext)
}
func (pr *PullRequest) loadProtectedBranch(ctx context.Context) (err error) {
// LoadProtectedBranchCtx loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
if pr.ProtectedBranch == nil {
if pr.BaseRepo == nil {
if pr.BaseRepoID == 0 {
@@ -392,7 +396,7 @@ func (pr *PullRequest) SetMerged() (bool, error) {
}
pr.Issue = nil
if err := pr.loadIssue(sess); err != nil {
if err := pr.LoadIssueCtx(ctx); err != nil {
return false, err
}
@@ -510,6 +514,11 @@ func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest,
// GetPullRequestByIndex returns a pull request by the given index
func GetPullRequestByIndex(repoID, index int64) (*PullRequest, error) {
return GetPullRequestByIndexCtx(db.DefaultContext, repoID, index)
}
// GetPullRequestByIndexCtx returns a pull request by the given index
func GetPullRequestByIndexCtx(ctx context.Context, repoID, index int64) (*PullRequest, error) {
if index < 1 {
return nil, ErrPullRequestNotExist{}
}
@@ -518,17 +527,17 @@ func GetPullRequestByIndex(repoID, index int64) (*PullRequest, error) {
Index: index,
}
has, err := db.GetEngine(db.DefaultContext).Get(pr)
has, err := db.GetEngine(ctx).Get(pr)
if err != nil {
return nil, err
} else if !has {
return nil, ErrPullRequestNotExist{0, 0, 0, repoID, "", ""}
}
if err = pr.LoadAttributes(); err != nil {
if err = pr.loadAttributes(db.GetEngine(ctx)); err != nil {
return nil, err
}
if err = pr.LoadIssue(); err != nil {
if err = pr.LoadIssueCtx(ctx); err != nil {
return nil, err
}
@@ -547,8 +556,8 @@ func getPullRequestByID(e db.Engine, id int64) (*PullRequest, error) {
}
// GetPullRequestByID returns a pull request by given ID.
func GetPullRequestByID(id int64) (*PullRequest, error) {
return getPullRequestByID(db.GetEngine(db.DefaultContext), id)
func GetPullRequestByID(ctx context.Context, id int64) (*PullRequest, error) {
return getPullRequestByID(db.GetEngine(ctx), id)
}
// GetPullRequestByIssueIDWithNoAttributes returns pull request with no attributes loaded by given issue ID.