-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsystem.go
288 lines (249 loc) · 11.6 KB
/
subsystem.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package reference
import (
"context"
"fmt"
golog "log"
"github.com/yaruz/app/pkg/yarus_platform/config"
"github.com/yaruz/app/pkg/yarus_platform/infrastructure"
minipkg_gorm "github.com/minipkg/db/gorm"
"github.com/minipkg/log"
"github.com/pkg/errors"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/entity_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/entity_type2property"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_group"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_type2property_view_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_unit"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/property_view_type"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/text_lang"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/text_source"
"github.com/yaruz/app/pkg/yarus_platform/reference/domain/text_value"
"github.com/yaruz/app/pkg/yarus_platform/reference/infrastructure/repository/gorm"
)
type EntityIDRepository interface {
AutoMigrate() error
NextVal(entityTypeSysname string) (id uint, err error)
LastVal(entityTypeSysname string) (id uint, err error)
}
type ReferenceSubsystem struct {
EntityType entity_type.IService
entityTypeRepository entity_type.Repository
EntityType2Property entity_type2property.IService
entityType2PropertyRepository entity_type2property.Repository
Property property.IService
propertyRepository property.Repository
Relation entity_type.RelationService
relationRepository entity_type.RelationRepository
PropertyGroup property_group.IService
propertyGroupRepository property_group.Repository
PropertyType property_type.IService
propertyTypeRepository property_type.Repository
propertyType2PropertyViewTypeRepository property_type2property_view_type.Repository
PropertyUnit property_unit.IService
propertyUnitRepository property_unit.Repository
PropertyViewType property_view_type.IService
propertyViewTypeRepository property_view_type.Repository
TextSource text_source.IService
textSourceRepository text_source.Repository
TextValue text_value.IService
textValueRepository text_value.Repository
TextLang text_lang.IService
textLangRepository text_lang.Repository
EntityIDRepository EntityIDRepository
}
func NewReferenceSubsystem(infra *infrastructure.Infrastructure, cfg *config.Configuration) (*ReferenceSubsystem, error) {
s := &ReferenceSubsystem{}
s.EntityIDRepository = gorm.GetEntityIDRepository(infra.Logger, infra.ReferenceDB, cfg.Infrastructure.DataSharding.GetEntityTypesByClusterSysnames())
if err := s.setupRepositories(infra); err != nil {
return nil, err
}
s.setupServices(infra.Logger)
//if err := s.autoMigrate(infra.ReferenceDB); err != nil {
// return nil, err
//}
if err := s.dbDataInit(cfg.Metadata); err != nil {
return nil, err
}
return s, nil
}
func (s *ReferenceSubsystem) autoMigrate(db minipkg_gorm.IDB) error {
if db.IsAutoMigrate() {
s.EntityIDRepository.AutoMigrate()
err := db.DB().SetupJoinTable(&entity_type.EntityType{}, "Properties", &entity_type2property.EntityType2Property{})
if err != nil {
return err
}
err = db.DB().AutoMigrate(
&text_lang.TextLang{},
&text_source.TextSource{},
&text_value.TextValue{},
&property_unit.PropertyUnit{},
&property_group.PropertyGroup{},
&property_type.PropertyType{},
&property_view_type.PropertyViewType{},
&property.Property{},
&entity_type.EntityType{},
)
if err != nil {
return err
}
if err = s.dbStructFix(db); err != nil {
return err
}
}
return nil
}
func (s *ReferenceSubsystem) setupRepositories(infra *infrastructure.Infrastructure) (err error) {
var ok bool
repo, err := gorm.GetRepository(infra.Logger, infra.ReferenceDB, text_source.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", text_source.EntityName, err)
}
s.textSourceRepository, ok = repo.(text_source.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", text_source.EntityName, text_source.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property_type.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property_type.EntityName, err)
}
s.propertyTypeRepository, ok = repo.(property_type.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property_type.EntityName, property_type.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property_view_type.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property_view_type.EntityName, err)
}
s.propertyViewTypeRepository, ok = repo.(property_view_type.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property_view_type.EntityName, property_view_type.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, entity_type.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", entity_type.EntityName, err)
}
s.entityTypeRepository, ok = repo.(entity_type.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", entity_type.EntityName, entity_type.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property_unit.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property_unit.EntityName, err)
}
s.propertyUnitRepository, ok = repo.(property_unit.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property_unit.EntityName, property_unit.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property_group.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property_group.EntityName, err)
}
s.propertyGroupRepository, ok = repo.(property_group.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property_group.EntityName, property_group.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property_type2property_view_type.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property_type2property_view_type.EntityName, err)
}
s.propertyType2PropertyViewTypeRepository, ok = repo.(property_type2property_view_type.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property_type2property_view_type.EntityName, property_type2property_view_type.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, property.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", property.EntityName, err)
}
s.propertyRepository, ok = repo.(property.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", property.EntityName, property.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, text_value.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", text_value.EntityName, err)
}
s.textValueRepository, ok = repo.(text_value.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", text_value.EntityName, text_value.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, entity_type2property.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", entity_type2property.EntityName, err)
}
s.entityType2PropertyRepository, ok = repo.(entity_type2property.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", entity_type2property.EntityName, entity_type2property.EntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, entity_type.RelationEntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", entity_type.RelationEntityName, err)
}
s.relationRepository, ok = repo.(entity_type.RelationRepository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", entity_type.RelationEntityName, entity_type.RelationEntityName, repo)
}
repo, err = gorm.GetRepository(infra.Logger, infra.ReferenceDB, text_lang.EntityName)
if err != nil {
golog.Fatalf("Can not get db repository for entity %q, error happened: %v", text_lang.EntityName, err)
}
s.textLangRepository, ok = repo.(text_lang.Repository)
if !ok {
return errors.Errorf("Can not cast DB repository for entity %q to %vRepository. Repo: %v", text_lang.EntityName, text_lang.EntityName, repo)
}
return nil
}
func (s *ReferenceSubsystem) setupServices(logger log.Logger) {
s.TextLang = text_lang.NewService(logger, s.textLangRepository)
s.TextSource = text_source.NewService(logger, s.textSourceRepository)
s.TextValue = text_value.NewService(logger, s.textValueRepository)
s.EntityType2Property = entity_type2property.NewService(logger, s.entityType2PropertyRepository)
s.PropertyGroup = property_group.NewService(logger, s.propertyGroupRepository)
s.PropertyType = property_type.NewService(logger, s.propertyTypeRepository, s.propertyType2PropertyViewTypeRepository, s.TextLang)
s.PropertyUnit = property_unit.NewService(logger, s.propertyUnitRepository, s.TextLang)
s.PropertyViewType = property_view_type.NewService(logger, s.propertyViewTypeRepository)
s.Property = property.NewService(logger, s.propertyRepository, s.PropertyType, s.PropertyUnit, s.PropertyViewType, s.PropertyGroup, s.TextLang)
s.Relation = entity_type.NewRelationService(logger, s.relationRepository, s.Property)
s.EntityType = entity_type.NewService(logger, s.entityTypeRepository, s.Relation, s.Property, s.TextLang)
}
func (s *ReferenceSubsystem) dbStructFix(db minipkg_gorm.IDB) error {
typeSpec := "DROP NOT NULL"
fields := []string{
"property_unit_id",
"property_view_type_id",
"property_group_id",
}
for _, f := range fields {
sql := fmt.Sprintf("ALTER TABLE %q ALTER COLUMN %q %s",
property.TableName,
f,
typeSpec,
)
err := db.DB().Exec(sql).Error
if err != nil {
return err
}
}
return nil
}
func (s *ReferenceSubsystem) dbDataInit(metadata *config.Metadata) error {
ctx := context.Background()
err := s.TextLang.DataInit(ctx, metadata.Languages)
if err != nil {
return err
}
err = s.PropertyType.DataInit(ctx)
if err != nil {
return err
}
err = s.PropertyUnit.DataInit(ctx, metadata.PropertyUnits)
if err != nil {
return err
}
err = s.EntityType.DataInit(ctx, metadata.EntityTypes, metadata.Relations)
if err != nil {
return err
}
return nil
}