init.go
1.1 KB
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
package pg
import (
"fmt"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/linmadan/egglib-go/persistent/pg/hooks"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/constant"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
)
var DB *pg.DB
func init() {
DB = pg.Connect(&pg.Options{
User: constant.POSTGRESQL_USER,
Password: constant.POSTGRESQL_PASSWORD,
Database: constant.POSTGRESQL_DB_NAME,
Addr: fmt.Sprintf("%s:%s", constant.POSTGRESQL_HOST, constant.POSTGRESQL_PORT),
})
if !constant.DISABLE_SQL_GENERATE_PRINT {
DB.AddQueryHook(hooks.SqlGeneratePrintHook{})
}
if !constant.DISABLE_CREATE_TABLE {
for _, model := range []interface{}{
(*models.Employee)(nil),
(*models.RobInfo)(nil),
(*models.BidderInfo)(nil),
(*models.BidInfo)(nil),
(*models.Task)(nil),
(*models.OffTaskRecord)(nil),
(*models.SuMoneyTransactionRecord)(nil),
} {
err := DB.CreateTable(model, &orm.CreateTableOptions{
Temp: false,
IfNotExists: true,
FKConstraints: true,
})
if err != nil {
panic(err)
}
}
}
}