MM-32893: Refactor the getting of DB handle (#16943)

* MM-32893: Refactor the getting of DB handle

We add a method in *SqlStore that returns the correct DB handle
depending on the context.

https://mattermost.atlassian.net/browse/MM-32893

```release-note
NONE
```

* fix receiver name
Этот коммит содержится в:
Agniva De Sarker
2021-02-16 14:00:03 +05:30
коммит произвёл GitHub
родитель ee5668b834
Коммит 2596a9e630
3 изменённых файлов: 15 добавлений и 39 удалений

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

@@ -5,6 +5,8 @@ package sqlstore
import (
"context"
"github.com/mattermost/gorp"
)
// storeContextKey is the base type for all context keys for the store.
@@ -32,3 +34,11 @@ func hasMaster(ctx context.Context) bool {
}
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()
}