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

@ -60,7 +60,7 @@ func isWatchMode(mode RepoWatchMode) bool {
// IsWatching checks if user has watched given repository.
func IsWatching(userID, repoID int64) bool {
watch, err := getWatch(db.DefaultContext().Engine(), userID, repoID)
watch, err := getWatch(db.GetEngine(db.DefaultContext), userID, repoID)
return err == nil && isWatchMode(watch.Mode)
}
@ -107,10 +107,10 @@ func watchRepoMode(e db.Engine, watch Watch, mode RepoWatchMode) (err error) {
// WatchRepoMode watch repository in specific mode.
func WatchRepoMode(userID, repoID int64, mode RepoWatchMode) (err error) {
var watch Watch
if watch, err = getWatch(db.DefaultContext().Engine(), userID, repoID); err != nil {
if watch, err = getWatch(db.GetEngine(db.DefaultContext), userID, repoID); err != nil {
return err
}
return watchRepoMode(db.DefaultContext().Engine(), watch, mode)
return watchRepoMode(db.GetEngine(db.DefaultContext), watch, mode)
}
func watchRepo(e db.Engine, userID, repoID int64, doWatch bool) (err error) {
@ -130,7 +130,7 @@ func watchRepo(e db.Engine, userID, repoID int64, doWatch bool) (err error) {
// WatchRepo watch or unwatch repository.
func WatchRepo(userID, repoID int64, watch bool) (err error) {
return watchRepo(db.DefaultContext().Engine(), userID, repoID, watch)
return watchRepo(db.GetEngine(db.DefaultContext), userID, repoID, watch)
}
func getWatchers(e db.Engine, repoID int64) ([]*Watch, error) {
@ -145,14 +145,14 @@ func getWatchers(e db.Engine, repoID int64) ([]*Watch, error) {
// GetWatchers returns all watchers of given repository.
func GetWatchers(repoID int64) ([]*Watch, error) {
return getWatchers(db.DefaultContext().Engine(), repoID)
return getWatchers(db.GetEngine(db.DefaultContext), repoID)
}
// GetRepoWatchersIDs returns IDs of watchers for a given repo ID
// but avoids joining with `user` for performance reasons
// User permissions must be verified elsewhere if required
func GetRepoWatchersIDs(repoID int64) ([]int64, error) {
return getRepoWatchersIDs(db.DefaultContext().Engine(), repoID)
return getRepoWatchersIDs(db.GetEngine(db.DefaultContext), repoID)
}
func getRepoWatchersIDs(e db.Engine, repoID int64) ([]int64, error) {
@ -166,7 +166,7 @@ func getRepoWatchersIDs(e db.Engine, repoID int64) ([]int64, error) {
// GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(opts ListOptions) ([]*User, error) {
sess := db.DefaultContext().Engine().Where("watch.repo_id=?", repo.ID).
sess := db.GetEngine(db.DefaultContext).Where("watch.repo_id=?", repo.ID).
Join("LEFT", "watch", "`user`.id=`watch`.user_id").
And("`watch`.mode<>?", RepoWatchModeDont)
if opts.Page > 0 {
@ -284,12 +284,12 @@ func notifyWatchers(e db.Engine, actions ...*Action) error {
// NotifyWatchers creates batch of actions for every watcher.
func NotifyWatchers(actions ...*Action) error {
return notifyWatchers(db.DefaultContext().Engine(), actions...)
return notifyWatchers(db.GetEngine(db.DefaultContext), actions...)
}
// NotifyWatchersActions creates batch of actions for every watcher.
func NotifyWatchersActions(acts []*Action) error {
sess := db.DefaultContext().NewSession()
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
@ -318,5 +318,5 @@ func watchIfAuto(e db.Engine, userID, repoID int64, isWrite bool) error {
// WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
func WatchIfAuto(userID, repoID int64, isWrite bool) error {
return watchIfAuto(db.DefaultContext().Engine(), userID, repoID, isWrite)
return watchIfAuto(db.GetEngine(db.DefaultContext), userID, repoID, isWrite)
}