-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqlstore): add sqlstore package (#6835)
### Summary Add `sqlstore` package
- Loading branch information
1 parent
3675457
commit dfb2270
Showing
6 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sqlstore | ||
|
||
import ( | ||
"go.signoz.io/signoz/pkg/factory" | ||
) | ||
|
||
type Config struct { | ||
// Provider is the provider to use. | ||
Provider string `mapstructure:"provider"` | ||
// Connection is the connection configuration. | ||
Connection ConnectionConfig `mapstructure:",squash"` | ||
// Sqlite is the sqlite configuration. | ||
Sqlite SqliteConfig `mapstructure:"sqlite"` | ||
} | ||
|
||
type SqliteConfig struct { | ||
// Path is the path to the sqlite database. | ||
Path string `mapstructure:"path"` | ||
} | ||
|
||
type ConnectionConfig struct { | ||
// MaxOpenConns is the maximum number of open connections to the database. | ||
MaxOpenConns int `mapstructure:"max_open_conns"` | ||
} | ||
|
||
func NewConfigFactory() factory.ConfigFactory { | ||
return factory.NewConfigFactory(factory.MustNewName("sqlstore"), newConfig) | ||
} | ||
|
||
func newConfig() factory.Config { | ||
return Config{ | ||
Provider: "sqlite", | ||
Connection: ConnectionConfig{ | ||
MaxOpenConns: 100, | ||
}, | ||
Sqlite: SqliteConfig{ | ||
Path: "/var/lib/signoz/signoz.db", | ||
}, | ||
} | ||
|
||
} | ||
|
||
func (c Config) Validate() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package sqlitesqlstore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/jmoiron/sqlx" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/sqlitedialect" | ||
"go.signoz.io/signoz/pkg/factory" | ||
"go.signoz.io/signoz/pkg/sqlstore" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type provider struct { | ||
settings factory.ScopedProviderSettings | ||
sqldb *sql.DB | ||
bundb *bun.DB | ||
sqlxdb *sqlx.DB | ||
} | ||
|
||
func NewFactory() factory.ProviderFactory[sqlstore.SQLStore, sqlstore.Config] { | ||
return factory.NewProviderFactory(factory.MustNewName("sqlite"), New) | ||
} | ||
|
||
func New(ctx context.Context, providerSettings factory.ProviderSettings, config sqlstore.Config) (sqlstore.SQLStore, error) { | ||
settings := factory.NewScopedProviderSettings(providerSettings, "go.signoz.io/signoz/pkg/sqlitesqlstore") | ||
|
||
sqldb, err := sql.Open("sqlite3", "file:"+config.Sqlite.Path+"?_foreign_keys=true") | ||
if err != nil { | ||
return nil, err | ||
} | ||
settings.ZapLogger().Info("connected to sqlite", zap.String("path", config.Sqlite.Path)) | ||
sqldb.SetMaxOpenConns(config.Connection.MaxOpenConns) | ||
|
||
return &provider{ | ||
settings: settings, | ||
sqldb: sqldb, | ||
bundb: bun.NewDB(sqldb, sqlitedialect.New()), | ||
sqlxdb: sqlx.NewDb(sqldb, "sqlite3"), | ||
}, nil | ||
} | ||
|
||
func (provider *provider) BunDB() *bun.DB { | ||
return provider.bundb | ||
} | ||
|
||
func (provider *provider) SQLDB() *sql.DB { | ||
return provider.sqldb | ||
} | ||
|
||
func (provider *provider) SQLxDB() *sqlx.DB { | ||
return provider.sqlxdb | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package sqlstore | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
// SQLStore is the interface for the SQLStore. | ||
type SQLStore interface { | ||
// SQLDB returns the underlying sql.DB. | ||
SQLDB() *sql.DB | ||
// BunDB returns an instance of bun.DB. This is the recommended way to interact with the database. | ||
BunDB() *bun.DB | ||
// SQLxDB returns an instance of sqlx.DB. | ||
SQLxDB() *sqlx.DB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package sqlstoretest | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/sqlitedialect" | ||
"go.signoz.io/signoz/pkg/sqlstore" | ||
) | ||
|
||
var _ sqlstore.SQLStore = (*Provider)(nil) | ||
|
||
type Provider struct { | ||
db *sql.DB | ||
mock sqlmock.Sqlmock | ||
bunDB *bun.DB | ||
sqlxDB *sqlx.DB | ||
} | ||
|
||
func New(config sqlstore.Config, matcher sqlmock.QueryMatcher) *Provider { | ||
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(matcher)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var bunDB *bun.DB | ||
var sqlxDB *sqlx.DB | ||
|
||
if config.Provider == "sqlite" { | ||
bunDB = bun.NewDB(db, sqlitedialect.New()) | ||
sqlxDB = sqlx.NewDb(db, "sqlite3") | ||
} else { | ||
panic(fmt.Errorf("provider %q is not supported by mockSQLStore", config.Provider)) | ||
} | ||
|
||
return &Provider{ | ||
db: db, | ||
mock: mock, | ||
bunDB: bunDB, | ||
sqlxDB: sqlxDB, | ||
} | ||
} | ||
|
||
func (provider *Provider) BunDB() *bun.DB { | ||
return provider.bunDB | ||
} | ||
|
||
func (provider *Provider) SQLDB() *sql.DB { | ||
return provider.db | ||
} | ||
|
||
func (provider *Provider) SQLxDB() *sqlx.DB { | ||
return provider.sqlxDB | ||
} | ||
|
||
func (provider *Provider) Mock() sqlmock.Sqlmock { | ||
return provider.mock | ||
} |