Skip to content

Commit

Permalink
check configuration data
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 23, 2024
1 parent 5261a37 commit a140302
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
7 changes: 0 additions & 7 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ const DefaultValues = `
Environment = "development" # "production" or "development"
Level = "info"
Outputs = ["stderr"]
[DB]
Name = "sync"
User = "test_user"
Password = "test_password"
Host = "localhost"
Port = "5436"
MaxConns = 10
[SQLDB]
DriverName = "choose_driver. example: sqlite3 or postgres"
DataSource = "example for sqlite3: file:/tmp/sync_db.sqlite"
Expand Down
19 changes: 19 additions & 0 deletions state/storage/sqlstorage/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package sqlstorage

import "fmt"

type Config struct {
// Name of the database
DriverName string `mapstructure:"DriverName"`
DataSource string `mapstructure:"DataSource"`
}

func (c *Config) String() string {
return fmt.Sprintf("DriverName=%s DataSource=%s", c.DriverName, c.DataSource)
}

func (c *Config) SanityCheck() error {
if c.DriverName == "" {
return fmt.Errorf("DriverName is required")
}
if c.DataSource == "" {
return fmt.Errorf("DataSource is required")
}
if c.DriverName != SqliteDriverName {
return fmt.Errorf("DriverName not supported: %s, only supports: %s", c.DriverName, SqliteDriverName)
}
return nil
}
8 changes: 6 additions & 2 deletions state/storage/sqlstorage/sqlstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ const (
)

func NewSqlStorage(cfg Config, runMigrations bool) (*SqlStorage, error) {
log.Infof("Running DB migrations")

err := cfg.SanityCheck()
if err != nil {
return nil, fmt.Errorf("config %s dont pass sanityCheck. Err: %w", cfg.String(), err)
}
log.Infof("Opening sync DB: cfg=%s", cfg.String())
db, err := sql.Open(cfg.DriverName, cfg.DataSource)
if err != nil {
log.Errorf("Unable to connect to database: %v\n", err)
return nil, err
}
if runMigrations {
log.Infof("Running DB migrations")
err := RunMigrationsUp(cfg.DriverName, db)
if err != nil {
err := fmt.Errorf("error executing migrations: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
Version = "v1.0.1"
Version = "v1.0.2"
)

// PrintVersion prints version info into the provided io.Writer.
Expand Down

0 comments on commit a140302

Please sign in to comment.