diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 0a7fc65c9da358240980638e82915089b13e5829..9f5f6a46c7818d54db7e5123cdbef595b0cc0ab9 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -61,9 +61,9 @@ func OverwriteCollection() CollectionsOption { return func(c *CollectionConfig) { c.UpdateFn = func(s *Setup, old, new *collections.Collection) (*collections.Collection, bool, bool, error) { update := new.Name != old.Name || new.IsSingle() != old.IsSingle() || new.IsSystem() != old.IsSystem() || - new.IsNoData() != old.IsNoData() || new.Hidden != old.Hidden || new.IsView() != old.IsView() || !data.ElementsMatch(old.Tags, new.Tags) + new.IsNoData() != old.IsNoData() || new.Hidden != old.Hidden || !new.View.Equal(old.View) || !data.ElementsMatch(old.Tags, new.Tags) - return new, update, old.Schema.Equal(new.Schema), nil + return new, update, !old.Schema.Equal(new.Schema), nil } } } diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index a06c117a3a5586b4a3afc9513bb6ac1333c556cf..43b450119a0892865486517df1d43a7e92d3dc2d 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -14,6 +14,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/schema/field" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestSetup_InstallCollections(t *testing.T) { @@ -156,3 +157,52 @@ func TestSetup_InstallCollections(t *testing.T) { }) } } + +func TestOverwriteCollection(t *testing.T) { + tests := []struct { + name string + old, new *collections.Collection + wantUpdate bool + wantSetSchema bool + wantErr bool + }{ + { + name: "Equal collections should not be updated", + old: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Schema: schema.New("a", field.String())}, + new: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Schema: schema.New("a", field.String())}, + wantUpdate: false, + wantSetSchema: false, + }, + { + name: "For collections with different schemas and equal other params schemas should be set", + old: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll", Schema: schema.New("a", field.String())}, + new: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll", Schema: schema.New("b", field.String())}, + wantUpdate: false, + wantSetSchema: true, + }, + { + name: "Collections with different names should be updated", + old: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll1", Schema: schema.New("a", field.String())}, + new: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll2", Schema: schema.New("a", field.String())}, + wantUpdate: true, + wantSetSchema: false, + }, + { + name: "Collections with different view params should be updated", + old: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", View: &collections.View{SpaceID: "sp1", EnvID: "env1", CollectionID: "coll1"}}, + new: &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", View: &collections.View{SpaceID: "sp2", EnvID: "env2", CollectionID: "coll2"}}, + wantUpdate: true, + wantSetSchema: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := new(CollectionConfig) + OverwriteCollection()(c) + _, update, setSchema, err := c.UpdateFn(new(Setup), tt.old, tt.new) + require.NoError(t, err) + assert.Equal(t, tt.wantUpdate, update) + assert.Equal(t, tt.wantSetSchema, setSchema) + }) + } +}