Skip to content

Commit

Permalink
feat(dbm-services): 语法检查禁止操作系统库表 #6682
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and iSecloud committed Sep 9, 2024
1 parent f530adc commit 9839e0c
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
package syntax

import (
util "dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/logger"
"github.com/samber/lo"
)

// Checker syntax checker
Expand All @@ -24,7 +23,6 @@ func (c AlterTableResult) Checker(mysqlVersion string) (r *CheckerResult) {
r.Parse(R.AlterTableRule.AlterUseAfter, altercmd.After, "")
// 如果是增加字段,需要判断增加的字段名称是否是关键字
if altercmd.Type == AlterTypeAddColumn {
logger.Info("col name is %s", altercmd.ColDef.ColName)
r.ParseBultinRisk(func() (bool, string) {
return KeyWordValidator(mysqlVersion, altercmd.ColDef.ColName)
})
Expand All @@ -41,11 +39,11 @@ func (c AlterTableResult) Checker(mysqlVersion string) (r *CheckerResult) {
// 去重后得到所有的alter types
func (c AlterTableResult) GetAllAlterType() (alterTypes []string) {
for _, a := range c.AlterCommands {
if !util.StringsHas([]string{"algorithm", "lock"}, a.Type) {
if !lo.Contains([]string{"algorithm", "lock"}, a.Type) {
alterTypes = append(alterTypes, a.Type)
}
}
return util.RemoveDuplicate(alterTypes)
return lo.Uniq(alterTypes)
}

// GetPkAlterType get the primary key change type
Expand Down
13 changes: 2 additions & 11 deletions dbm-services/mysql/db-simulation/app/syntax/create_db_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

package syntax

import "dbm-services/common/go-pubpkg/cmutil"

// Checker TODO
// Checker create db syntax checker
func (c CreateDBResult) Checker(mysqlVersion string) (r *CheckerResult) {
r = &CheckerResult{}
// 检查库名规范
Expand All @@ -26,17 +24,10 @@ func (c CreateDBResult) Checker(mysqlVersion string) (r *CheckerResult) {
return SpecialCharValidator(c.DbName)
})
}
// 不允许包含系统库
r.ParseBultinBan(func() (bool, string) {
if cmutil.HasElem(c.DbName, cmutil.GetGcsSystemDatabasesIgnoreTest(mysqlVersion)) {
return true, "不允许操作系统库" + c.DbName
}
return false, ""
})
return
}

// SpiderChecker TODO
// SpiderChecker spider create db syntax checker
func (c CreateDBResult) SpiderChecker(mysqlVersion string) (r *CheckerResult) {
return c.Checker(mysqlVersion)
}
15 changes: 9 additions & 6 deletions dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package syntax

import (
"fmt"
"strings"

"dbm-services/common/go-pubpkg/cmutil"
"github.com/samber/lo"

"dbm-services/common/go-pubpkg/logger"
)

Expand Down Expand Up @@ -85,24 +85,27 @@ func (c CreateTableResult) GetTableCharset() (engine string) {
// GetAllColCharsets get columns define charset
func (c CreateTableResult) GetAllColCharsets() (charsets []string) {
for _, colDef := range c.CreateDefinitions.ColDefs {
if !cmutil.IsEmpty(colDef.CharacterSet) {
if lo.IsNotEmpty(colDef.CharacterSet) {
charsets = append(charsets, colDef.CharacterSet)
}
}
return cmutil.RemoveDuplicate(charsets)
return lo.Uniq(charsets)
}

// ColCharsetNotEqTbCharset 字段的字符集合和表的字符集合相同
func (c CreateTableResult) ColCharsetNotEqTbCharset() bool {
colCharsets := c.GetAllColCharsets()
fmt.Println("colCharsets", colCharsets, len(colCharsets))
if len(colCharsets) == 0 {
return false
}
if len(colCharsets) > 1 {
return true
}
if strings.Compare(strings.ToUpper(colCharsets[0]), c.GetTableCharset()) == 0 {
tableDefineCharset := c.GetTableCharset()
if lo.IsEmpty(tableDefineCharset) {
return false
}
if strings.Compare(strings.ToUpper(colCharsets[0]), tableDefineCharset) == 0 {
return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"slices"
"strings"

"dbm-services/common/go-pubpkg/cmutil"
"github.com/samber/lo"

"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-simulation/pkg/util"
)
Expand Down Expand Up @@ -67,7 +68,7 @@ func (c CreateTableResult) shardKeyChecker(r *CheckerResult) {
}
tableComment := c.GetComment()
logger.Info("tableComment is %s", tableComment)
if cmutil.IsNotEmpty(tableComment) {
if lo.IsNotEmpty(tableComment) {
// table comment 不为空的时候 先校验comment 格式是否合法
legal, msg := c.validateSpiderComment(tableComment)
if !legal {
Expand Down
Loading

0 comments on commit 9839e0c

Please sign in to comment.