diff --git a/pkg/sqlstore/config.go b/pkg/sqlstore/config.go index 6902067c788..126d02fed82 100644 --- a/pkg/sqlstore/config.go +++ b/pkg/sqlstore/config.go @@ -6,14 +6,18 @@ import "go.signoz.io/signoz/pkg/config" var _ config.Config = (*Config)(nil) type Config struct { - Provider string `mapstructure:"provider"` - Sqlite SqliteConfig `mapstructure:"sqlite"` - Postgres PostgresConfig `mapstructure:"postgres"` + Provider string `mapstructure:"provider"` + Connection ConnectionConfig `mapstructure:",squash"` + Sqlite SqliteConfig `mapstructure:"sqlite"` + Postgres PostgresConfig `mapstructure:"postgres"` } type SqliteConfig struct { - Path string `mapstructure:"path"` - MaxOpenConns int `mapstructure:"max_open_conns"` + Path string `mapstructure:"path"` +} + +type ConnectionConfig struct { + MaxOpenConns int `mapstructure:"max_open_conns"` } type PostgresConfig struct { @@ -31,10 +35,12 @@ func NewConfigFactory() config.ConfigFactory { func newConfig() config.Config { return &Config{ Provider: "sqlite", - Sqlite: SqliteConfig{ - Path: "/var/lib/signoz/signoz.db", + Connection: ConnectionConfig{ MaxOpenConns: 100, }, + Sqlite: SqliteConfig{ + Path: "/var/lib/signoz/signoz.db", + }, } } diff --git a/pkg/sqlstore/provider/sqlite/provider.go b/pkg/sqlstore/provider/sqlite/provider.go index ebcbf3ecfe3..28169e5e8df 100644 --- a/pkg/sqlstore/provider/sqlite/provider.go +++ b/pkg/sqlstore/provider/sqlite/provider.go @@ -23,21 +23,17 @@ func New(config sqlstore.Config, providerConfig sqlstore.ProviderConfig) (sqlsto return nil, fmt.Errorf("provider %q is not supported by sqlite", config.Provider) } - sqlDB, err := sql.Open("sqlite3", "file:"+config.Sqlite.Path) + sqlDB, err := sql.Open("sqlite3", "file:"+config.Sqlite.Path+"?_foreign_keys=true") if err != nil { return nil, err } - sqlDB.SetMaxOpenConns(config.Sqlite.MaxOpenConns) - bunDB := bun.NewDB(sqlDB, sqlitedialect.New()) + providerConfig.Logger.Info("connected to sqlite", zap.String("path", config.Sqlite.Path)) - providerConfig.Logger.Info("connected to sqlite", zap.String("path", config.Sqlite.Path+"?_foreign_keys=true")) - - // enable foreign key support - if _, err := bunDB.Exec("PRAGMA foreign_keys = ON;"); err != nil { - return nil, err - } - providerConfig.Logger.Info("enabled foreign key support in sqlite", zap.String("path", config.Sqlite.Path)) + // Set connection options + sqlDB.SetMaxOpenConns(config.Connection.MaxOpenConns) + // Initialize ORMs + bunDB := bun.NewDB(sqlDB, sqlitedialect.New()) sqlxDB := sqlx.NewDb(sqlDB, "sqlite3") return &provider{