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
129 changed files with 1112 additions and 1022 deletions

View File

@ -69,7 +69,7 @@ func NewAccessToken(t *AccessToken) error {
t.Token = base.EncodeSha1(gouuid.New().String())
t.TokenHash = hashToken(t.Token, t.TokenSalt)
t.TokenLastEight = t.Token[len(t.Token)-8:]
_, err = db.DefaultContext().Engine().Insert(t)
_, err = db.GetEngine(db.DefaultContext).Insert(t)
return err
}
@ -110,7 +110,7 @@ func GetAccessTokenBySHA(token string) (*AccessToken, error) {
TokenLastEight: lastEight,
}
// Re-get the token from the db in case it has been deleted in the intervening period
has, err := db.DefaultContext().Engine().ID(id).Get(token)
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(token)
if err != nil {
return nil, err
}
@ -121,7 +121,7 @@ func GetAccessTokenBySHA(token string) (*AccessToken, error) {
}
var tokens []AccessToken
err := db.DefaultContext().Engine().Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)
err := db.GetEngine(db.DefaultContext).Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)
if err != nil {
return nil, err
} else if len(tokens) == 0 {
@ -142,7 +142,7 @@ func GetAccessTokenBySHA(token string) (*AccessToken, error) {
// AccessTokenByNameExists checks if a token name has been used already by a user.
func AccessTokenByNameExists(token *AccessToken) (bool, error) {
return db.DefaultContext().Engine().Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
return db.GetEngine(db.DefaultContext).Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
}
// ListAccessTokensOptions contain filter options
@ -154,7 +154,7 @@ type ListAccessTokensOptions struct {
// ListAccessTokens returns a list of access tokens belongs to given user.
func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
sess := db.DefaultContext().Engine().Where("uid=?", opts.UserID)
sess := db.GetEngine(db.DefaultContext).Where("uid=?", opts.UserID)
if len(opts.Name) != 0 {
sess = sess.Where("name=?", opts.Name)
@ -175,13 +175,13 @@ func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
// UpdateAccessToken updates information of access token.
func UpdateAccessToken(t *AccessToken) error {
_, err := db.DefaultContext().Engine().ID(t.ID).AllCols().Update(t)
_, err := db.GetEngine(db.DefaultContext).ID(t.ID).AllCols().Update(t)
return err
}
// CountAccessTokens count access tokens belongs to given user by options
func CountAccessTokens(opts ListAccessTokensOptions) (int64, error) {
sess := db.DefaultContext().Engine().Where("uid=?", opts.UserID)
sess := db.GetEngine(db.DefaultContext).Where("uid=?", opts.UserID)
if len(opts.Name) != 0 {
sess = sess.Where("name=?", opts.Name)
}
@ -190,7 +190,7 @@ func CountAccessTokens(opts ListAccessTokensOptions) (int64, error) {
// DeleteAccessTokenByID deletes access token by given ID.
func DeleteAccessTokenByID(id, userID int64) error {
cnt, err := db.DefaultContext().Engine().ID(id).Delete(&AccessToken{
cnt, err := db.GetEngine(db.DefaultContext).ID(id).Delete(&AccessToken{
UID: userID,
})
if err != nil {