-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
39 lines (31 loc) · 879 Bytes
/
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
package main
import (
"gorm.io/gorm"
"gorm.io/driver/postgres"
"os"
"github.com/shopspring/decimal"
. "main/models"
)
// Initiase GORM
func initDb() {
//// gorm postgresql
dsn := "host=" + os.Getenv("postgres_host") + " user=" + os.Getenv("postgres_user") + " password=" + os.Getenv("postgres_pass") + " dbname=" + os.Getenv("postgres_db") + " port=" + os.Getenv("postgres_port") + " sslmode=disable TimeZone=Europe/London"
var err error
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic(err.Error())
}
// setup database??
db.AutoMigrate(&Main{})
db.AutoMigrate(&Asset{})
db.AutoMigrate(&Trade{})
db.AutoMigrate(&Position{})
main := Main{}
result := db.First(&main)
if result.RowsAffected == 0 {
main.Pool, err = decimal.NewFromString("1")
if err != nil {
panic(err)
}
}
}