From b83b003c1703826ab292770397a925394a9c13b9 Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Mon, 3 Jul 2023 13:45:09 +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=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20WithMetaData=20=D0=B8=20SetMetadata=20-=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B=20-=20=D0=B2=D1=8B=D0=B4=D0=B5=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=20che?= =?UTF-8?q?ckSchemaMetadata()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/schema/schema.go | 15 +++++++++++++++ pkg/schema/test/object_test.go | 2 +- pkg/setup/collection.go | 16 ++++++++++++---- pkg/setup/collection_test.go | 10 +++++----- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index ab9b7e95..bac95d05 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -45,6 +45,21 @@ func (s Schema) WithIncludes(includes ...interface{}) *Schema { return &s } +func (s *Schema) WithMetadata(kv ...string) *Schema { + if s.Metadata == nil { + s.Metadata = make(map[string]string, len(s.Metadata)) + } + for i := 0; i < len(kv); i += 2 { + s.Metadata[kv[i]] = kv[i+1] + } + return s +} + +func (s Schema) SetMetadata(md map[string]string) *Schema { + s.Metadata = md + return &s +} + func (s *Schema) Load(ctx context.Context) error { if s.Loaded { return nil diff --git a/pkg/schema/test/object_test.go b/pkg/schema/test/object_test.go index 21330f9f..9ba61879 100644 --- a/pkg/schema/test/object_test.go +++ b/pkg/schema/test/object_test.go @@ -111,7 +111,7 @@ func TestSchema_JSON(t *testing.T) { ) sch.Loaded = true sch.Metadata = map[string]string{ - "extension": "perxisweb", + "extension": "test-extension", } b, err := json.MarshalIndent(sch, "", " ") diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 4b339d55..8d719137 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -140,10 +140,9 @@ 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") - } + err = s.checkSchemaMetadata(collection, exist) + if err != nil { + return false, err } collection, upd, setSchema = c.UpdateFn(s, exist, c.collection) @@ -164,6 +163,15 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS return setSchema, nil } +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") + } + } + return nil +} + func (s *Setup) CheckCollections(ctx context.Context) error { if len(s.Collections) == 0 { return nil diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index a6a3b5e0..bd90346f 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -93,10 +93,10 @@ func TestSetup_InstallCollections(t *testing.T) { }, { 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"}}}}, + collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}}, 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("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}, nil).Once() + svc.On("Update", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}).Return(nil).Once() svc.On("SetSchema", mock.Anything, "sp", "env", "1", schema.New("name", field.String())).Return(nil).Once() }, envsCall: func(svc *envmocks.Environments) { @@ -108,9 +108,9 @@ func TestSetup_InstallCollections(t *testing.T) { }, { 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"}}}}, + collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}}, 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() + svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("surname", field.String())}, nil).Once() }, wantErr: func(t *testing.T, err error) { assert.Error(t, err) -- GitLab