Skip to content

Commit

Permalink
feat: add tags to make models compatible for mysql
Browse files Browse the repository at this point in the history
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
  • Loading branch information
NeerajGartia21 committed Jun 12, 2023
1 parent 2931e13 commit fe3ade9
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 67 deletions.
44 changes: 22 additions & 22 deletions pkg/server/domain/model/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func init() {
// Application application delivery model
type Application struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Project string `json:"project"`
Description string `json:"description"`
Icon string `json:"icon"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty" gorm:"serializer:json"`
Annotations map[string]string `json:"annotations,omitempty" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down Expand Up @@ -117,27 +117,27 @@ type ComponentSelector struct {
// ApplicationComponent component database model
type ApplicationComponent struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey"`
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Description string `json:"description,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Labels map[string]string `json:"labels,omitempty" gorm:"serializer:json"`
Icon string `json:"icon,omitempty"`
Creator string `json:"creator"`
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Type string `json:"type"`
Main bool `json:"main"`
// ExternalRevision specified the component revisionName
ExternalRevision string `json:"externalRevision,omitempty"`
Properties *JSONStruct `json:"properties,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
Inputs workflowv1alpha1.StepInputs `json:"inputs,omitempty"`
Outputs workflowv1alpha1.StepOutputs `json:"outputs,omitempty"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
DependsOn []string `json:"dependsOn,omitempty" gorm:"serializer:json"`
Inputs workflowv1alpha1.StepInputs `json:"inputs,omitempty" gorm:"serializer:json"`
Outputs workflowv1alpha1.StepOutputs `json:"outputs,omitempty" gorm:"serializer:json"`
// Traits define the trait of one component, the type must be array to keep the order.
Traits []ApplicationTrait `json:"traits,omitempty"`
Traits []ApplicationTrait `json:"traits,omitempty" gorm:"serializer:json"`
// scopes in ApplicationComponent defines the component-level scopes
// the format is <scope-type:scope-instance-name> pairs, the key represents type of `ScopeDefinition` while the value represent the name of scope instance.
Scopes map[string]string `json:"scopes,omitempty"`
WorkloadType common.WorkloadTypeDescriptor `json:"workloadType,omitempty"`
Scopes map[string]string `json:"scopes,omitempty" gorm:"serializer:json"`
WorkloadType common.WorkloadTypeDescriptor `json:"workloadType,omitempty" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down Expand Up @@ -176,13 +176,13 @@ func (a *ApplicationComponent) Index() map[string]interface{} {
// ApplicationPolicy app policy
type ApplicationPolicy struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey"`
Name string `json:"name"`
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description"`
Type string `json:"type"`
Creator string `json:"creator"`
Properties *JSONStruct `json:"properties,omitempty"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
// EnvName if it is not empty, the policy is only belong to this environment
// For auto created policies, this field will be assigned a value
EnvName string `json:"envName"`
Expand Down Expand Up @@ -226,7 +226,7 @@ type ApplicationTrait struct {
Alias string `json:"alias"`
Description string `json:"description"`
Type string `json:"type"`
Properties *JSONStruct `json:"properties,omitempty"`
Properties *JSONStruct `json:"properties,omitempty" gorm:"serializer:json"`
CreateTime time.Time `json:"createTime"`
UpdateTime time.Time `json:"updateTime"`
}
Expand Down Expand Up @@ -255,8 +255,8 @@ var WorkflowStepPhaseStopped workflowv1alpha1.WorkflowStepPhase = "stopped"
// ApplicationRevision be created when an application initiates deployment and describes the phased version of the application.
type ApplicationRevision struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey"`
Version string `json:"version"`
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
Version string `json:"version" gorm:"primaryKey"`
RollbackVersion string `json:"rollbackVersion,omitempty"`
// ApplyAppConfig Stores the application configuration during the current deploy.
ApplyAppConfig string `json:"applyAppConfig,omitempty"`
Expand All @@ -281,9 +281,9 @@ type ApplicationRevision struct {
// EnvName is the env name of this application revision
EnvName string `json:"envName"`
// CodeInfo is the code info of this application revision
CodeInfo *CodeInfo `json:"codeInfo,omitempty"`
CodeInfo *CodeInfo `json:"codeInfo,omitempty" gorm:"serializer:json"`
// ImageInfo is the image info of this application revision
ImageInfo *ImageInfo `json:"imageInfo,omitempty"`
ImageInfo *ImageInfo `json:"imageInfo,omitempty" gorm:"serializer:json"`
}

// CodeInfo is the code info for webhook request
Expand Down Expand Up @@ -384,7 +384,7 @@ type ApplicationTrigger struct {
Name string `json:"name"`
Alias string `json:"alias,omitempty"`
Description string `json:"description,omitempty"`
Token string `json:"token"`
Token string `json:"token" gorm:"primaryKey"`
Type string `json:"type"`
PayloadType string `json:"payloadType"`
ComponentName string `json:"componentName"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/domain/model/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ var (
// Cluster describes the model of cluster in apiserver
type Cluster struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description"`
Icon string `json:"icon"`
Labels map[string]string `json:"labels"`
Labels map[string]string `json:"labels" gorm:"serializer:json"`
Status string `json:"status"`
Reason string `json:"reason"`
Provider ProviderInfo `json:"provider"`
Provider ProviderInfo `json:"provider" gorm:"serializer:json"`
APIServerURL string `json:"apiServerURL"`
DashboardURL string `json:"dashboardURL"`
KubeConfig string `json:"kubeConfig"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/domain/model/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
// Env models the data of env in database
type Env struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description,omitempty"`

Expand All @@ -34,7 +34,7 @@ type Env struct {

// Targets defines the name of delivery target that belongs to this env
// In one project, a delivery target can only belong to one env.
Targets []string `json:"targets,omitempty"`
Targets []string `json:"targets,omitempty" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/domain/model/envbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func init() {
// EnvBinding application env binding
type EnvBinding struct {
BaseModel
AppPrimaryKey string `json:"appPrimaryKey"`
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
AppDeployName string `json:"appDeployName"`
Name string `json:"name"`
ComponentsPatch []ComponentPatch `json:"componentsPatchs"`
Name string `json:"name" gorm:"primaryKey"`
ComponentsPatch []ComponentPatch `json:"componentsPatchs" gorm:"serializer:json"`
}

// ComponentPatch Define differential patches for components in the environment.
Expand Down
16 changes: 8 additions & 8 deletions pkg/server/domain/model/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type WorkflowSpec struct {
// Pipeline is the model of pipeline
type Pipeline struct {
BaseModel
Spec WorkflowSpec
Name string `json:"name"`
Project string `json:"project"`
Alias string `json:"alias"`
Description string `json:"description"`
Spec WorkflowSpec `gorm:"serializer:json"`
Name string `json:"name" gorm:"primaryKey"`
Project string `json:"project" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description"`
}

// PrimaryKey return custom primary key
Expand Down Expand Up @@ -81,9 +81,9 @@ type Value struct {
// PipelineContext is pipeline's context groups
type PipelineContext struct {
BaseModel
PipelineName string `json:"pipelineName"`
ProjectName string `json:"projectName"`
Contexts map[string][]Value `json:"contexts"`
PipelineName string `json:"pipelineName" gorm:"primaryKey"`
ProjectName string `json:"projectName" gorm:"primaryKey"`
Contexts map[string][]Value `json:"contexts" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/domain/model/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func init() {
// PluginSetting save the setting data of the plugin
type PluginSetting struct {
BaseModel
ID string `json:"id"`
ID string `json:"id" gorm:"primaryKey"`
Enabled bool `json:"enabled"`
JSONData map[string]interface{} `json:"jsonData"`
SecureJSONData map[string]interface{} `json:"secureJsonData"`
JSONData map[string]interface{} `json:"jsonData" gorm:"serializer:json"`
SecureJSONData map[string]interface{} `json:"secureJsonData" gorm:"serializer:json"`
}

// PrimaryKey return custom primary key
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/domain/model/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
// Project basic model
type Project struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Owner string `json:"owner"`
Description string `json:"description,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions pkg/server/domain/model/system_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const (
type SystemInfo struct {
BaseModel
SignedKey string `json:"signedKey"`
InstallID string `json:"installID"`
InstallID string `json:"installID" gorm:"primaryKey"`
EnableCollection bool `json:"enableCollection"`
StatisticInfo StatisticInfo `json:"statisticInfo,omitempty"`
StatisticInfo StatisticInfo `json:"statisticInfo,omitempty" gorm:"serializer:json"`
LoginType string `json:"loginType"`
DexUserDefaultProjects []ProjectRef `json:"projects"`
DexUserDefaultPlatformRoles []string `json:"dexUserDefaultPlatformRoles"`
DexUserDefaultProjects []ProjectRef `json:"projects" gorm:"serializer:json"`
DexUserDefaultPlatformRoles []string `json:"dexUserDefaultPlatformRoles" gorm:"serializer:json"`
}

// ProjectRef set the project name and roles
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/domain/model/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func init() {
// It includes kubernetes clusters or cloud service providers
type Target struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias,omitempty"`
Project string `json:"project"`
Description string `json:"description,omitempty"`
Cluster *ClusterTarget `json:"cluster,omitempty"`
Variable map[string]interface{} `json:"variable,omitempty"`
Cluster *ClusterTarget `json:"cluster,omitempty" gorm:"serializer:json"`
Variable map[string]interface{} `json:"variable,omitempty" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down
22 changes: 11 additions & 11 deletions pkg/server/domain/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type User struct {
Disabled bool `json:"disabled"`
LastLoginTime time.Time `json:"lastLoginTime,omitempty"`
// UserRoles binding the platform level roles
UserRoles []string `json:"userRoles"`
UserRoles []string `json:"userRoles" gorm:"serializer:json"`
DexSub string `json:"dexSub,omitempty"`
}

Expand Down Expand Up @@ -97,7 +97,7 @@ type ProjectUser struct {
Username string `json:"username"`
ProjectName string `json:"projectName"`
// UserRoles binding the project level roles
UserRoles []string `json:"userRoles"`
UserRoles []string `json:"userRoles" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down Expand Up @@ -140,7 +140,7 @@ type Role struct {
Name string `json:"name"`
Alias string `json:"alias"`
Project string `json:"project,omitempty"`
Permissions []string `json:"permissions"`
Permissions []string `json:"permissions" gorm:"serializer:json"`
}

// Permission is a model for a new RBAC mode.
Expand All @@ -149,12 +149,12 @@ type Permission struct {
Name string `json:"name"`
Alias string `json:"alias"`
Project string `json:"project,omitempty"`
Resources []string `json:"resources"`
Actions []string `json:"actions"`
Resources []string `json:"resources" gorm:"serializer:json"`
Actions []string `json:"actions" gorm:"serializer:json"`
// Effect option values: Allow,Deny
Effect string `json:"effect"`
Principal *Principal `json:"principal,omitempty"`
Condition *Condition `json:"condition,omitempty"`
Principal *Principal `json:"principal,omitempty" gorm:"serializer:json"`
Condition *Condition `json:"condition,omitempty" gorm:"serializer:json"`
}

// Principal is a model for a new RBAC mode.
Expand Down Expand Up @@ -234,14 +234,14 @@ func (p *Permission) Index() map[string]interface{} {
// PermissionTemplate is a model for a new RBAC mode.
type PermissionTemplate struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
// Scope options: project or platform
Scope string `json:"scope"`
Resources []string `json:"resources"`
Actions []string `json:"actions"`
Resources []string `json:"resources" gorm:"serializer:json"`
Actions []string `json:"actions" gorm:"serializer:json"`
Effect string `json:"effect"`
Condition *Condition `json:"condition,omitempty"`
Condition *Condition `json:"condition,omitempty" gorm:"serializer:json"`
}

// TableName return custom table name
Expand Down
14 changes: 7 additions & 7 deletions pkg/server/domain/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ const UnFinished = "false"
// Workflow application delivery database model
type Workflow struct {
BaseModel
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Alias string `json:"alias"`
Description string `json:"description"`
// Workflow used by the default
Default *bool `json:"default"`
AppPrimaryKey string `json:"appPrimaryKey"`
AppPrimaryKey string `json:"appPrimaryKey" gorm:"primaryKey"`
EnvName string `json:"envName"`
Mode workflowv1alpha1.WorkflowExecuteMode `json:"mode,omitempty"`
Steps []WorkflowStep `json:"steps,omitempty"`
Mode workflowv1alpha1.WorkflowExecuteMode `json:"mode,omitempty" gorm:"serializer:json"`
Steps []WorkflowStep `json:"steps,omitempty" gorm:"serializer:json"`
}

// WorkflowStep defines how to execute a workflow step.
Expand Down Expand Up @@ -114,16 +114,16 @@ type WorkflowRecord struct {
AppPrimaryKey string `json:"appPrimaryKey"`
// RevisionPrimaryKey: should be assigned the version(PublishVersion)
RevisionPrimaryKey string `json:"revisionPrimaryKey"`
Name string `json:"name"`
Name string `json:"name" gorm:"primaryKey"`
Namespace string `json:"namespace"`
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty"`
Finished string `json:"finished"`
Steps []WorkflowStepStatus `json:"steps,omitempty"`
Steps []WorkflowStepStatus `json:"steps,omitempty" gorm:"serializer:json"`
Status string `json:"status"`
Message string `json:"message"`
Mode string `json:"mode"`
ContextValue map[string]string `json:"contextValue,omitempty"`
ContextValue map[string]string `json:"contextValue,omitempty" gorm:"serializer:json"`
}

// WorkflowStepStatus is the workflow step status database model
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/infrastructure/datastore/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
mysqlgorm "gorm.io/driver/mysql"
"gorm.io/gorm"

"github.com/kubevela/velaux/pkg/server/domain/model"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore"
)

Expand All @@ -38,6 +39,10 @@ func New(ctx context.Context, cfg datastore.Config) (datastore.DataStore, error)
return nil, err
}

for _, v := range model.GetRegisterModels() {
db.AutoMigrate(v)

Check failure on line 43 in pkg/server/infrastructure/datastore/mysql/mysql.go

View workflow job for this annotation

GitHub Actions / check

Error return value of `db.AutoMigrate` is not checked (errcheck)
}

m := &mysql{
client: *db,
database: cfg.Database,
Expand Down

0 comments on commit fe3ade9

Please sign in to comment.