diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 2d3656eaebd339dd9e064cef57e2ff6e501b1e2b..48598be284453f3bf4ab67e5c8d10be6e5587968 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -7,6 +7,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/data" "git.perx.ru/perxis/perxis-go/pkg/errors" + "git.perx.ru/perxis/perxis-go/pkg/items" "go.uber.org/zap" ) @@ -260,7 +261,12 @@ func (s *Setup) UninstallCollection(ctx context.Context, c CollectionConfig) err if err = s.content.Collections.Delete(ctx, s.SpaceID, s.EnvironmentID, c.collection.ID); err != nil && !strings.Contains(err.Error(), collections.ErrNotFound.Error()) { return err } - s.removeItems(c.collection.ID) // после удаления коллекции нет смысла удалять ее элементы + + // после удаления коллекции нет смысла удалять ее элементы + s.Config.WithItemsOptions( + func(i *items.Item) bool { return i.CollectionID == c.collection.ID }, + SkipItemDeletion(), + ) } return nil } diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index 788314ca8aa15aa9e4bba63ae6c090f0e5662e69..c78263d153407a38c46d6ee14ef3f2459925c680 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -10,6 +10,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/environments" envmocks "git.perx.ru/perxis/perxis-go/pkg/environments/mocks" "git.perx.ru/perxis/perxis-go/pkg/errors" + "git.perx.ru/perxis/perxis-go/pkg/items" "git.perx.ru/perxis/perxis-go/pkg/schema" "git.perx.ru/perxis/perxis-go/pkg/schema/field" "github.com/stretchr/testify/assert" @@ -217,3 +218,44 @@ func TestOverwriteCollection(t *testing.T) { }) } } + +func TestUninstallCollection(t *testing.T) { + t.Run("DeleteFn error", func(t *testing.T) { + setup := NewSetup(nil, "sp", "env", nil) + err := setup.UninstallCollection(context.Background(), CollectionConfig{ + DeleteFn: func(_ *Setup, _ *collections.Collection) (bool, error) { return false, errors.New("some err") }, + }) + require.EqualError(t, err, "some err", "При ошибке DeleteFn, ошибка должна возвращаться без вызовов сервиса") + }) + t.Run("Collection delete error", func(t *testing.T) { + svc := mockscollections.NewCollections(t) + svc.On("Delete", mock.Anything, "sp", "env", "collID").Return(errors.New("some err")).Once() + + setup := NewSetup(&content.Content{Collections: svc}, "sp", "env", nil) + err := setup.UninstallCollection(context.Background(), CollectionConfig{ + collection: &collections.Collection{ID: "collID"}, + DeleteFn: func(_ *Setup, _ *collections.Collection) (bool, error) { return true, nil }, + }) + + require.EqualError(t, err, "some err", "Ошибка сервиса должна возвращаться") + }) + t.Run("Success delete", func(t *testing.T) { + svc := mockscollections.NewCollections(t) + svc.On("Delete", mock.Anything, "sp", "env", "coll1").Return(nil).Once() + + setup := NewSetup(&content.Content{Collections: svc}, "sp", "env", nil) + setup.Items = []ItemConfig{ + {item: &items.Item{ID: "id1", CollectionID: "coll1"}, DeleteFn: nil}, + {item: &items.Item{ID: "id2", CollectionID: "coll2"}, DeleteFn: nil}, + } + err := setup.UninstallCollection(context.Background(), CollectionConfig{ + collection: &collections.Collection{ID: "coll1"}, + DeleteFn: func(_ *Setup, _ *collections.Collection) (bool, error) { return true, nil }, + }) + + require.NoError(t, err, "Метод удаляет коллекцию без ошибок") + require.Len(t, setup.Items, 2, "Количество записей в конфигурации должно остаться прежним") + require.NotNil(t, setup.Items[0].DeleteFn, "Записи удаленной коллекции получают DeleteFn (SkipItemDeletion)") + require.Nil(t, setup.Items[1].DeleteFn, "Записи других коллекций остаются неизмененными") + }) +} diff --git a/pkg/setup/item.go b/pkg/setup/item.go index af08ea951ad51da0a7cb7057191288d4997faea4..afd7006d99191d6542c8fc631c6127abc9fb1358 100644 --- a/pkg/setup/item.go +++ b/pkg/setup/item.go @@ -139,6 +139,12 @@ func DeleteItemIfRemove() ItemsOption { } } +func SkipItemDeletion() ItemsOption { + return func(c *ItemConfig) { + c.DeleteFn = func(s *Setup, item *items.Item) bool { return false } + } +} + func PublishItem() ItemsOption { return func(c *ItemConfig) { c.PublishFn = func(s *Setup, item *items.Item) (*items.Item, bool) { return item, true } @@ -295,16 +301,6 @@ func (s *Setup) CheckItems(ctx context.Context) error { return nil } -func (s *Setup) removeItems(collID string) { - itms := make([]ItemConfig, 0, len(s.Items)) - for _, i := range s.Items { - if i.item.CollectionID != collID { - itms = append(itms, i) - } - } - s.Items = itms -} - func (s *Setup) groupByCollection() map[string][]ItemConfig { itemsByColl := map[string][]ItemConfig{} for _, i := range s.Items { diff --git a/pkg/setup/item_test.go b/pkg/setup/item_test.go index 2d2e1a7705a80be6d4eb783af5ad81daf431e886..ed9b4be5e68a2c7a8795dc659c937fcad952b759 100644 --- a/pkg/setup/item_test.go +++ b/pkg/setup/item_test.go @@ -363,3 +363,10 @@ func TestSetup_UpdateDraft(t *testing.T) { }) } } + +func TestSetup_SkipItemDeletion(t *testing.T) { + setup := NewSetup(&content.Content{Items: nil}, "", "", nil).WithRemove(true) + setup.Config.AddItem(&items.Item{}, SkipItemDeletion()) + err := setup.UninstallItems(context.Background()) + require.NoError(t, err, "Не должно быть ошибок и вызовов items.Delete для записей с опцией SkipItemDeletion") +}