From 2fd733200e249272caabb06fb23e56c6b7de997c Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Wed, 12 Jul 2023 17:21:02 +0300
Subject: [PATCH] =?UTF-8?q?-=20UpdateExistingCollection()=20->=20DefaultUp?=
 =?UTF-8?q?dateCollStrategy=20-=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?=
 =?UTF-8?q?=D0=BA=D0=B0=20metadata=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?=
 =?UTF-8?q?=D1=81=D0=B5=D0=BD=D0=B0=20=D0=B2=20DefaultUpdateCollStrategy?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection.go      | 32 +++++++++++---------------------
 pkg/setup/collection_test.go |  6 +-----
 2 files changed, 12 insertions(+), 26 deletions(-)

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 4c247953..d1c354d3 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 66c5e256..2e807a88 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()))
 		})
 	}
-- 
GitLab