diff --git a/pkg/storage/unified/sql/server.go b/pkg/storage/unified/sql/server.go index a9778d0c42d..0bd5e6976a6 100644 --- a/pkg/storage/unified/sql/server.go +++ b/pkg/storage/unified/sql/server.go @@ -45,14 +45,7 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg, return nil, err } - dbCfg := cfg.SectionWithEnvOverrides("database") - // Check in the config if HA is enabled by default we always assume a HA setup. - isHA := dbCfg.Key("high_availability").MustBool(true) - // SQLite is not possible to run in HA, so we set it to false. - databaseType := dbCfg.Key("type").MustString(migrator.SQLite) - if databaseType == migrator.SQLite { - isHA = false - } + isHA := isHighAvailabilityEnabled(cfg.SectionWithEnvOverrides("database")) store, err := NewBackend(BackendOptions{DBProvider: eDB, Tracer: tracer, IsHA: isHA}) if err != nil { @@ -70,3 +63,19 @@ func NewResourceServer(db infraDB.DB, cfg *setting.Cfg, return rs, nil } + +// isHighAvailabilityEnabled determines if high availability mode should +// be enabled based on database configuration. High availability is enabled +// by default except for SQLite databases. +func isHighAvailabilityEnabled(dbCfg *setting.DynamicSection) bool { + // Check in the config if HA is enabled - by default we always assume a HA setup. + isHA := dbCfg.Key("high_availability").MustBool(true) + + // SQLite is not possible to run in HA, so we force it to false. + databaseType := dbCfg.Key("type").String() + if databaseType == migrator.SQLite { + isHA = false + } + + return isHA +} diff --git a/pkg/storage/unified/sql/server_test.go b/pkg/storage/unified/sql/server_test.go new file mode 100644 index 00000000000..992319c12b2 --- /dev/null +++ b/pkg/storage/unified/sql/server_test.go @@ -0,0 +1,100 @@ +package sql + +import ( + "strconv" + "testing" + + "github.com/grafana/grafana/pkg/services/sqlstore/migrator" + "github.com/grafana/grafana/pkg/setting" + "github.com/stretchr/testify/require" +) + +func TestIsHighAvailabilityEnabled(t *testing.T) { + tests := []struct { + name string + dbType string + haConfigValue *bool + isHA bool + }{ + { + name: "SQLite should never have HA enabled", + dbType: migrator.SQLite, + haConfigValue: boolPtr(true), + isHA: false, + }, + { + name: "MySQL with HA enabled in config should default to true", + dbType: migrator.MySQL, + haConfigValue: boolPtr(true), + isHA: true, + }, + { + name: "MySQL with HA disabled in config should default to false", + dbType: migrator.MySQL, + haConfigValue: boolPtr(false), + isHA: false, + }, + { + name: "MySQL with no HA config should default to true", + dbType: migrator.MySQL, + haConfigValue: nil, + isHA: true, + }, + { + name: "Postgres with HA enabled in config should default to true", + dbType: migrator.Postgres, + haConfigValue: boolPtr(true), + isHA: true, + }, + { + name: "Postgres with HA disabled in config should default to false", + dbType: migrator.Postgres, + haConfigValue: boolPtr(false), + isHA: false, + }, + { + name: "Postgres with no HA config should default to true", + dbType: migrator.Postgres, + haConfigValue: nil, + isHA: true, + }, + { + name: "No database type set should default to true", + dbType: "", + haConfigValue: nil, + isHA: true, + }, + { + name: "No database type set with HA enabled in config should default to true", + dbType: "", + haConfigValue: boolPtr(true), + isHA: true, + }, + { + name: "No database type set with HA disabled in config should default to false", + dbType: "", + haConfigValue: boolPtr(false), + isHA: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := setting.NewCfg().SectionWithEnvOverrides("database") + if tt.dbType != "" { + cfg.Key("type").SetValue(tt.dbType) + } + + if tt.haConfigValue != nil { + cfg.Key("high_availability").SetValue(strconv.FormatBool(*tt.haConfigValue)) + } + + result := isHighAvailabilityEnabled(cfg) + require.Equal(t, tt.isHA, result) + }) + } +} + +func boolPtr(b bool) *bool { + return &b +}