From 3c2afb2067f93a0d7f5b9dddf4f5b7a236593dc0 Mon Sep 17 00:00:00 2001 From: "wu.sphinx" Date: Thu, 15 Nov 2018 09:18:04 +0800 Subject: [PATCH] Add test case and function rename --- advisor/index.go | 7 ++++--- advisor/index_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/advisor/index.go b/advisor/index.go index 0e74f87..03581b8 100644 --- a/advisor/index.go +++ b/advisor/index.go @@ -30,7 +30,7 @@ import ( ) const ( - // https://dev.mysql.com/doc/refman/8.0/en/identifiers.html + // IndexNameMaxLength Ref. https://dev.mysql.com/doc/refman/8.0/en/identifiers.html IndexNameMaxLength = 64 ) @@ -562,7 +562,7 @@ func (idxAdv *IndexAdvisor) mergeIndexes(idxList []IndexInfo) []IndexInfo { // 检测索引名称是否重复? if existedIndexes := indexMeta.FindIndex(database.IndexKeyName, idx.Name); len(existedIndexes) > 0 { var newName string - idxSuffix := getIndexNameSuffix() + idxSuffix := getRandomIndexSuffix() if len(idx.Name) < IndexNameMaxLength-len(idxSuffix) { newName = idx.Name + idxSuffix } else { @@ -584,7 +584,8 @@ func (idxAdv *IndexAdvisor) mergeIndexes(idxList []IndexInfo) []IndexInfo { return rmSelfDupIndex(indexes) } -func getIndexNameSuffix() string { +// getRandomIndexSuffix format: _xxxx, length: 5 +func getRandomIndexSuffix() string { return fmt.Sprintf("_%s", uniuri.New()[:4]) } diff --git a/advisor/index_test.go b/advisor/index_test.go index b0e35d0..314655f 100644 --- a/advisor/index_test.go +++ b/advisor/index_test.go @@ -19,6 +19,7 @@ package advisor import ( "fmt" "os" + "strings" "testing" "github.com/XiaoMi/soar/common" @@ -464,3 +465,12 @@ func TestIdxColsTypeCheck(t *testing.T) { } } } + +func TestGetRandomIndexSuffix(t *testing.T) { + for i := 0; i < 5; i++ { + r := getRandomIndexSuffix() + if !(strings.HasPrefix(r, "_") && len(r) == 5) { + t.Errorf("getRandomIndexSuffix should return a string with prefix `_` and 5 length, but got:%s", r) + } + } +} -- GitLab