Move middleware context handler logic to service (#29605)
* middleware: Move context handler to own service Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Emil Tullsted <sakjur@users.noreply.github.com> Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
co-authored by
Emil Tullsted
Will Browne
parent
d0f52d5334
commit
12661e8a9d
@@ -6,8 +6,6 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func (ss *SQLStore) addPreferencesQueryAndCommandHandlers() {
|
||||
@@ -42,7 +40,7 @@ func (ss *SQLStore) GetPreferencesWithDefaults(query *models.GetPreferencesWithD
|
||||
}
|
||||
|
||||
res := &models.Preferences{
|
||||
Theme: setting.DefaultTheme,
|
||||
Theme: ss.Cfg.DefaultTheme,
|
||||
Timezone: ss.Cfg.DateFormats.DefaultTimezone,
|
||||
HomeDashboardId: 0,
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -14,7 +13,7 @@ func TestPreferencesDataAccess(t *testing.T) {
|
||||
ss := InitTestDB(t)
|
||||
|
||||
t.Run("GetPreferencesWithDefaults with no saved preferences should return defaults", func(t *testing.T) {
|
||||
setting.DefaultTheme = "light"
|
||||
ss.Cfg.DefaultTheme = "light"
|
||||
ss.Cfg.DateFormats.DefaultTimezone = "UTC"
|
||||
|
||||
query := &models.GetPreferencesWithDefaultsQuery{User: &models.SignedInUser{}}
|
||||
|
||||
@@ -39,16 +39,21 @@ var (
|
||||
// ContextSessionKey is used as key to save values in `context.Context`
|
||||
type ContextSessionKey struct{}
|
||||
|
||||
const ServiceName = "SqlStore"
|
||||
const InitPriority = registry.High
|
||||
|
||||
func init() {
|
||||
ss := &SQLStore{}
|
||||
|
||||
// This change will make xorm use an empty default schema for postgres and
|
||||
// by that mimic the functionality of how it was functioning before
|
||||
// xorm's changes above.
|
||||
xorm.DefaultPostgresSchema = ""
|
||||
|
||||
registry.Register(®istry.Descriptor{
|
||||
Name: "SQLStore",
|
||||
Instance: &SQLStore{},
|
||||
InitPriority: registry.High,
|
||||
Name: ServiceName,
|
||||
Instance: ss,
|
||||
InitPriority: InitPriority,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -113,13 +118,20 @@ func (ss *SQLStore) Init() error {
|
||||
|
||||
func (ss *SQLStore) ensureMainOrgAndAdminUser() error {
|
||||
err := ss.InTransaction(context.Background(), func(ctx context.Context) error {
|
||||
systemUserCountQuery := models.GetSystemUserCountStatsQuery{}
|
||||
err := bus.DispatchCtx(ctx, &systemUserCountQuery)
|
||||
var stats models.SystemUserCountStats
|
||||
err := ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
|
||||
if _, err := sess.SQL(rawSql).Get(&stats); err != nil {
|
||||
return fmt.Errorf("could not determine if admin user exists: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not determine if admin user exists: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if systemUserCountQuery.Result.Count > 0 {
|
||||
if stats.Count > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -351,7 +363,7 @@ func InitTestDB(t ITestDB) *SQLStore {
|
||||
testSQLStore = &SQLStore{}
|
||||
testSQLStore.Bus = bus.New()
|
||||
testSQLStore.CacheService = localcache.New(5*time.Minute, 10*time.Minute)
|
||||
testSQLStore.skipEnsureDefaultOrgAndUser = true
|
||||
testSQLStore.skipEnsureDefaultOrgAndUser = false
|
||||
|
||||
dbType := migrator.SQLite
|
||||
|
||||
|
||||
Reference in New Issue
Block a user