From 50550a83dadbeeb4ddba7162d060e2a862dc2848 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Thu, 29 Sep 2022 15:15:38 +0200 Subject: [PATCH] Fix RDBMS upgrade procedures --- store/adapters/rdbms/upgrade.go | 24 ++++++++++++++++++++++++ store/adapters/rdbms/upgrade_fixes.go | 20 ++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/store/adapters/rdbms/upgrade.go b/store/adapters/rdbms/upgrade.go index c6c1ac424..d5232f6d5 100644 --- a/store/adapters/rdbms/upgrade.go +++ b/store/adapters/rdbms/upgrade.go @@ -82,9 +82,17 @@ func createTablesFromModels(ctx context.Context, log *zap.Logger, dd ddl.DataDef return nil } +// addColumn adds column on a table but only if table exists! +// +// If table does not exist adding column can be skipped +// We can assume that 2nd step of the upgrade process will include the column func addColumn(ctx context.Context, s *Store, table string, attr *dal.Attribute) error { tbl, err := s.DataDefiner.TableLookup(ctx, table) if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err } @@ -102,9 +110,17 @@ func addColumn(ctx context.Context, s *Store, table string, attr *dal.Attribute) return s.DataDefiner.ColumnAdd(ctx, table, col) } +// dropColumns removes columns from a table but only if table exists! +// +// If table does not exist column removing can be skipped +// We can assume that 2nd step of the upgrade process will omit the column func dropColumns(ctx context.Context, s *Store, table string, cc ...string) error { tbl, err := s.DataDefiner.TableLookup(ctx, table) if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err } @@ -122,9 +138,17 @@ func dropColumns(ctx context.Context, s *Store, table string, cc ...string) erro return nil } +// renameColumn renames columns from a table but only if table exists! +// +// If table does not exist column renaming can be skipped +// We can assume that 2nd step of the upgrade process will have columns properly nameed func renameColumn(ctx context.Context, s *Store, table string, from, to string) error { tbl, err := s.DataDefiner.TableLookup(ctx, table) if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err } diff --git a/store/adapters/rdbms/upgrade_fixes.go b/store/adapters/rdbms/upgrade_fixes.go index e55880a51..912bc18c5 100644 --- a/store/adapters/rdbms/upgrade_fixes.go +++ b/store/adapters/rdbms/upgrade_fixes.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/cortezaproject/corteza-server/pkg/dal" + "github.com/cortezaproject/corteza-server/pkg/errors" labelsType "github.com/cortezaproject/corteza-server/pkg/label/types" "github.com/cortezaproject/corteza-server/store" "go.uber.org/zap" @@ -26,6 +27,7 @@ var ( fix_2022_09_00_extendDalConnectionsForMeta, fix_2022_09_00_renameModuleColOnComposeRecords, fix_2022_09_00_addMetaOnComposeRecords, + fix_2022_09_00_addMissingNodeIdOnFederationMapping, } ) @@ -38,7 +40,7 @@ func fix_2022_09_00_extendComposeModuleForPrivacyAndDAL(ctx context.Context, s * func fix_2022_09_00_extendComposeModuleFieldsForPrivacyAndDAL(ctx context.Context, s *Store) (err error) { return addColumn(ctx, s, - "compose_module", + "compose_module_field", &dal.Attribute{Ident: "config", Type: &dal.TypeJSON{DefaultValue: "{}"}}, ) } @@ -70,6 +72,14 @@ func fix_2022_09_00_addMetaOnComposeRecords(ctx context.Context, s *Store) (err packed []byte ) + _, err = s.DataDefiner.TableLookup(ctx, "labels") + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + err = addColumn(ctx, s, "compose_record", &dal.Attribute{Ident: "meta", Type: &dal.TypeJSON{DefaultValue: "{}"}}, @@ -110,5 +120,11 @@ func fix_2022_09_00_addMetaOnComposeRecords(ctx context.Context, s *Store) (err return }) - +} + +func fix_2022_09_00_addMissingNodeIdOnFederationMapping(ctx context.Context, s *Store) (err error) { + return addColumn(ctx, s, + "federation_module_mapping", + &dal.Attribute{Ident: "node_id", Type: &dal.TypeID{}}, + ) }