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

@ -72,7 +72,7 @@ func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
return nil, err
}
_, err = db.DefaultContext().Engine().InsertOne(lock)
_, err = db.GetEngine(db.DefaultContext).InsertOne(lock)
return lock, err
}
@ -80,7 +80,7 @@ func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
path = cleanPath(path)
rel := &LFSLock{RepoID: repo.ID}
has, err := db.DefaultContext().Engine().Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
has, err := db.GetEngine(db.DefaultContext).Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
if err != nil {
return nil, err
}
@ -93,7 +93,7 @@ func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
// GetLFSLockByID returns release by given id.
func GetLFSLockByID(id int64) (*LFSLock, error) {
lock := new(LFSLock)
has, err := db.DefaultContext().Engine().ID(id).Get(lock)
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(lock)
if err != nil {
return nil, err
} else if !has {
@ -104,7 +104,7 @@ func GetLFSLockByID(id int64) (*LFSLock, error) {
// GetLFSLockByRepoID returns a list of locks of repository.
func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if page >= 0 && pageSize > 0 {
@ -120,7 +120,7 @@ func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
// CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.
func CountLFSLockByRepoID(repoID int64) (int64, error) {
return db.DefaultContext().Engine().Count(&LFSLock{RepoID: repoID})
return db.GetEngine(db.DefaultContext).Count(&LFSLock{RepoID: repoID})
}
// DeleteLFSLockByID deletes a lock by given ID.
@ -139,7 +139,7 @@ func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
}
_, err = db.DefaultContext().Engine().ID(id).Delete(new(LFSLock))
_, err = db.GetEngine(db.DefaultContext).ID(id).Delete(new(LFSLock))
return lock, err
}