-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.go
55 lines (51 loc) · 1.22 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package state
import (
"fmt"
"log"
"os"
"time"
"github.com/dipdup-net/go-lib/config"
"github.com/pkg/errors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// OpenConnection -
func OpenConnection(cfg config.Database) (*gorm.DB, error) {
var dialector gorm.Dialector
switch cfg.Kind {
case config.DBKindSqlite:
dialector = sqlite.Open(cfg.Path)
case config.DBKindPostgres:
connString := cfg.Path
if connString == "" {
connString = fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%d",
cfg.Host, cfg.User, cfg.Password, cfg.Database, cfg.Port,
)
}
dialector = postgres.Open(connString)
case config.DBKindMysql:
connString := cfg.Path
if connString == "" {
connString = fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s",
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database,
)
}
dialector = mysql.Open(connString)
default:
return nil, errors.Errorf("Unsupported database kind %s", cfg.Kind)
}
return gorm.Open(dialector, &gorm.Config{
Logger: logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
IgnoreRecordNotFoundError: true,
},
),
})
}