diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 4c247953694209b3c05e8db4b04d3ae13dd1ca0d..d1c354d330ad10f866852286d9e4354e775673ac 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -31,7 +31,7 @@ type CollectionConfig struct { func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsOption) CollectionConfig { c := CollectionConfig{collection: collection} - UpdateExistingCollection()(&c) + DefaultUpdateCollStrategy()(&c) DeleteCollectionIfRemove()(&c) for _, o := range opt { @@ -69,30 +69,20 @@ func DeleteCollectionIfRemove() CollectionsOption { } } -func UpdateExistingCollection() CollectionsOption { +func DefaultUpdateCollStrategy() CollectionsOption { return func(c *CollectionConfig) { - 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 -} + 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...) + } -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 - } + if !s.IsForce() && !collection.IsView() && !exist.IsView() && collection.Schema.Metadata != nil && collection.Schema.Metadata[extension.ExtensionMetadataKey] != "" { + if exist.Schema.Metadata == nil || exist.Schema.Metadata[extension.ExtensionMetadataKey] != collection.Schema.Metadata[extension.ExtensionMetadataKey] { + return nil, false, false, collections.ErrAlreadyExists } } - return UpdateExistingCollectionFn(s, exist, new) + + return collection, true, !collection.IsView() && !reflect.DeepEqual(exist.Schema, collection.Schema), nil } } } diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index 66c5e25676d51d944681f4c80915600433b2bb99..2e807a88906548480d18ce69a74e7fdc199dc7d9 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -23,7 +23,6 @@ func TestSetup_InstallCollections(t *testing.T) { collections []*collections.Collection collectionsCall func(svc *mockscollections.Collections) envsCall func(svc *envmocks.Environments) - co []CollectionsOption force bool wantErr func(t *testing.T, err error) }{ @@ -108,7 +107,6 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.NoError(t, err) }, - co: []CollectionsOption{UpdateCollectionSchemaMetadata()}, }, { name: "Fail to update user collection with same id as in extensions collection", @@ -120,7 +118,6 @@ func TestSetup_InstallCollections(t *testing.T) { assert.Error(t, err) assert.ErrorIs(t, err, collections.ErrAlreadyExists) }, - co: []CollectionsOption{UpdateCollectionSchemaMetadata()}, }, { name: "Update user collection with same id as in extensions collection with force", @@ -136,7 +133,6 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.NoError(t, err) }, - co: []CollectionsOption{UpdateCollectionSchemaMetadata()}, force: true, }, { @@ -182,7 +178,7 @@ func TestSetup_InstallCollections(t *testing.T) { } s := NewSetup(&content.Content{Collections: c, Environments: e}, "sp", "env", nil).WithForce(tt.force) - s.AddCollections(tt.collections, tt.co...) + s.AddCollections(tt.collections) tt.wantErr(t, s.InstallCollections(context.Background())) }) }