From 3d2d5439d6baaf57722abb239e3836c79c54f483 Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Fri, 7 Jul 2023 14:13:42 +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?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 09c13cd1..008fb711 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -12,9 +12,10 @@ import (
 )
 
 var (
-	ErrCheckCollections     = errors.New("collections check error")
-	ErrInstallCollections   = errors.New("failed to install collections")
-	ErrUninstallCollections = errors.New("failed to uninstall collections")
+	ErrCheckCollections        = errors.New("collections check error")
+	ErrInstallCollections      = errors.New("failed to install collections")
+	ErrUninstallCollections    = errors.New("failed to uninstall collections")
+	ErrCollectionAlreadyExists = errors.New("failed to uninstall collections")
 )
 
 type CollectionsOption func(c *CollectionConfig)
@@ -81,9 +82,14 @@ func UpdateExistingCollection() CollectionsOption {
 	}
 }
 
-func SetSchemaMetadata(md map[string]string) CollectionsOption {
+func SetSchemaMetadata(kv ...string) CollectionsOption {
 	return func(c *CollectionConfig) {
-		c.metadata = md
+		if c.metadata == nil {
+			c.metadata = make(map[string]string)
+		}
+		for i := 0; i < len(kv); i += 2 {
+			c.metadata[kv[i]] = kv[i+1]
+		}
 	}
 }
 
@@ -152,7 +158,7 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 		}
 	} else {
 		if collection.Schema.Metadata != nil && exist.Schema.Metadata["extension"] != collection.Schema.Metadata["extension"] && !s.IsForce() {
-			return false, errors.New("collection already exists")
+			return false, ErrCollectionAlreadyExists
 		}
 
 		var upd bool
diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index 027c13b9..a3a6bafa 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -107,7 +107,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"}),
+			co: SetSchemaMetadata("extension", "test-extension"),
 		},
 		{
 			name:        "Fail to update collection with the same id",
@@ -118,7 +118,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"}),
+			co: SetSchemaMetadata("extension", "test-extension"),
 		},
 		{
 			name:        "Update collection with the same id, with force",
@@ -134,7 +134,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"}),
+			co:    SetSchemaMetadata("extension", "test-extension"),
 			force: true,
 		},
 	}
-- 
GitLab