diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index ab9b7e952d3720f8c7065a2bb7d7e74b1c91927f..bac95d05de2e863e5540c02f8553e431b2f602c7 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 21330f9f49ebfe5a438fc2eef9cb6a02657e95ff..9ba618794f122ed8c5551324b02d6b8cf9e46829 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 4b339d5542e24c604f4ed9fe0c726c5d1cdafb49..8d719137f4ab1ee9c4fa06d88880389fd8360efe 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 a6a3b5e0738413f8bec8c788d5036c65b393bc40..bd90346ff0c8bf6cbc5db52e291702b134f10b9d 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)