init.go
1.6 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pg
import (
"context"
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
"log"
)
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(SqlGeneratePrintHook{})
}
//orm.RegisterTable((*models.OrderGood)(nil))
if !constant.DISABLE_CREATE_TABLE {
for _, model := range []interface{}{
(*models.Access)(nil),
(*models.CasbinRule)(nil),
(*models.Menu)(nil),
(*models.ProjectModule)(nil),
(*models.ProjectModuleFiles)(nil),
(*models.ProjectModuleVersion)(nil),
(*models.Role)(nil),
(*models.RoleAccess)(nil),
(*models.Users)(nil),
(*models.ClientVersion)(nil),
(*models.OperatorLog)(nil),
} {
err := DB.CreateTable(model, &orm.CreateTableOptions{
Temp: false,
IfNotExists: true,
FKConstraints: true,
})
if err != nil {
panic(err)
}
}
}
}
type SqlGeneratePrintHook struct{}
func (hook SqlGeneratePrintHook) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent) error {
data, err := q.FormattedQuery()
//if len(string(data)) > 8 { //BEGIN COMMIT
log.Println(string(data))
//}
return err
}