mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 05:31:49 +08:00
feat(config): add config ensure_default_org_and_user
(#107619)
This commit is contained in:

committed by
GitHub

parent
cc2eac7f84
commit
3abe84121f
@ -39,6 +39,7 @@ type DatabaseConfig struct {
|
|||||||
WALEnabled bool
|
WALEnabled bool
|
||||||
UrlQueryParams map[string][]string
|
UrlQueryParams map[string][]string
|
||||||
SkipMigrations bool
|
SkipMigrations bool
|
||||||
|
EnsureDefaultOrgAndUser bool
|
||||||
MigrationLock bool
|
MigrationLock bool
|
||||||
MigrationLockAttemptTimeout int
|
MigrationLockAttemptTimeout int
|
||||||
LogQueries bool
|
LogQueries bool
|
||||||
@ -114,6 +115,7 @@ func (dbCfg *DatabaseConfig) readConfig(cfg *setting.Cfg) error {
|
|||||||
dbCfg.CacheMode = sec.Key("cache_mode").MustString("private")
|
dbCfg.CacheMode = sec.Key("cache_mode").MustString("private")
|
||||||
dbCfg.WALEnabled = sec.Key("wal").MustBool(false)
|
dbCfg.WALEnabled = sec.Key("wal").MustBool(false)
|
||||||
dbCfg.SkipMigrations = sec.Key("skip_migrations").MustBool()
|
dbCfg.SkipMigrations = sec.Key("skip_migrations").MustBool()
|
||||||
|
dbCfg.EnsureDefaultOrgAndUser = sec.Key("ensure_default_org_and_user").MustBool(true)
|
||||||
dbCfg.MigrationLock = sec.Key("migration_locking").MustBool(true)
|
dbCfg.MigrationLock = sec.Key("migration_locking").MustBool(true)
|
||||||
dbCfg.MigrationLockAttemptTimeout = sec.Key("locking_attempt_timeout_sec").MustInt()
|
dbCfg.MigrationLockAttemptTimeout = sec.Key("locking_attempt_timeout_sec").MustInt()
|
||||||
|
|
||||||
|
@ -47,7 +47,6 @@ type SQLStore struct {
|
|||||||
engine *xorm.Engine
|
engine *xorm.Engine
|
||||||
log log.Logger
|
log log.Logger
|
||||||
dialect migrator.Dialect
|
dialect migrator.Dialect
|
||||||
skipEnsureDefaultOrgAndUser bool
|
|
||||||
migrations registry.DatabaseMigrator
|
migrations registry.DatabaseMigrator
|
||||||
tracer tracing.Tracer
|
tracer tracing.Tracer
|
||||||
recursiveQueriesAreSupported *bool
|
recursiveQueriesAreSupported *bool
|
||||||
@ -62,7 +61,7 @@ func ProvideService(cfg *setting.Cfg,
|
|||||||
// by that mimic the functionality of how it was functioning before
|
// by that mimic the functionality of how it was functioning before
|
||||||
// xorm's changes above.
|
// xorm's changes above.
|
||||||
xorm.DefaultPostgresSchema = ""
|
xorm.DefaultPostgresSchema = ""
|
||||||
s, err := newStore(cfg, nil, features, migrations, bus, tracer, false)
|
s, err := newStore(cfg, nil, features, migrations, bus, tracer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -80,7 +79,7 @@ func ProvideService(cfg *setting.Cfg,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ProvideServiceForTests(t sqlutil.ITestDB, cfg *setting.Cfg, features featuremgmt.FeatureToggles, bus bus.Bus, migrations registry.DatabaseMigrator) (*SQLStore, error) {
|
func ProvideServiceForTests(t sqlutil.ITestDB, cfg *setting.Cfg, features featuremgmt.FeatureToggles, bus bus.Bus, migrations registry.DatabaseMigrator) (*SQLStore, error) {
|
||||||
return initTestDB(t, cfg, features, migrations, bus, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
|
return initTestDB(t, cfg, features, migrations, bus, InitTestDBOpt{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSQLStoreWithoutSideEffects creates a new *SQLStore without side-effects such as
|
// NewSQLStoreWithoutSideEffects creates a new *SQLStore without side-effects such as
|
||||||
@ -88,20 +87,20 @@ func ProvideServiceForTests(t sqlutil.ITestDB, cfg *setting.Cfg, features featur
|
|||||||
func NewSQLStoreWithoutSideEffects(cfg *setting.Cfg,
|
func NewSQLStoreWithoutSideEffects(cfg *setting.Cfg,
|
||||||
features featuremgmt.FeatureToggles,
|
features featuremgmt.FeatureToggles,
|
||||||
bus bus.Bus, tracer tracing.Tracer) (*SQLStore, error) {
|
bus bus.Bus, tracer tracing.Tracer) (*SQLStore, error) {
|
||||||
return newStore(cfg, nil, features, nil, bus, tracer, true)
|
cfgDBSection := cfg.Raw.Section("database")
|
||||||
|
cfgDBSection.Key("ensure_default_org_and_user").SetValue("false")
|
||||||
|
return newStore(cfg, nil, features, nil, bus, tracer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newStore(cfg *setting.Cfg, engine *xorm.Engine, features featuremgmt.FeatureToggles,
|
func newStore(cfg *setting.Cfg, engine *xorm.Engine, features featuremgmt.FeatureToggles,
|
||||||
migrations registry.DatabaseMigrator, bus bus.Bus, tracer tracing.Tracer,
|
migrations registry.DatabaseMigrator, bus bus.Bus, tracer tracing.Tracer) (*SQLStore, error) {
|
||||||
skipEnsureDefaultOrgAndUser bool) (*SQLStore, error) {
|
|
||||||
ss := &SQLStore{
|
ss := &SQLStore{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
log: log.New("sqlstore"),
|
log: log.New("sqlstore"),
|
||||||
skipEnsureDefaultOrgAndUser: skipEnsureDefaultOrgAndUser,
|
migrations: migrations,
|
||||||
migrations: migrations,
|
bus: bus,
|
||||||
bus: bus,
|
tracer: tracer,
|
||||||
tracer: tracer,
|
features: features,
|
||||||
features: features,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ss.initEngine(engine); err != nil {
|
if err := ss.initEngine(engine); err != nil {
|
||||||
@ -148,11 +147,10 @@ func (ss *SQLStore) Migrate(isDatabaseLockingEnabled bool) error {
|
|||||||
// Reset resets database state.
|
// Reset resets database state.
|
||||||
// If default org and user creation is enabled, it will be ensured they exist in the database.
|
// If default org and user creation is enabled, it will be ensured they exist in the database.
|
||||||
func (ss *SQLStore) Reset() error {
|
func (ss *SQLStore) Reset() error {
|
||||||
if ss.skipEnsureDefaultOrgAndUser {
|
if ss.dbCfg.EnsureDefaultOrgAndUser {
|
||||||
return nil
|
return ss.ensureMainOrgAndAdminUser(false)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return ss.ensureMainOrgAndAdminUser(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quote quotes the value in the used SQL dialect
|
// Quote quotes the value in the used SQL dialect
|
||||||
@ -412,10 +410,8 @@ var testSQLStoreCleanup []func()
|
|||||||
|
|
||||||
// InitTestDBOpt contains options for InitTestDB.
|
// InitTestDBOpt contains options for InitTestDB.
|
||||||
type InitTestDBOpt struct {
|
type InitTestDBOpt struct {
|
||||||
// EnsureDefaultOrgAndUser flags whether to ensure that default org and user exist.
|
FeatureFlags []string
|
||||||
EnsureDefaultOrgAndUser bool
|
Cfg *setting.Cfg
|
||||||
FeatureFlags []string
|
|
||||||
Cfg *setting.Cfg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitTestDBWithMigration initializes the test DB given custom migrations.
|
// InitTestDBWithMigration initializes the test DB given custom migrations.
|
||||||
@ -533,7 +529,8 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(opts) == 0 {
|
if len(opts) == 0 {
|
||||||
opts = []InitTestDBOpt{{EnsureDefaultOrgAndUser: false, FeatureFlags: []string{}}}
|
cfgDBSec := testCfg.Raw.Section("database")
|
||||||
|
cfgDBSec.Key("ensure_default_org_and_user").SetValue("false")
|
||||||
}
|
}
|
||||||
|
|
||||||
if testSQLStore == nil {
|
if testSQLStore == nil {
|
||||||
@ -566,16 +563,8 @@ func TestMain(m *testing.M) {
|
|||||||
engine.DatabaseTZ = time.UTC
|
engine.DatabaseTZ = time.UTC
|
||||||
engine.TZLocation = time.UTC
|
engine.TZLocation = time.UTC
|
||||||
|
|
||||||
skipEnsureDefaultOrgAndUser := false
|
|
||||||
for _, opt := range opts {
|
|
||||||
if !opt.EnsureDefaultOrgAndUser {
|
|
||||||
skipEnsureDefaultOrgAndUser = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tracer := tracing.InitializeTracerForTest()
|
tracer := tracing.InitializeTracerForTest()
|
||||||
testSQLStore, err = newStore(testCfg, engine, features, migration, bus, tracer, skipEnsureDefaultOrgAndUser)
|
testSQLStore, err = newStore(testCfg, engine, features, migration, bus, tracer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -174,8 +174,12 @@ func NewTestStore(tb TestingTB, opts ...TestOption) *SQLStore {
|
|||||||
engine.DatabaseTZ = time.UTC
|
engine.DatabaseTZ = time.UTC
|
||||||
engine.TZLocation = time.UTC
|
engine.TZLocation = time.UTC
|
||||||
|
|
||||||
|
cfgDBSec := cfg.Raw.Section("database")
|
||||||
|
shouldEnsure := fmt.Sprintf("%t", !options.NoDefaultUserOrg && !options.Truncate)
|
||||||
|
cfgDBSec.Key("ensure_default_org_and_user").SetValue(shouldEnsure)
|
||||||
|
|
||||||
store, err := newStore(cfg, engine, features, options.MigratorFactory(features),
|
store, err := newStore(cfg, engine, features, options.MigratorFactory(features),
|
||||||
options.Bus, options.Tracer, options.NoDefaultUserOrg || options.Truncate)
|
options.Bus, options.Tracer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tb.Fatalf("failed to create a new SQLStore: %v", err)
|
tb.Fatalf("failed to create a new SQLStore: %v", err)
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
|
Reference in New Issue
Block a user