From 6d7fc0fd953b66b63ed7b4696bdff6e79e9585e2 Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Mon, 10 Jul 2023 21:18:08 +0300
Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20Upd?=
 =?UTF-8?q?ateCollectionSchemaMetadata()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection.go | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 17011372..4dca0192 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -80,6 +80,22 @@ func UpdateExistingCollection() CollectionsOption {
 	}
 }
 
+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 && 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
+		}
+	}
+}
+
 func (s *Setup) InstallCollections(ctx context.Context) (err error) {
 	if len(s.Collections) == 0 {
 		return nil
@@ -133,13 +149,16 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 
 	if exist == nil {
 		setSchema = !collection.IsView()
-		exist, err = s.content.Collections.Create(ctx, collection)
+		_, err = s.content.Collections.Create(ctx, collection)
 		if err != nil {
 			return false, err
 		}
 	} else {
 		var upd bool
 		collection, upd, setSchema, err = c.UpdateFn(s, exist, c.collection)
+		if err != nil {
+			return false, err
+		}
 		if upd {
 			if err = s.content.Collections.Update(ctx, collection); err != nil {
 				return false, err
-- 
GitLab