Skip to content

Commit

Permalink
Merge pull request #520 from actiontech/issue_509_models
Browse files Browse the repository at this point in the history
define models
  • Loading branch information
sjjian authored May 13, 2022
2 parents 0811243 + 4037a2a commit 53567a1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
23 changes: 12 additions & 11 deletions sqle/model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import (
type Instance struct {
Model
// has created composite index: [id, name] by gorm#AddIndex
Name string `json:"name" gorm:"not null;index" example:""`
DbType string `json:"db_type" gorm:"column:db_type; not null" example:"mysql"`
Host string `json:"host" gorm:"column:db_host; not null" example:"10.10.10.10"`
Port string `json:"port" gorm:"column:db_port; not null" example:"3306"`
User string `json:"user" gorm:"column:db_user; not null" example:"root"`
Password string `json:"-" gorm:"-"`
SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"`
Desc string `json:"desc" example:"this is a instance"`
WorkflowTemplateId uint `json:"workflow_template_id"`
AdditionalParams params.Params `json:"additional_params" gorm:"type:text"`
MaintenancePeriod Periods `json:"maintenance_period" gorm:"type:text"`
Name string `json:"name" gorm:"not null;index" example:""`
DbType string `json:"db_type" gorm:"column:db_type; not null" example:"mysql"`
Host string `json:"host" gorm:"column:db_host; not null" example:"10.10.10.10"`
Port string `json:"port" gorm:"column:db_port; not null" example:"3306"`
User string `json:"user" gorm:"column:db_user; not null" example:"root"`
Password string `json:"-" gorm:"-"`
SecretPassword string `json:"secret_password" gorm:"column:db_password; not null"`
Desc string `json:"desc" example:"this is a instance"`
WorkflowTemplateId uint `json:"workflow_template_id"`
AdditionalParams params.Params `json:"additional_params" gorm:"type:text"`
MaintenancePeriod Periods `json:"maintenance_period" gorm:"type:text"`
SqlQueryConfig SqlQueryConfig `json:"sql_query_config" gorm:"type:text"`

// relation table
Roles []*Role `json:"-" gorm:"many2many:instance_role;"`
Expand Down
58 changes: 58 additions & 0 deletions sqle/model/sql_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package model

import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)

type SqlQueryConfig struct {
MaxPreQueryRows int `json:"max_pre_query_rows"`
QueryTimeoutSecond int `json:"query_timeout_second"`
}

// Scan impl sql.Scanner interface
func (c *SqlQueryConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to unmarshal json value: %v", value)
}
if len(bytes) == 0 {
return nil
}
result := SqlQueryConfig{}
err := json.Unmarshal(bytes, &result)
*c = result
return err
}

// Value impl sql.driver.Valuer interface
func (c SqlQueryConfig) Value() (driver.Value, error) {
v, err := json.Marshal(c)
if err != nil {
return nil, fmt.Errorf("failed to marshal json value: %v", v)
}
return v, err
}

type SqlQueryHistory struct {
Model
CreateUserId uint `json:"create_user_id" gorm:"not null"`
InstanceId uint `json:"instance_id" gorm:"not null"`
Database string `json:"database"`
RawSql string `json:"raw_sql" gorm:"type:text;not null"`
}

type SqlQueryExecutionSql struct {
Model
SqlQueryHistoryId uint `json:"sql_query_history_id" gorm:"not null"`
Sql string `json:"sql" gorm:"type:text;not null"`
ExecStartAt *time.Time `json:"exec_start_at"`
ExecEndAt *time.Time `json:"exec_end_at"`
ExecResult string `json:"exec_result" gorm:"type:text"`
RawSqlInfo SqlQueryHistory `json:"raw_sql_info" gorm:"foreignkey:SqlQueryHistoryId"`
}
2 changes: 2 additions & 0 deletions sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (s *Storage) AutoMigrate() error {
&WorkflowStep{},
&WorkflowTemplate{},
&Workflow{},
&SqlQueryExecutionSql{},
&SqlQueryHistory{},
).Error
if err != nil {
return errors.New(errors.ConnectStorageError, err)
Expand Down

0 comments on commit 53567a1

Please sign in to comment.