diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 5e31c7005ac44d47f2fb94f8c8a5037e8d2425d5..fb8044c7a2f4c76b775840c5628319196a0493ae 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -4,11 +4,9 @@ import ( "context" "fmt" - "git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/content" "git.perx.ru/perxis/perxis-go/pkg/errors" "git.perx.ru/perxis/perxis-go/pkg/items" - "git.perx.ru/perxis/perxis-go/pkg/setup" pb "git.perx.ru/perxis/perxis-go/proto/extensions" ) @@ -107,19 +105,3 @@ func ExtensionFromError(err error) string { ext, _ := v.(string) return ext } - -func UpdateCollectionSchemaMetadata() setup.CollectionsOption { - return func(c *setup.CollectionConfig) { - c.UpdateFn = func(s *setup.Setup, exist, new *collections.Collection) (coll *collections.Collection, upd bool, setSchema bool, err error) { - if !new.IsView() && !exist.IsView() { - if new.Schema.Metadata != nil && exist.Schema.Metadata == nil && !s.IsForce() { - return nil, false, false, collections.ErrAlreadyExists - } - if new.Schema.Metadata != nil && exist.Schema.Metadata != nil { - return new, true, true, nil - } - } - return new, true, true, nil - } - } -} diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index f80e0827ce150095a0e075248590bd05c2ec0cf4..4cb6b0947de8bcd8f2f130ce3fc67c8371ca9af1 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -8,6 +8,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/environments" "git.perx.ru/perxis/perxis-go/pkg/errors" + "git.perx.ru/perxis/perxis-go/pkg/extension" "go.uber.org/zap" ) @@ -70,12 +71,28 @@ func DeleteCollectionIfRemove() CollectionsOption { func UpdateExistingCollection() CollectionsOption { return func(c *CollectionConfig) { - c.UpdateFn = func(s *Setup, exist, collection *collections.Collection) (*collections.Collection, bool, bool, error) { - if len(exist.Tags) > 0 { - collection.Tags = append(exist.Tags, collection.Tags...) - } + c.UpdateFn = UpdateExistingCollectionFn + } +} + +func UpdateExistingCollectionFn(s *Setup, exist, collection *collections.Collection) (*collections.Collection, bool, bool, error) { + if len(exist.Tags) > 0 { + collection.Tags = append(exist.Tags, collection.Tags...) + } + return collection, true, !collection.IsView() && !reflect.DeepEqual(exist.Schema, collection.Schema), nil +} - return collection, true, !collection.IsView() && !reflect.DeepEqual(exist.Schema, collection.Schema), nil +func UpdateCollectionSchemaMetadata() CollectionsOption { + return func(c *CollectionConfig) { + c.UpdateFn = func(s *Setup, exist, new *collections.Collection) (coll *collections.Collection, upd bool, setSchema bool, err error) { + if !new.IsView() && !exist.IsView() { + if new.Schema.Metadata != nil && new.Schema.Metadata[extension.ExtensionMetadataKey] != "" && !s.IsForce() { + if exist.Schema.Metadata == nil || exist.Schema.Metadata[extension.ExtensionMetadataKey] != new.Schema.Metadata[extension.ExtensionMetadataKey] { + return nil, false, false, collections.ErrAlreadyExists + } + } + } + return UpdateExistingCollectionFn(s, exist, new) } } }