Skip to content
Snippets Groups Projects
Commit 998f3e84 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

fix(extensions): Исправлена ошибка, при которой при обновлении расширений для...

fix(extensions): Исправлена ошибка, при которой при обновлении расширений для некоторых коллекций всегда переустанавливались схемы, в которых не было изменений. Добавлен возврат ошибки при любом действии с расширением в случае неправильной его конфигурации

Close #PRXS-1695
parents 606406be 2fb5e6f2
No related branches found
No related tags found
No related merge requests found
......@@ -133,22 +133,38 @@ func (s *Extension) setupExtensionClient(set *setup.Setup, spaceID string) {
set.AddClient(&client, setup.OverwriteClient(), setup.DeleteClientIfRemove())
}
func (s *Extension) GetSetup(spaceID, envID string) *setup.Setup {
func (s *Extension) GetSetup(spaceID, envID string) (*setup.Setup, error) {
set := s.setupFunc(spaceID, envID)
if set.HasErrors() {
s.Logger.Error("Invalid setup config", zap.Errors("Errors", set.Errors()))
return nil, set.Error()
}
s.setupExtensionClient(set, spaceID)
return set
return set, nil
}
func (s *Extension) Install(ctx context.Context, in *extension.InstallRequest) error {
return s.GetSetup(in.SpaceId, in.EnvId).WithForce(in.Force).Install(ctx)
set, err := s.GetSetup(in.SpaceId, in.EnvId)
if err != nil {
return err
}
return set.WithForce(in.Force).Install(ctx)
}
func (s *Extension) Check(ctx context.Context, in *extension.CheckRequest) error {
return s.GetSetup(in.SpaceId, in.EnvId).Check(ctx)
set, err := s.GetSetup(in.SpaceId, in.EnvId)
if err != nil {
return err
}
return set.Check(ctx)
}
func (s *Extension) Uninstall(ctx context.Context, in *extension.UninstallRequest) error {
return s.GetSetup(in.SpaceId, in.EnvId).WithForce(in.Force).WithRemove(in.Remove).Uninstall(ctx)
set, err := s.GetSetup(in.SpaceId, in.EnvId)
if err != nil {
return err
}
return set.WithForce(in.Force).WithRemove(in.Remove).Uninstall(ctx)
}
// isCorrectExtension проверяет что расширение в url совпадает с расширением расширения
......
......@@ -71,6 +71,15 @@ func (s Schema) SetMetadata(md map[string]string) *Schema {
return &s
}
func (s *Schema) ConvertTypes() error {
b, err := s.MarshalJSON()
if err != nil {
return errors.Wrap(err, "marshal schema")
}
*s = *New()
return errors.Wrap(s.UnmarshalJSON(b), "unmarshal schema")
}
func (s *Schema) Load(ctx context.Context) error {
if s.Loaded {
return nil
......
......@@ -27,8 +27,19 @@ type CollectionConfig struct {
SkipMigration bool
}
func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsOption) CollectionConfig {
c := CollectionConfig{collection: collection}
func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsOption) (c CollectionConfig, err error) {
collection = collection.Clone()
if collection.Schema != nil {
// приведение внутренних типов схемы, чтобы избежать возможного несоответствия типов при
// сравнивании схем (`[]interface{}/[]string`, `int/int64`, etc.)
err = collection.Schema.ConvertTypes()
if err != nil {
return
}
}
c = CollectionConfig{collection: collection}
DefaultUpdateCollectionStrategy()(&c)
DeleteCollectionIfRemove()(&c)
......@@ -37,7 +48,7 @@ func NewCollectionConfig(collection *collections.Collection, opt ...CollectionsO
o(&c)
}
return c
return c, nil
}
func SkipMigration() CollectionsOption {
......
......@@ -2,6 +2,7 @@ package setup
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/clients"
"git.perx.ru/perxis/perxis-go/pkg/collections"
......@@ -11,6 +12,10 @@ import (
"go.uber.org/zap"
)
var (
ErrInvalidSetupConfig = errors.New("invalid setup config")
)
// Setup реализует процесс настройки пространства. Указав необходимые требования к конфигурации пространства можно
// выполнить процесс установки, проверки и удаления требований.
type Setup struct {
......@@ -32,12 +37,12 @@ type Setup struct {
}
func NewSetup(content *content.Content, spaceID, environmentID string, logger *zap.Logger) *Setup {
//logger = logger.With(zap.String("Space", spaceID), zap.String("Environment", environmentID))
if logger == nil {
logger = zap.NewNop()
}
logger = logger.With(zap.String("Space", spaceID), zap.String("Environment", environmentID))
return &Setup{
SpaceID: spaceID,
EnvironmentID: environmentID,
......@@ -74,6 +79,14 @@ func (s *Setup) AddError(err error) {
s.errors = append(s.errors, err)
}
func (s *Setup) Errors() []error {
return s.errors
}
func (s *Setup) Error() error {
return errors.WithErrors(ErrInvalidSetupConfig, s.errors...)
}
// AddRoles добавляет требования к настройке ролей в пространстве
func (s *Setup) AddRoles(roles []*roles.Role, opt ...RolesOption) *Setup {
for _, role := range roles {
......@@ -109,7 +122,12 @@ func (s *Setup) AddCollections(collections []*collections.Collection, opt ...Col
}
func (s *Setup) AddCollection(collection *collections.Collection, opt ...CollectionsOption) *Setup {
s.Collections = append(s.Collections, NewCollectionConfig(collection, opt...))
config, err := NewCollectionConfig(collection, opt...)
if err != nil {
s.AddError(err)
return s
}
s.Collections = append(s.Collections, config)
return s
}
......@@ -128,8 +146,6 @@ func (s *Setup) AddItem(item *items.Item, opt ...ItemsOption) *Setup {
// Install выполняет установку необходимых требований
func (s *Setup) Install(ctx context.Context) error {
s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID))
if err := s.InstallRoles(ctx); err != nil {
return err
}
......@@ -142,14 +158,11 @@ func (s *Setup) Install(ctx context.Context) error {
if err := s.InstallItems(ctx); err != nil {
return err
}
return nil
}
// Check выполняет проверку требований
func (s *Setup) Check(ctx context.Context) error {
s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID))
if err := s.CheckRoles(ctx); err != nil {
return err
}
......@@ -162,14 +175,11 @@ func (s *Setup) Check(ctx context.Context) error {
if err := s.CheckItems(ctx); err != nil {
return err
}
return nil
}
// Uninstall выполняет удаление установленных раннее требований
func (s *Setup) Uninstall(ctx context.Context) error {
s.logger = s.logger.With(zap.String("Space", s.SpaceID), zap.String("Environment", s.EnvironmentID))
// В случае если необходимо удалить данные удаляем все что создано при установке расширения
if err := s.UninstallClients(ctx); err != nil {
return err
......@@ -183,6 +193,5 @@ func (s *Setup) Uninstall(ctx context.Context) error {
if err := s.UninstallItems(ctx); err != nil {
return err
}
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment