From eebb8025afd9b341e78c980d1999cf96fbf08ddc Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Tue, 11 Jul 2023 12:30:15 +0300
Subject: [PATCH] =?UTF-8?q?-=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81?=
 =?UTF-8?q?=20func=20UpdateCollectionSchemaMetadata()=20=D0=B2=20=D0=BF?=
 =?UTF-8?q?=D0=B0=D0=BA=D0=B5=D1=82=20setup=20-=20=D0=BF=D1=80=D0=B0=D0=B2?=
 =?UTF-8?q?=D0=BA=D0=B8=20=D0=B2=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8?=
 =?UTF-8?q?=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20UpdateCol?=
 =?UTF-8?q?lectionSchemaMetadata()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/extension/extension.go | 18 ------------------
 pkg/setup/collection.go    | 27 ++++++++++++++++++++++-----
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go
index 5e31c700..fb8044c7 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 f80e0827..4cb6b094 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)
 		}
 	}
 }
-- 
GitLab