Migrate emojiStore to use request.CTX instead of context.Context (#24514)

* migrate emojistore to request.ctx

* use mlog.CreateConsoleTestLogger

* Add comment to WithMaster and RequestContextWithMaster
Этот коммит содержится в:
Felipe Martin
2023-09-11 17:07:29 +02:00
коммит произвёл GitHub
родитель e9cc03c1c8
Коммит f65dad83bb
25 изменённых файлов: 233 добавлений и 194 удалений

Просмотреть файл

@@ -5,6 +5,8 @@ package sqlstore
import (
"context"
"github.com/mattermost/mattermost/server/public/shared/request"
)
// storeContextKey is the base type for all context keys for the store.
@@ -19,10 +21,20 @@ const (
)
// WithMaster adds the context value that master DB should be selected for this request.
//
// Deprecated: This method is deprecated and there's ongoing change to use `request.CTX` across
// instead of `context.Context`. Please use `RequestContextWithMaster` instead.
func WithMaster(ctx context.Context) context.Context {
return context.WithValue(ctx, storeContextKey(useMaster), true)
}
// RequestContextWithMaster adds the context value that master DB should be selected for this request.
func RequestContextWithMaster(c request.CTX) request.CTX {
ctx := WithMaster(c.Context())
c.SetContext(ctx)
return c
}
// hasMaster is a helper function to check whether master DB should be selected or not.
func hasMaster(ctx context.Context) bool {
if v := ctx.Value(storeContextKey(useMaster)); v != nil {

Просмотреть файл

@@ -4,7 +4,6 @@
package sqlstore
import (
"context"
"database/sql"
"fmt"
"strings"
@@ -12,6 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/einterfaces"
)
@@ -44,21 +44,21 @@ func (es SqlEmojiStore) Save(emoji *model.Emoji) (*model.Emoji, error) {
return emoji, nil
}
func (es SqlEmojiStore) Get(ctx context.Context, id string, allowFromCache bool) (*model.Emoji, error) {
return es.getBy(ctx, "Id", id)
func (es SqlEmojiStore) Get(c request.CTX, id string, allowFromCache bool) (*model.Emoji, error) {
return es.getBy(c, "Id", id)
}
func (es SqlEmojiStore) GetByName(ctx context.Context, name string, allowFromCache bool) (*model.Emoji, error) {
return es.getBy(ctx, "Name", name)
func (es SqlEmojiStore) GetByName(c request.CTX, name string, allowFromCache bool) (*model.Emoji, error) {
return es.getBy(c, "Name", name)
}
func (es SqlEmojiStore) GetMultipleByName(ctx context.Context, names []string) ([]*model.Emoji, error) {
func (es SqlEmojiStore) GetMultipleByName(c request.CTX, names []string) ([]*model.Emoji, error) {
// Creating (?, ?, ?) len(names) number of times.
keys := strings.Join(strings.Fields(strings.Repeat("? ", len(names))), ",")
args := makeStringArgs(names)
emojis := []*model.Emoji{}
if err := es.DBXFromContext(ctx).Select(&emojis,
if err := es.DBXFromContext(c.Context()).Select(&emojis,
`SELECT
*
FROM
@@ -134,10 +134,10 @@ func (es SqlEmojiStore) Search(name string, prefixOnly bool, limit int) ([]*mode
}
// getBy returns one active (not deleted) emoji, found by any one column (what/key).
func (es SqlEmojiStore) getBy(ctx context.Context, what, key string) (*model.Emoji, error) {
func (es SqlEmojiStore) getBy(c request.CTX, what, key string) (*model.Emoji, error) {
var emoji model.Emoji
err := es.DBXFromContext(ctx).Get(&emoji,
err := es.DBXFromContext(c.Context()).Get(&emoji,
`SELECT
*
FROM