Skip to content

Commit

Permalink
feat(dbm-services): 语法检查tendbcluster 操作json、blob字段不允许有默认值 #8760
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Dec 24, 2024
1 parent 90d6b2b commit c3a5ff1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func (t *TmysqlParse) analyzeRelationDbs(inputfileName, mysqlVersion string) (
if res.ErrorCode != 0 {
return nil, nil, false, err
}
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger},
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
SQLTypeInsertSelect, SQLTypeRelaceSelect},
res.Command) {
return nil, nil, true, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package syntax

import "fmt"

// SpiderChecker syntax checker
func (c AlterTableResult) SpiderChecker(mysqlVersion string) (r *CheckerResult) {
r = &CheckerResult{}
for _, altercmd := range c.AlterCommands {
// 如果是增加字段,需要判断增加的字段名称是否是关键字
if altercmd.Type == AlterTypeAddColumn {
r.ParseBultinRisk(func() (bool, string) {
return KeyWordValidator(mysqlVersion, altercmd.ColDef.ColName)
})
}
}
r.ParseBultinBan(c.NotAllowedDefaulValCol)
return
}

// NotAllowedDefaulValCol 不允许存在默认值的字段
func (c AlterTableResult) NotAllowedDefaulValCol() (bool, string) {
for _, alt := range c.AlterCommands {
if alt.ColDef.IsNotAllowDefaulValCol() {
return true, fmt.Sprintf("col:%s,类型:%s 不允许存在默认值的字段", alt.ColDef.ColName, alt.ColDef.DataType)
}
}
return false, ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package syntax

import (
"fmt"
"slices"
"strings"

Expand All @@ -33,6 +34,7 @@ func (c CreateTableResult) SpiderChecker(spiderVersion string) (r *CheckerResult
return SpecialCharValidator(c.TableName)
})
}
r.ParseBultinBan(c.NotAllowedDefaulValCol)
if c.IsCreateTableSelect {
r.Trigger(SR.SpiderCreateTableRule.CreateWithSelect, "")
}
Expand Down Expand Up @@ -224,3 +226,13 @@ func (c CreateTableResult) getColDef(colName string) (colDef ColDef) {
}
return colDef
}

// NotAllowedDefaulValCol 不允许存在默认值的字段
func (c CreateTableResult) NotAllowedDefaulValCol() (bool, string) {
for _, col := range c.CreateDefinitions.ColDefs {
if col.IsNotAllowDefaulValCol() {
return true, fmt.Sprintf("col:%s,类型:%s 不允许存在默认值:[`%s`] ", col.ColName, col.DataType, col.DefaultVal.Value)
}
}
return false, ""
}
1 change: 1 addition & 0 deletions dbm-services/mysql/db-simulation/app/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ func (c *CheckInfo) runSpidercheck(ddlTbls map[string][]string, res ParseLineQue
logger.Error("json unmasrshal line failed %s", err.Error())
return err
}
sc = o
ddlTbls[o.DbName] = append(ddlTbls[o.DbName], o.TableName)
}
if sc == nil {
Expand Down
45 changes: 36 additions & 9 deletions dbm-services/mysql/db-simulation/app/syntax/tmysqlpase_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ const (
SQLTypeUpdate = "update"
// SQLTypeUseDb is use db sql
SQLTypeUseDb = "change_db"
// SQLTypeInsertSelect insert select sql
SQLTypeInsertSelect = "insert_select"
// SQLTypeRelaceSelect replace select sql
SQLTypeRelaceSelect = "replace_select"
)

// NotAllowedDefaulValColMap 不允许默认值的字段
var NotAllowedDefaulValColMap map[string]struct{}

func init() {
NotAllowedDefaulValColMap = lo.SliceToMap([]string{"json", "tinyblob", "blob", "mediumblob", "longblob"},
func(s string) (string, struct{}) {
return s, struct{}{}
})
}

// ColDef mysql column definition
type ColDef struct {
Type string `json:"type"`
ColName string `json:"col_name"`
DataType string `json:"data_type"`
FieldLength int `json:"field_length"`
Nullable bool `json:"nullable"`
DefaultVal struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"default_val"`
Type string `json:"type"`
ColName string `json:"col_name"`
DataType string `json:"data_type"`
FieldLength int `json:"field_length"`
Nullable bool `json:"nullable"`
DefaultVal *DefaultVal `json:"default_val"`
AutoIncrement bool `json:"auto_increment"`
UniqueKey bool `json:"unique_key"`
PrimaryKey bool `json:"primary_key"`
Expand All @@ -67,6 +78,22 @@ type ColDef struct {
ReferenceDefinition interface{} `json:"reference_definition"`
}

// IsNotAllowDefaulValCol tendbcluster 不允许某些类型的字段存在默认值的字段
func (c ColDef) IsNotAllowDefaulValCol() bool {
if _, ok := NotAllowedDefaulValColMap[c.DataType]; ok {
if c.DefaultVal != nil {
return true
}
}
return false
}

// DefaultVal column default value
type DefaultVal struct {
Type string `json:"type"`
Value string `json:"value"`
}

// KeyDef mysql index definition
type KeyDef struct {
Type string `json:"type"`
Expand Down

0 comments on commit c3a5ff1

Please sign in to comment.