Update Optimize schema (#4364)

This commit is contained in:
Chen Quan 2021-05-10 09:51:50 +08:00 committed by GitHub
parent 6b7abc54a2
commit a480bd8545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 8 deletions

View File

@ -71,7 +71,7 @@ type Tabler interface {
TableName() string TableName() string
} }
// get data type from dialector // Parse get data type from dialector
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
if dest == nil { if dest == nil {
return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest) return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
@ -91,6 +91,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
if v, ok := cacheStore.Load(modelType); ok { if v, ok := cacheStore.Load(modelType); ok {
s := v.(*Schema) s := v.(*Schema)
// Wait for the initialization of other goroutines to complete
<-s.initialized <-s.initialized
return s, s.err return s, s.err
} }
@ -115,6 +116,15 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
namer: namer, namer: namer,
initialized: make(chan struct{}), initialized: make(chan struct{}),
} }
// When the schema initialization is completed, the channel will be closed
defer close(schema.initialized)
if v, loaded := cacheStore.LoadOrStore(modelType, schema); loaded {
s := v.(*Schema)
// Wait for the initialization of other goroutines to complete
<-s.initialized
return s, s.err
}
defer func() { defer func() {
if schema.err != nil { if schema.err != nil {
@ -223,13 +233,6 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
} }
} }
if v, loaded := cacheStore.LoadOrStore(modelType, schema); loaded {
s := v.(*Schema)
<-s.initialized
return s, s.err
}
defer close(schema.initialized)
if _, embedded := schema.cacheStore.Load(embeddedCacheKey); !embedded { if _, embedded := schema.cacheStore.Load(embeddedCacheKey); !embedded {
for _, field := range schema.Fields { for _, field := range schema.Fields {
if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) { if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) {