-
Notifications
You must be signed in to change notification settings - Fork 13
/
orm_config.go
131 lines (98 loc) · 3.29 KB
/
orm_config.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package registry
import (
"database/sql"
"errors"
"fmt"
"os"
"strings"
"github.com/fatih/color"
"github.com/latolukasz/beeorm"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
"github.com/coretrix/hitrix/service/component/config"
)
type ORMRegistryInitFunc func(registry *beeorm.Registry)
var sequence int
func ServiceProviderOrmRegistry(init ORMRegistryInitFunc) *service.DefinitionGlobal {
var defferFunc func()
var err error
var ormConfig beeorm.ValidatedRegistry
var appService *app.App
var configService config.IConfig
return &service.DefinitionGlobal{
Name: service.ORMConfigService,
Build: func(ctn di.Container) (interface{}, error) {
appService = ctn.Get(service.AppService).(*app.App)
configService = ctn.Get(service.ConfigService).(config.IConfig)
registry := beeorm.NewRegistry()
configuration, ok := configService.Get("orm")
if !ok {
return nil, errors.New("no orm config")
}
yamlConfig := map[string]interface{}{}
for k, v := range configuration.(map[interface{}]interface{}) {
yamlConfig[fmt.Sprint(k)] = v
}
if appService.IsInTestMode() {
overwriteORMConfig(appService, configService, yamlConfig)
}
registry.InitByYaml(yamlConfig)
if appService.IsInTestMode() {
registry.ForceEntityLogInAllEntities("")
}
init(registry)
ormConfig, defferFunc, err = registry.Validate()
return ormConfig, err
},
Close: func(obj interface{}) error {
defferFunc()
return nil
},
}
}
func overwriteORMConfig(appService *app.App, configService config.IConfig, yamlConfig map[string]interface{}) {
mysqlConnection := strings.Split(configService.MustString("orm.default.mysql"), "/")
db, err := sql.Open("mysql", mysqlConnection[0]+"/?multiStatements=true")
if err != nil {
panic(err)
}
defer db.Close()
newDBName := "t_" + appService.ParallelTestID
color.Blue("DB name: %s", newDBName)
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS `" + newDBName + "`")
if err != nil {
panic(err)
}
yamlConfig["default"].(map[interface{}]interface{})["mysql"] = mysqlConnection[0] + "/" + newDBName
connectionString, has := configService.String("orm.log_db_pool.mysql")
if has {
mysqlLogConnection := strings.Split(connectionString, "/")
dbLog, err := sql.Open("mysql", mysqlLogConnection[0]+"/?multiStatements=true")
if err != nil {
panic(err)
}
defer dbLog.Close()
newDBLogName := newDBName + "_log"
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS `" + newDBLogName + "`")
if err != nil {
panic(err)
}
yamlConfig["log_db_pool"].(map[interface{}]interface{})["mysql"] = mysqlLogConnection[0] + "/" + newDBLogName
}
for _, value := range yamlConfig {
if _, ok := value.(map[interface{}]interface{})["sentinel"]; ok {
for masterConf := range value.(map[interface{}]interface{})["sentinel"].(map[interface{}]interface{}) {
settings := strings.Split(fmt.Sprint(masterConf), ":")
_, has = os.LookupEnv("REDIS_TEST")
if !has {
panic("Please set `REDIS_TEST` ENV variable")
}
sequence++
//host:dbIndex:namespace
value.(map[interface{}]interface{})["redis"] = os.Getenv("REDIS_TEST") + ":" + settings[1] + ":" + newDBName + fmt.Sprint(sequence)
delete(value.(map[interface{}]interface{}), "sentinel")
}
}
}
}