* Migrating Emoji store to use sqlx https://community-daily.mattermost.com/boards/workspace/zyoahc9uapdn3xdptac6jb69ic/285b80a3-257d-41f6-8cf4-ed80ca9d92e5/495cdb4d-c13a-4992-8eb9-80cfee2819a4?c=646cee9a-219c-49f3-8e0b-6ec2cff4deba ```release-note NONE ``` * Incorporate review suggestions ```release-note NONE ``` Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
53 строки
1.4 KiB
Go
53 строки
1.4 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mattermost/gorp"
|
|
)
|
|
|
|
// storeContextKey is the base type for all context keys for the store.
|
|
type storeContextKey string
|
|
|
|
// contextValue is a type to hold some pre-determined context values.
|
|
type contextValue string
|
|
|
|
// Different possible values of contextValue.
|
|
const (
|
|
useMaster contextValue = "useMaster"
|
|
)
|
|
|
|
// WithMaster adds the context value that master DB should be selected for this request.
|
|
func WithMaster(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, storeContextKey(useMaster), true)
|
|
}
|
|
|
|
// 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 {
|
|
if res, ok := v.(bool); ok && res {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// DBFromContext is a helper utility that returns the DB handle from a given context.
|
|
func (ss *SqlStore) DBFromContext(ctx context.Context) *gorp.DbMap {
|
|
if hasMaster(ctx) {
|
|
return ss.GetMaster()
|
|
}
|
|
return ss.GetReplica()
|
|
}
|
|
|
|
// DBXFromContext is a helper utility that returns the sqlx DB handle from a given context.
|
|
func (ss *SqlStore) DBXFromContext(ctx context.Context) *sqlxDBWrapper {
|
|
if hasMaster(ctx) {
|
|
return ss.GetMasterX()
|
|
}
|
|
return ss.GetReplicaX()
|
|
}
|