mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-28 21:00:03 +02:00
DBContext is just a Context (#17100)
* DBContext is just a Context This PR removes some of the specialness from the DBContext and makes it context This allows us to simplify the GetEngine code to wrap around any context in future and means that we can change our loadRepo(e Engine) functions to simply take contexts. Signed-off-by: Andrew Thornton <art27@cantab.net> * fix unit tests Signed-off-by: Andrew Thornton <art27@cantab.net> * another place that needs to set the initial context Signed-off-by: Andrew Thornton <art27@cantab.net> * avoid race Signed-off-by: Andrew Thornton <art27@cantab.net> * change attachment error Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
parent
b22be7f594
commit
9302eba971
129 changed files with 1112 additions and 1022 deletions
|
@ -122,7 +122,7 @@ func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permi
|
|||
|
||||
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
|
||||
func (protectBranch *ProtectedBranch) IsUserOfficialReviewer(user *User) (bool, error) {
|
||||
return protectBranch.isUserOfficialReviewer(db.DefaultContext().Engine(), user)
|
||||
return protectBranch.isUserOfficialReviewer(db.GetEngine(db.DefaultContext), user)
|
||||
}
|
||||
|
||||
func (protectBranch *ProtectedBranch) isUserOfficialReviewer(e db.Engine, user *User) (bool, error) {
|
||||
|
@ -162,7 +162,7 @@ func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {
|
|||
|
||||
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
|
||||
func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
|
||||
sess := db.DefaultContext().Engine().Where("issue_id = ?", pr.IssueID).
|
||||
sess := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
|
||||
And("type = ?", ReviewTypeApprove).
|
||||
And("official = ?", true).
|
||||
And("dismissed = ?", false)
|
||||
|
@ -183,7 +183,7 @@ func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullReque
|
|||
if !protectBranch.BlockOnRejectedReviews {
|
||||
return false
|
||||
}
|
||||
rejectExist, err := db.DefaultContext().Engine().Where("issue_id = ?", pr.IssueID).
|
||||
rejectExist, err := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
|
||||
And("type = ?", ReviewTypeReject).
|
||||
And("official = ?", true).
|
||||
And("dismissed = ?", false).
|
||||
|
@ -202,7 +202,7 @@ func (protectBranch *ProtectedBranch) MergeBlockedByOfficialReviewRequests(pr *P
|
|||
if !protectBranch.BlockOnOfficialReviewRequests {
|
||||
return false
|
||||
}
|
||||
has, err := db.DefaultContext().Engine().Where("issue_id = ?", pr.IssueID).
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
|
||||
And("type = ?", ReviewTypeRequest).
|
||||
And("official = ?", true).
|
||||
Exist(new(Review))
|
||||
|
@ -300,7 +300,7 @@ func (protectBranch *ProtectedBranch) IsUnprotectedFile(patterns []glob.Glob, pa
|
|||
|
||||
// GetProtectedBranchBy getting protected branch by ID/Name
|
||||
func GetProtectedBranchBy(repoID int64, branchName string) (*ProtectedBranch, error) {
|
||||
return getProtectedBranchBy(db.DefaultContext().Engine(), repoID, branchName)
|
||||
return getProtectedBranchBy(db.GetEngine(db.DefaultContext), repoID, branchName)
|
||||
}
|
||||
|
||||
func getProtectedBranchBy(e db.Engine, repoID int64, branchName string) (*ProtectedBranch, error) {
|
||||
|
@ -375,13 +375,13 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts
|
|||
|
||||
// Make sure protectBranch.ID is not 0 for whitelists
|
||||
if protectBranch.ID == 0 {
|
||||
if _, err = db.DefaultContext().Engine().Insert(protectBranch); err != nil {
|
||||
if _, err = db.GetEngine(db.DefaultContext).Insert(protectBranch); err != nil {
|
||||
return fmt.Errorf("Insert: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err = db.DefaultContext().Engine().ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
|
||||
if _, err = db.GetEngine(db.DefaultContext).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
|
||||
return fmt.Errorf("Update: %v", err)
|
||||
}
|
||||
|
||||
|
@ -391,7 +391,7 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts
|
|||
// GetProtectedBranches get all protected branches
|
||||
func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
|
||||
protectedBranches := make([]*ProtectedBranch, 0)
|
||||
return protectedBranches, db.DefaultContext().Engine().Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
|
||||
return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
|
||||
}
|
||||
|
||||
// GetBranchProtection get the branch protection of a branch
|
||||
|
@ -406,7 +406,7 @@ func (repo *Repository) IsProtectedBranch(branchName string) (bool, error) {
|
|||
BranchName: branchName,
|
||||
}
|
||||
|
||||
has, err := db.DefaultContext().Engine().Exist(protectedBranch)
|
||||
has, err := db.GetEngine(db.DefaultContext).Exist(protectedBranch)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
@ -493,7 +493,7 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
|
|||
ID: id,
|
||||
}
|
||||
|
||||
if affected, err := db.DefaultContext().Engine().Delete(protectedBranch); err != nil {
|
||||
if affected, err := db.GetEngine(db.DefaultContext).Delete(protectedBranch); err != nil {
|
||||
return err
|
||||
} else if affected != 1 {
|
||||
return fmt.Errorf("delete protected branch ID(%v) failed", id)
|
||||
|
@ -522,20 +522,20 @@ func (repo *Repository) AddDeletedBranch(branchName, commit string, deletedByID
|
|||
DeletedByID: deletedByID,
|
||||
}
|
||||
|
||||
_, err := db.DefaultContext().Engine().InsertOne(deletedBranch)
|
||||
_, err := db.GetEngine(db.DefaultContext).InsertOne(deletedBranch)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetDeletedBranches returns all the deleted branches
|
||||
func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
|
||||
deletedBranches := make([]*DeletedBranch, 0)
|
||||
return deletedBranches, db.DefaultContext().Engine().Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches)
|
||||
return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches)
|
||||
}
|
||||
|
||||
// GetDeletedBranchByID get a deleted branch by its ID
|
||||
func (repo *Repository) GetDeletedBranchByID(id int64) (*DeletedBranch, error) {
|
||||
deletedBranch := &DeletedBranch{}
|
||||
has, err := db.DefaultContext().Engine().ID(id).Get(deletedBranch)
|
||||
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(deletedBranch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -552,7 +552,7 @@ func (repo *Repository) RemoveDeletedBranch(id int64) (err error) {
|
|||
ID: id,
|
||||
}
|
||||
|
||||
if affected, err := db.DefaultContext().Engine().Delete(deletedBranch); err != nil {
|
||||
if affected, err := db.GetEngine(db.DefaultContext).Delete(deletedBranch); err != nil {
|
||||
return err
|
||||
} else if affected != 1 {
|
||||
return fmt.Errorf("remove deleted branch ID(%v) failed", id)
|
||||
|
@ -573,7 +573,7 @@ func (deletedBranch *DeletedBranch) LoadUser() {
|
|||
|
||||
// RemoveDeletedBranch removes all deleted branches
|
||||
func RemoveDeletedBranch(repoID int64, branch string) error {
|
||||
_, err := db.DefaultContext().Engine().Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
|
||||
_, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -583,7 +583,7 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
|
|||
log.Trace("Doing: DeletedBranchesCleanup")
|
||||
|
||||
deleteBefore := time.Now().Add(-olderThan)
|
||||
_, err := db.DefaultContext().Engine().Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
|
||||
_, err := db.GetEngine(db.DefaultContext).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
|
||||
if err != nil {
|
||||
log.Error("DeletedBranchesCleanup: %v", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue