From fb8765ff394d1dd54a187047189ba96af97850c0 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sun, 21 Aug 2022 16:41:38 +0200 Subject: [PATCH] Improved default-connection handlong on module reload --- compose/service/module.go | 27 ++++++++++++++++++++++----- pkg/dal/service.go | 9 +++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/compose/service/module.go b/compose/service/module.go index 7334a7cb0..de7ce4443 100644 --- a/compose/service/module.go +++ b/compose/service/module.go @@ -1090,6 +1090,7 @@ func DalModelReload(ctx context.Context, s store.Storer, dmm dalModelManager) (e if err != nil { return } + err = loadModuleFields(ctx, s, mm...) if err != nil { return err @@ -1187,16 +1188,23 @@ func modulesToModelSet(dmm dalModelManager, ns *types.Namespace, mm ...*types.Mo // namespace partition replacement pairs // {{namespace}} is replaced with the namespace handle (slug) nsPartition = []string{"{{namespace}}", ns.Slug} + + defConnID uint64 + defConn = dmm.GetConnectionByID(0) ) - for connectionID, modules := range modulesByConnection(mm...) { + if defConn != nil { + defConnID = defConn.ID + } + + for connectionID, modules := range modulesByConnection(defConnID, mm...) { // Get the connection meta conn = dmm.GetConnectionByID(connectionID) // Convert all modules to models for _, mod := range modules { if conn == nil { - // construct a simplified model w/o attributes, connction + // construct a simplified model w/o attributes, connection // this will allow us to manage model's issues within // the DAL service model = &dal.Model{ @@ -1452,10 +1460,19 @@ func moduleFieldToAttribute(f *types.ModuleField, conn *dal.ConnectionWrap) (out } // modulesByConnection groups given modules by the common connectionID -func modulesByConnection(modules ...*types.Module) map[uint64]types.ModuleSet { - out := make(map[uint64]types.ModuleSet) +func modulesByConnection(defConnID uint64, modules ...*types.Module) map[uint64]types.ModuleSet { + var ( + id uint64 + out = make(map[uint64]types.ModuleSet) + ) for _, mod := range modules { - out[mod.Config.DAL.ConnectionID] = append(out[mod.Config.DAL.ConnectionID], mod) + if id = mod.Config.DAL.ConnectionID; id == 0 { + // connection not explicitly set on module + // use default + id = defConnID + } + + out[id] = append(out[id], mod) } return out diff --git a/pkg/dal/service.go b/pkg/dal/service.go index f73a87ab1..9da678632 100644 --- a/pkg/dal/service.go +++ b/pkg/dal/service.go @@ -575,19 +575,20 @@ func (svc *service) ReplaceModel(ctx context.Context, model *Model) (err error) return nil } - log.Debug("adding to connection") auxIssues, err = svc.registerModelToConnection(ctx, connection, model) issues.mergeWith(auxIssues) if err != nil { log.Error("failed with errors", zap.Error(err)) return } - log.Debug("added to connection") } // Add to registry svc.addModelToRegistry(model, upd) - log.Debug("added") + log.Debug( + "added", + zap.Uint64("connectionID", model.ConnectionID), + ) return } @@ -851,7 +852,7 @@ func (svc *service) getConnection(connectionID uint64, cc ...Operation) (cw *Con // get the requested connection cw = svc.GetConnectionByID(connectionID) if cw == nil { - return fmt.Errorf("connection %d does not exist", connectionID) + return errConnectionNotFound(connectionID) } // check if connection supports requested operations