From e44739ace3c9b6739642423c4dedcadd5bccd201 Mon Sep 17 00:00:00 2001 From: make123 Date: Wed, 11 Sep 2024 14:16:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(dbm-services):=20=E8=A1=A5=E5=85=85tendbclu?= =?UTF-8?q?ster=20create=20table=20=E6=B2=A1=E6=9C=89=E6=8C=87=E5=AE=9Asha?= =?UTF-8?q?rdkey=E6=83=85=E5=86=B5=E4=B8=8B=E5=88=86=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=20#6840?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/go-pubpkg/bkrepo/bkrepo.go | 2 +- .../app/syntax/create_table_rule.go | 10 ++++-- .../app/syntax/spider_create_table_rule.go | 35 ++++++++++++++----- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/dbm-services/common/go-pubpkg/bkrepo/bkrepo.go b/dbm-services/common/go-pubpkg/bkrepo/bkrepo.go index 6ec146598b..6fb9d66e9f 100644 --- a/dbm-services/common/go-pubpkg/bkrepo/bkrepo.go +++ b/dbm-services/common/go-pubpkg/bkrepo/bkrepo.go @@ -187,7 +187,7 @@ func (b *BkRepoClient) QueryFileNodeInfo(filepath, filename string) (realData Fi return } -// UploadRespData TODO +// UploadRespData upload respone data type UploadRespData struct { Sha256 string `json:"sha256"` Md5 string `json:"md5"` diff --git a/dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go b/dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go index 64d38f591e..0db8e744d3 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go +++ b/dbm-services/mysql/db-simulation/app/syntax/create_table_rule.go @@ -84,8 +84,14 @@ func (c CreateTableResult) GetTableCharset() (engine string) { // GetAllColCharsets get columns define charset func (c CreateTableResult) GetAllColCharsets() (charsets []string) { + haveDefaultCharseTypes := []string{ + "timestamp", "time", "datetime", "date", + "tinyblob", "blob", "mediumblob", "longblob", + "string", "varchar", "json", + } for _, colDef := range c.CreateDefinitions.ColDefs { - if lo.IsNotEmpty(colDef.CharacterSet) { + // Mysql会对MYSQL_TYPE_TIME,MYSQL_TYPE_TIMESTAMP,MYSQL_TYPE_DATETIME这三种类型的字段默认设置字符集为latin1 + if lo.IsNotEmpty(colDef.CharacterSet) && !lo.Contains(haveDefaultCharseTypes, colDef.DataType) { charsets = append(charsets, colDef.CharacterSet) } } @@ -105,7 +111,7 @@ func (c CreateTableResult) ColCharsetNotEqTbCharset() bool { if lo.IsEmpty(tableDefineCharset) { return false } - if strings.Compare(strings.ToUpper(colCharsets[0]), tableDefineCharset) == 0 { + if strings.Compare(strings.ToLower(colCharsets[0]), strings.ToLower(tableDefineCharset)) == 0 { return false } return true diff --git a/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go b/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go index 7d819a427d..3eba48dd4a 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go +++ b/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go @@ -47,17 +47,19 @@ func (c CreateTableResult) SpiderChecker(spiderVersion string) (r *CheckerResult } // shardKeyChecker 分片键检查 +// nolint func (c CreateTableResult) shardKeyChecker(r *CheckerResult) { - var hasShardKey bool + var commentSpecialShardKey bool var shardKeyCol string var err error + var pubCols []string + if len(c.CreateDefinitions.KeyDefs) == 0 { r.Trigger(SR.SpiderCreateTableRule.NoIndexExists, "") return } - hasPk, uks, keys := c.findTablesIndex() + _, uks, keys := c.findTablesIndex() // 如果存在多个唯一健(含主键),多个唯一键都没有包含相同的字段也是不允许的 - var pubCols []string logger.Info("uniqueKeys is %v,len is %d", uks, len(uks)) if len(uks) > 1 { pubCols = findCommonColByKeys(uks) @@ -77,19 +79,23 @@ func (c CreateTableResult) shardKeyChecker(r *CheckerResult) { } shardKeyCol, err = util.ParseGetShardKeyForSpider(tableComment) if err != nil { - // Todo 错误处理 logger.Error("parse %s comment %s shard key failed %s", c.TableName, tableComment, err.Error()) return } - hasShardKey = true + commentSpecialShardKey = true } // 如果table comment 为空,表示没有指定shard key,或table comnent 没有指定shardkey 由中控自主选择 - if !hasShardKey { + if !commentSpecialShardKey { switch { - case hasPk: - return - case len(uks) >= 1: + case len(uks) == 1: return + case len(uks) > 1: + // 如果没有显示的指定shard key,多个唯一键必须要包含相同的字段,且是第一个字段 + pubPreCols := findCommonPreColByKeys(uks) + if len(pubPreCols) != 1 { + r.Trigger(SR.SpiderCreateTableRule.NoPubColAtMultUniqueIndex, "") + return + } case len(keys) > 1: // 如果没有唯一索引,如果包含多个普通索引,则必须指定shard_key,否则需要报错 r.Trigger(SR.SpiderCreateTableRule.MustSpecialShardKeyOnlyHaveCommonIndex, "") @@ -136,6 +142,7 @@ func (c CreateTableResult) findTablesIndex() (hasPk bool, uks []KeyDef, keys []K switch { case key.PrimaryKey: hasPk = true + uks = append(uks, key) case key.UniqueKey: uks = append(uks, key) default: @@ -161,6 +168,16 @@ func findCommonColByKeys(keys []KeyDef) (cols []string) { return cols } +// findCommonPreColByKeys 寻找多个唯一键中的公共前缀列 +func findCommonPreColByKeys(keys []KeyDef) (cols []string) { + for _, key := range keys { + if len(key.KeyParts) > 0 { + cols = append(cols, key.KeyParts[0].ColName) + } + } + return lo.Uniq(cols) +} + func (c CreateTableResult) validateSpiderComment(comment string) (legal bool, prompt string) { ret := util.ParseGetSpiderUserComment(comment) switch ret {