From 4e33d7688582f1146ad3b0ae830498d7c622faff Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Sat, 1 Jul 2023 18:23:14 +0300
Subject: [PATCH] =?UTF-8?q?-=20=D0=BF=D0=BE=D0=B4=D0=BD=D1=8F=D1=82=D0=B0?=
 =?UTF-8?q?=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0?=
 =?UTF-8?q?=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20-=20=D0=BE=D0=B1=D0=BD?=
 =?UTF-8?q?=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8?=
 =?UTF-8?q?=D0=BA=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20Ins?=
 =?UTF-8?q?tallCollection=20=D1=81=20=D1=83=D1=87=D0=B5=D1=82=D0=BE=D0=BC?=
 =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?=
 =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=8F=20Metadata=20-=20=D0=B4=D0=BE=D0=B1?=
 =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/content/content.go       |  2 +-
 pkg/setup/collection.go      |  7 +++++++
 pkg/setup/collection_test.go | 25 +++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/pkg/content/content.go b/pkg/content/content.go
index 1d351e93..29abfb45 100644
--- a/pkg/content/content.go
+++ b/pkg/content/content.go
@@ -39,7 +39,7 @@ type Content struct {
 }
 
 const (
-	DBVersion uint32 = 2
+	DBVersion uint32 = 3
 )
 
 func (c *Content) RegisterStart(svc interface{}) {
diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index d61ba14d..4b339d55 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -139,6 +139,13 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
 		}
 	} else {
 		var upd bool
+
+		if _, ok := collection.Schema.Metadata["extension"]; ok {
+			if _, ok := exist.Schema.Metadata["extension"]; !ok {
+				return false, errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
+			}
+		}
+
 		collection, upd, setSchema = c.UpdateFn(s, exist, c.collection)
 		if upd {
 			if err = s.content.Collections.Update(ctx, collection); err != nil {
diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index 433aa739..a6a3b5e0 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -91,6 +91,31 @@ func TestSetup_InstallCollections(t *testing.T) {
 				assert.Contains(t, errors.GetDetail(err), "Возникла ошибка при настройке коллекции space(1)")
 			},
 		},
+		{
+			name:        "Update extension collection with metadata",
+			collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}},
+			collectionsCall: func(svc *mockscollections.Collections) {
+				svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}, nil).Once()
+				svc.On("Update", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}).Return(nil).Once()
+				svc.On("SetSchema", mock.Anything, "sp", "env", "1", schema.New("name", field.String())).Return(nil).Once()
+			},
+			envsCall: func(svc *envmocks.Environments) {
+				svc.On("Migrate", mock.Anything, "sp", "env", &environments.MigrateOptions{Wait: true}).Return(nil).Once()
+			},
+			wantErr: func(t *testing.T, err error) {
+				assert.NoError(t, err)
+			},
+		},
+		{
+			name:        "Fail to update collection with the same id",
+			collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}},
+			collectionsCall: func(svc *mockscollections.Collections) {
+				svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("surname", field.String()).Field}}, nil).Once()
+			},
+			wantErr: func(t *testing.T, err error) {
+				assert.Error(t, err)
+			},
+		},
 	}
 
 	for _, tt := range tests {
-- 
GitLab