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:
zeripath 2021-09-23 16:45:36 +01:00 committed by GitHub
parent b22be7f594
commit 9302eba971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
129 changed files with 1112 additions and 1022 deletions

View file

@ -90,7 +90,7 @@ func (repos RepositoryList) loadAttributes(e db.Engine) error {
// LoadAttributes loads the attributes for the given RepositoryList
func (repos RepositoryList) LoadAttributes() error {
return repos.loadAttributes(db.DefaultContext().Engine())
return repos.loadAttributes(db.GetEngine(db.DefaultContext))
}
// MirrorRepositoryList contains the mirror repositories
@ -130,7 +130,7 @@ func (repos MirrorRepositoryList) loadAttributes(e db.Engine) error {
// LoadAttributes loads the attributes for the given MirrorRepositoryList
func (repos MirrorRepositoryList) LoadAttributes() error {
return repos.loadAttributes(db.DefaultContext().Engine())
return repos.loadAttributes(db.GetEngine(db.DefaultContext))
}
// SearchRepoOptions holds the search options
@ -410,7 +410,7 @@ func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (*x
opts.OrderBy = SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy))
}
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
var count int64
if opts.PageSize > 0 {
@ -520,7 +520,7 @@ func AccessibleRepoIDsQuery(user *User) *builder.Builder {
// FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
func FindUserAccessibleRepoIDs(user *User) ([]int64, error) {
repoIDs := make([]int64, 0, 10)
if err := db.DefaultContext().Engine().
if err := db.GetEngine(db.DefaultContext).
Table("repository").
Cols("id").
Where(accessibleRepositoryCondition(user)).