From 6ad38a7535492182b911ac6e184cdca1efbaede6 Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Mon, 3 Jul 2023 20:00:35 +0300
Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?=
 =?UTF-8?q?=D0=BE=20=D0=9F=D0=A0:=20-=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?=
 =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D0=B5=20metadata?=
 =?UTF-8?q?=20=D0=B2=20CollectionConfig=20-=20=D0=B4=D0=BE=D0=B1=D0=B0?=
 =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D0=BF=D1=86=D0=B8=D1=8F?=
 =?UTF-8?q?=20SetSchemaMetadata?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection.go      | 15 ++++++++++++---
 pkg/setup/collection_test.go |  9 ++++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 8d719137..679ef262 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -23,6 +23,7 @@ type DeleteCollectionFn func(s *Setup, col *collections.Collection) bool
 
 type CollectionConfig struct {
 	collection *collections.Collection
+	metadata   map[string]string
 	UpdateFn   UpdateCollectionFn
 	DeleteFn   DeleteCollectionFn
 }
@@ -80,6 +81,12 @@ func UpdateExistingCollection() CollectionsOption {
 	}
 }
 
+func SetSchemaMetadata(md map[string]string) CollectionsOption {
+	return func(c *CollectionConfig) {
+		c.metadata = md
+	}
+}
+
 func (s *Setup) InstallCollections(ctx context.Context) (err error) {
 	if len(s.Collections) == 0 {
 		return nil
@@ -164,9 +171,11 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 }
 
 func (s *Setup) checkSchemaMetadata(collection *collections.Collection, exist *collections.Collection) error {
-	if _, ok := collection.Schema.Metadata["extension"]; ok {
-		if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() {
-			return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
+	if collection.Schema.Metadata != nil {
+		if _, ok := collection.Schema.Metadata["extension"]; ok {
+			if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() {
+				return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
+			}
 		}
 	}
 	return nil
diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index bd90346f..2b9f8e4c 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -22,6 +22,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 		collections     []*collections.Collection
 		collectionsCall func(svc *mockscollections.Collections)
 		envsCall        func(svc *envmocks.Environments)
+		co              CollectionsOption
 		wantErr         func(t *testing.T, err error)
 	}{
 		{
@@ -105,6 +106,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.NoError(t, err)
 			},
+			co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}),
 		},
 		{
 			name:        "Fail to update collection with the same id",
@@ -115,6 +117,7 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.Error(t, err)
 			},
+			co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}),
 		},
 	}
 
@@ -130,7 +133,11 @@ func TestSetup_InstallCollections(t *testing.T) {
 			}
 
 			s := NewSetup(&content.Content{Collections: c, Environments: e}, "sp", "env", nil)
-			s.AddCollections(tt.collections)
+			if tt.co != nil {
+				s.AddCollections(tt.collections, tt.co)
+			} else {
+				s.AddCollections(tt.collections)
+			}
 			tt.wantErr(t, s.InstallCollections(context.Background()))
 		})
 	}
-- 
GitLab