Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for lifecycle management of database instance #226

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -60,6 +88,7 @@ func New(opts Options) (Handler, error) {
return Handler{
db,
&sync.Mutex{},
opts,
}, nil
case SQLITE:
config := &gorm.Config{}
Expand All @@ -75,6 +104,7 @@ func New(opts Options) (Handler, error) {
return Handler{
db,
&sync.Mutex{},
opts,
}, nil
}

Expand Down
25 changes: 17 additions & 8 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ package v1alpha1
const ComponentDefinitionKindKey = "ComponentDefinition"

type TypeMeta struct {
Kind string `json:"kind,omitempty"`
APIVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion"`
}

// use NewComponent function for instantiating
type Component struct {
TypeMeta
ComponentSpec
Metadata map[string]interface{} `json:"metadata,omitempty"`
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 Capability struct {
// Host is the address of the service registering the capability
Host string `json:"host,omitempty" yaml:"host"`
}

type ComponentSpec struct {
Schematic map[string]interface{} `json:"schematic,omitempty"`
Schematic map[string]interface{} `json:"schematic,omitempty" yaml:"schematic"`
}

func NewComponent() Component {
comp := Component{}
comp.APIVersion = "core.meshery.io/v1alpha1"
comp.Kind = ComponentDefinitionKindKey
comp.Metadata = make(map[string]interface{}, 1)
return comp
}

type ComponentCapability struct {
Component `yaml:"component"`
Capability `yaml:"capability"`
}
52 changes: 52 additions & 0 deletions models/meshmodel/core/v1alpha1/component_capabilities_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v1alpha1

import (
"encoding/json"

"github.com/google/uuid"
)

// This file consists of helper 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
}

// 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.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
}

// 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.TypeMeta = c.TypeMeta
cdb.Spec = c.Spec
cdb.Metadata, _ = json.Marshal(c.Metadata)
cdb.Schematic, _ = json.Marshal(c.Schematic)
return
}
1 change: 1 addition & 0 deletions utils/component/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func getNewComponent(spec string, name string) v1alpha1.Component {
meta := map[string]interface{}{
ComponentMetaNameKey: name,
}

comp.Metadata = meta
return comp
}
Expand Down