From 09cf1099a5186d07651c8ebff3bd13b69e13b8ed Mon Sep 17 00:00:00 2001 From: revolyssup Date: Tue, 25 Oct 2022 20:32:07 +0530 Subject: [PATCH 1/3] Add methods for lifecycle management of database instance Signed-off-by: revolyssup --- database/database.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/database/database.go b/database/database.go index 794a43a0..78f8fb80 100644 --- a/database/database.go +++ b/database/database.go @@ -35,9 +35,37 @@ type Model struct { type Handler struct { *gorm.DB *sync.Mutex + options Options // Implement methods if necessary } +func (h *Handler) GetInfo() Options { + return h.options +} + +// ChangeDatabase takes new set of options and creates a new database instance, attaching it to the database handler. +// Make sure to Migrate tables after switching the database, whenever this function is called. +func (h *Handler) ChangeDatabase(opts Options) error { + h.Lock() + defer h.Unlock() + err := h.DBClose() + if err != nil { + return err + } + opts.Logger = h.options.Logger + if opts.Engine == "" { + opts.Engine = h.options.Engine + } + + newHandler, err := New(opts) + if err != nil { + return err + } + h.DB = newHandler.DB + h.options = newHandler.options + h.options.Logger.Info("Database switched") + return nil +} func (h *Handler) DBClose() error { db, err := h.DB.DB() if err != nil { @@ -60,6 +88,7 @@ func New(opts Options) (Handler, error) { return Handler{ db, &sync.Mutex{}, + opts, }, nil case SQLITE: config := &gorm.Config{} @@ -75,6 +104,7 @@ func New(opts Options) (Handler, error) { return Handler{ db, &sync.Mutex{}, + opts, }, nil } From b217bf4fb0e273958d0bbdc8a74140507b43d608 Mon Sep 17 00:00:00 2001 From: revolyssup Date: Sat, 29 Oct 2022 00:30:43 +0530 Subject: [PATCH 2/3] Add Database compliant Meshmodel structs Signed-off-by: revolyssup --- models/meshmodel/core/v1alpha1/component.go | 23 ++++++--- .../v1alpha1/component_capabilities_db.go | 51 +++++++++++++++++++ utils/component/generator_test.go | 1 + 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 models/meshmodel/core/v1alpha1/component_capabilities_db.go diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index 7bcea0a1..b162ebc1 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -3,26 +3,37 @@ package v1alpha1 const ComponentDefinitionKindKey = "ComponentDefinition" type TypeMeta struct { - Kind string `json:"kind,omitempty"` - APIVersion string `json:"apiVersion,omitempty"` + Kind string `json:"kind,omitempty" gorm:"kind"` + APIVersion string `json:"apiVersion,omitempty" gorm:"apiVersion"` } // use NewComponent function for instantiating type Component struct { - TypeMeta - ComponentSpec - Metadata map[string]interface{} `json:"metadata,omitempty"` + TypeMeta `gorm:"embedded"` + ComponentSpec `gorm:"embedded"` + Metadata map[string]interface{} `json:"metadata,omitempty" gorm:"type:JSONB"` // for backward compatibility Spec string `json:"spec,omitempty"` } type ComponentSpec struct { - Schematic map[string]interface{} `json:"schematic,omitempty"` + Schematic map[string]interface{} `json:"schematic,omitempty" gorm:"type:JSONB"` +} + +type ComponentCapability struct { + Component + capability +} +type capability struct { + ID string `json:"id,omitempty"` + // Host is the address of the service registering the capability + Host string `json:"host,omitempty"` } func NewComponent() Component { comp := Component{} comp.APIVersion = "core.meshery.io/v1alpha1" comp.Kind = ComponentDefinitionKindKey + comp.Metadata = make(map[string]interface{}, 1) return comp } diff --git a/models/meshmodel/core/v1alpha1/component_capabilities_db.go b/models/meshmodel/core/v1alpha1/component_capabilities_db.go new file mode 100644 index 00000000..1ae2543c --- /dev/null +++ b/models/meshmodel/core/v1alpha1/component_capabilities_db.go @@ -0,0 +1,51 @@ +package v1alpha1 + +import ( + "encoding/json" + + "github.com/google/uuid" +) + +// This file consists of methods and structs that database(gorm) will use to interact with meshmodel components +type ComponentDB struct { + TypeMeta + ComponentSpecDB + Metadata []byte `json:"metadata"` + // for backward compatibility + Spec string `json:"spec,omitempty"` +} + +type ComponentSpecDB struct { + Schematic []byte `json:"schematic,omitempty"` +} + +type ComponentCapabilityDB struct { + ID uuid.UUID `json:"id,omitempty"` + ComponentDB + capability +} +type capabilityDB struct { + // Host is the address of the service registering the capability + Host string `json:"host,omitempty"` +} + +func ComponentCapabilityFromCCDB(cdb ComponentCapabilityDB) (c ComponentCapability) { + c.capability = cdb.capability + c.TypeMeta = cdb.TypeMeta + c.Spec = cdb.Spec + m := make(map[string]interface{}) + json.Unmarshal(cdb.Metadata, &m) + c.Metadata = m + schematic := make(map[string]interface{}) + json.Unmarshal(cdb.Schematic, &schematic) + c.Schematic = schematic + return +} +func ComponentCapabilityDBFromCC(c ComponentCapability) (cdb ComponentCapabilityDB) { + cdb.capability = c.capability + cdb.TypeMeta = c.TypeMeta + cdb.Spec = c.Spec + cdb.Metadata, _ = json.Marshal(c.Metadata) + cdb.Schematic, _ = json.Marshal(c.Schematic) + return +} diff --git a/utils/component/generator_test.go b/utils/component/generator_test.go index d1f38739..80a4a2e4 100644 --- a/utils/component/generator_test.go +++ b/utils/component/generator_test.go @@ -130,6 +130,7 @@ func getNewComponent(spec string, name string) v1alpha1.Component { meta := map[string]interface{}{ ComponentMetaNameKey: name, } + comp.Metadata = meta return comp } From fbe99142b6be51f5dab4fedfc5806c5b39057eb4 Mon Sep 17 00:00:00 2001 From: revolyssup Date: Fri, 4 Nov 2022 23:49:12 +0530 Subject: [PATCH 3/3] remove redundant change Signed-off-by: revolyssup --- models/meshmodel/core/v1alpha1/component.go | 34 +++++++++---------- .../v1alpha1/component_capabilities_db.go | 21 ++++++------ 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index b162ebc1..2f4f5836 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -3,31 +3,24 @@ package v1alpha1 const ComponentDefinitionKindKey = "ComponentDefinition" type TypeMeta struct { - Kind string `json:"kind,omitempty" gorm:"kind"` - APIVersion string `json:"apiVersion,omitempty" gorm:"apiVersion"` + Kind string `json:"kind,omitempty" yaml:"kind"` + APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion"` } // use NewComponent function for instantiating type Component struct { - TypeMeta `gorm:"embedded"` - ComponentSpec `gorm:"embedded"` - Metadata map[string]interface{} `json:"metadata,omitempty" gorm:"type:JSONB"` + TypeMeta `gorm:"embedded" yaml:"typemeta"` + ComponentSpec `gorm:"embedded" yaml:"componentspec"` + Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata"` // for backward compatibility - Spec string `json:"spec,omitempty"` + Spec string `json:"spec,omitempty" yaml:"spec"` } - -type ComponentSpec struct { - Schematic map[string]interface{} `json:"schematic,omitempty" gorm:"type:JSONB"` -} - -type ComponentCapability struct { - Component - capability -} -type capability struct { - ID string `json:"id,omitempty"` +type Capability struct { // Host is the address of the service registering the capability - Host string `json:"host,omitempty"` + Host string `json:"host,omitempty" yaml:"host"` +} +type ComponentSpec struct { + Schematic map[string]interface{} `json:"schematic,omitempty" yaml:"schematic"` } func NewComponent() Component { @@ -37,3 +30,8 @@ func NewComponent() Component { comp.Metadata = make(map[string]interface{}, 1) return comp } + +type ComponentCapability struct { + Component `yaml:"component"` + Capability `yaml:"capability"` +} diff --git a/models/meshmodel/core/v1alpha1/component_capabilities_db.go b/models/meshmodel/core/v1alpha1/component_capabilities_db.go index 1ae2543c..fb407cd6 100644 --- a/models/meshmodel/core/v1alpha1/component_capabilities_db.go +++ b/models/meshmodel/core/v1alpha1/component_capabilities_db.go @@ -6,7 +6,7 @@ import ( "github.com/google/uuid" ) -// This file consists of methods and structs that database(gorm) will use to interact with meshmodel components +// This file consists of helper methods and structs that database(gorm) will use to interact with meshmodel components type ComponentDB struct { TypeMeta ComponentSpecDB @@ -22,27 +22,28 @@ type ComponentSpecDB struct { type ComponentCapabilityDB struct { ID uuid.UUID `json:"id,omitempty"` ComponentDB - capability -} -type capabilityDB struct { - // Host is the address of the service registering the capability - Host string `json:"host,omitempty"` + Capability } +// ComponentCapabilityFromCCDB produces a client facing instance of ComponentCapability from a database representation of ComponentCapability. +// Use this function to interconvert any time the ComponentCapability is fetched from the database and is to be returned to client. func ComponentCapabilityFromCCDB(cdb ComponentCapabilityDB) (c ComponentCapability) { - c.capability = cdb.capability + c.Capability = cdb.Capability c.TypeMeta = cdb.TypeMeta c.Spec = cdb.Spec m := make(map[string]interface{}) - json.Unmarshal(cdb.Metadata, &m) + _ = json.Unmarshal(cdb.Metadata, &m) c.Metadata = m schematic := make(map[string]interface{}) - json.Unmarshal(cdb.Schematic, &schematic) + _ = json.Unmarshal(cdb.Schematic, &schematic) c.Schematic = schematic return } + +// ComponentCapabilityDBFromCC produces a database compatible instance of ComponentCapability from a client representation of ComponentCapability. +// Use this function to interconvert any time the ComponentCapability is created by some client and is to be saved to the database. func ComponentCapabilityDBFromCC(c ComponentCapability) (cdb ComponentCapabilityDB) { - cdb.capability = c.capability + cdb.Capability = c.Capability cdb.TypeMeta = c.TypeMeta cdb.Spec = c.Spec cdb.Metadata, _ = json.Marshal(c.Metadata)