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

@ -30,7 +30,7 @@ func init() {
// GetUserOpenIDs returns all openid addresses that belongs to given user.
func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) {
openids := make([]*UserOpenID, 0, 5)
if err := db.DefaultContext().Engine().
if err := db.GetEngine(db.DefaultContext).
Where("uid=?", uid).
Asc("id").
Find(&openids); err != nil {
@ -64,7 +64,7 @@ func addUserOpenID(e db.Engine, openid *UserOpenID) error {
// AddUserOpenID adds an pre-verified/normalized OpenID URI to given user.
func AddUserOpenID(openid *UserOpenID) error {
return addUserOpenID(db.DefaultContext().Engine(), openid)
return addUserOpenID(db.GetEngine(db.DefaultContext), openid)
}
// DeleteUserOpenID deletes an openid address of given user.
@ -75,9 +75,9 @@ func DeleteUserOpenID(openid *UserOpenID) (err error) {
UID: openid.UID,
}
if openid.ID > 0 {
deleted, err = db.DefaultContext().Engine().ID(openid.ID).Delete(&address)
deleted, err = db.GetEngine(db.DefaultContext).ID(openid.ID).Delete(&address)
} else {
deleted, err = db.DefaultContext().Engine().
deleted, err = db.GetEngine(db.DefaultContext).
Where("openid=?", openid.URI).
Delete(&address)
}
@ -92,7 +92,7 @@ func DeleteUserOpenID(openid *UserOpenID) (err error) {
// ToggleUserOpenIDVisibility toggles visibility of an openid address of given user.
func ToggleUserOpenIDVisibility(id int64) (err error) {
_, err = db.DefaultContext().Engine().Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
_, err = db.GetEngine(db.DefaultContext).Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
return err
}
@ -111,7 +111,7 @@ func GetUserByOpenID(uri string) (*User, error) {
// Otherwise, check in openid table
oid := &UserOpenID{}
has, err := db.DefaultContext().Engine().Where("uri=?", uri).Get(oid)
has, err := db.GetEngine(db.DefaultContext).Where("uri=?", uri).Get(oid)
if err != nil {
return nil, err
}