Skip to content

Commit 87d3df6

Browse files
committed
Fix db connection string & cmdline arg
1 parent 3b45357 commit 87d3df6

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

cmdline/commandline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Args struct {
1111
// Parse -
1212
func Parse() (args Args) {
1313
flag.BoolVar(&args.Help, "h", false, "Show usage")
14-
flag.StringVar(&args.Config, "f", "config.yaml", "Path to YAML config file")
14+
flag.StringVar(&args.Config, "c", "dipdup.yml", "Path to YAML config file")
1515

1616
flag.Parse()
1717

config/config.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ type Contract struct {
4141

4242
// Database
4343
type Database struct {
44-
Path string `yaml:"path"`
45-
Kind string `yaml:"kind"`
44+
Path string `yaml:"path"`
45+
Kind string `yaml:"kind"`
46+
Host string `yaml:"host"`
47+
Port int `yaml:"port"`
48+
User string `yaml:"user"`
49+
Password string `yaml:"password"`
50+
Database string `yaml:"database"`
51+
SchemaName string `yaml:"schema_name"`
4652
}
4753

4854
// Hasura -
@@ -53,12 +59,28 @@ type Hasura struct {
5359

5460
// Validate -
5561
func (db *Database) Validate() error {
56-
for _, valid := range []string{
57-
DBKindClickHouse, DBKindMysql, DBKindPostgres, DBKindSqlServer, DBKindSqlite,
58-
} {
59-
if valid == db.Kind {
60-
return nil
62+
if db.Kind == DBKindSqlite {
63+
if db.Path == "" {
64+
return errors.Wrap(ErrMissingField, "Path")
65+
}
66+
return nil
67+
} else if db.Kind == DBKindPostgres || db.Kind == DBKindMysql {
68+
if db.Host == "" {
69+
return errors.Wrap(ErrMissingField, "Host")
70+
}
71+
if db.Port == 0 {
72+
return errors.Wrap(ErrMissingField, "Port")
73+
}
74+
if db.User == "" {
75+
return errors.Wrap(ErrMissingField, "User")
76+
}
77+
if db.Password == "" {
78+
return errors.Wrap(ErrMissingField, "Password")
79+
}
80+
if db.Database == "" {
81+
return errors.Wrap(ErrMissingField, "Database")
6182
}
83+
return nil
6284
}
6385
return errors.Wrap(ErrUnsupportedDB, db.Kind)
6486
}

config/consts.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import "github.com/pkg/errors"
44

55
// Supported database kinds
66
const (
7-
DBKindSqlite = "sqlite"
8-
DBKindPostgres = "postgres"
9-
DBKindMysql = "mysql"
10-
DBKindClickHouse = "clickhouse"
11-
DBKindSqlServer = "sqlserver"
7+
DBKindSqlite = "sqlite"
8+
DBKindPostgres = "postgres"
9+
DBKindMysql = "mysql"
1210
)
1311

1412
var (
1513
ErrUnsupportedDB = errors.New("Unsupported database")
14+
ErrMissingField = errors.New("Missing field")
1615
)

state/db.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
package state
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"time"
78

9+
"github.com/dipdup-net/go-lib/config"
810
"github.com/pkg/errors"
9-
"gorm.io/driver/clickhouse"
1011
"gorm.io/driver/mysql"
1112
"gorm.io/driver/postgres"
1213
"gorm.io/driver/sqlite"
13-
"gorm.io/driver/sqlserver"
1414
"gorm.io/gorm"
1515
"gorm.io/gorm/logger"
1616
)
1717

18-
// Supported database kinds
19-
const (
20-
DBKindSqlite = "sqlite"
21-
DBKindPostgres = "postgres"
22-
DBKindMysql = "mysql"
23-
DBKindClickHouse = "clickhouse"
24-
DBKindSqlServer = "sqlserver"
25-
)
26-
2718
// OpenConnection -
28-
func OpenConnection(kind, path string) (*gorm.DB, error) {
19+
func OpenConnection(cfg config.Database) (*gorm.DB, error) {
2920
var dialector gorm.Dialector
30-
switch kind {
31-
case DBKindSqlite:
32-
dialector = sqlite.Open(path)
33-
case DBKindPostgres:
34-
dialector = postgres.Open(path)
35-
case DBKindMysql:
36-
dialector = mysql.Open(path)
37-
case DBKindClickHouse:
38-
dialector = clickhouse.Open(path)
39-
case DBKindSqlServer:
40-
dialector = sqlserver.Open(path)
21+
switch cfg.Kind {
22+
case config.DBKindSqlite:
23+
dialector = sqlite.Open(cfg.Path)
24+
case config.DBKindPostgres:
25+
connString := fmt.Sprintf(
26+
"host=%s user=%s password=%s dbname=%s port=%d",
27+
cfg.Host, cfg.User, cfg.Password, cfg.Database, cfg.Port,
28+
)
29+
dialector = postgres.Open(connString)
30+
case config.DBKindMysql:
31+
connString := fmt.Sprintf(
32+
"%s:%s@tcp(%s:%d)/%s",
33+
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database,
34+
)
35+
dialector = mysql.Open(connString)
4136
default:
42-
return nil, errors.Errorf("Unsupported database %s", kind)
37+
return nil, errors.Errorf("Unsupported database kind %s", cfg.Kind)
4338
}
4439

4540
return gorm.Open(dialector, &gorm.Config{

0 commit comments

Comments
 (0)