From b690f1ceadabedb7e5ae1ad4380a71062d1b4b2e Mon Sep 17 00:00:00 2001 From: lance6716 Date: Fri, 19 Aug 2022 17:04:53 +0800 Subject: [PATCH] dumpling: fix wrong behaviour of StrictCollationCompatible (#37243) close pingcap/tidb#37241 --- dumpling/export/dump.go | 4 ++-- dumpling/export/dump_test.go | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 42f796996e660..1310c37ffa314 100644 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -512,7 +512,6 @@ func adjustTableCollation(tctx *tcontext.Context, collationCompatible string, pa } if collation == "" && charset != "" { - // get db collation collation, ok := charsetAndDefaultCollationMap[strings.ToLower(charset)] if !ok { tctx.L().Warn("not found table charset default collation.", zap.String("originSQL", originSQL), zap.String("charset", strings.ToLower(charset))) @@ -541,11 +540,12 @@ func adjustTableCollation(tctx *tcontext.Context, collationCompatible string, pa // adjustColumnsCollation adds column's collation. func adjustColumnsCollation(tctx *tcontext.Context, createStmt *ast.CreateTableStmt, charsetAndDefaultCollationMap map[string]string) { +ColumnLoop: for _, col := range createStmt.Cols { for _, options := range col.Options { // already have 'Collation' if options.Tp == ast.ColumnOptionCollate { - continue + continue ColumnLoop } } fieldType := col.Tp diff --git a/dumpling/export/dump_test.go b/dumpling/export/dump_test.go index 51e522bf4824b..1f18e9e8aa19c 100644 --- a/dumpling/export/dump_test.go +++ b/dumpling/export/dump_test.go @@ -178,6 +178,7 @@ func TestAdjustTableCollation(t *testing.T) { "create table `test`.`t1` (id int, name varchar(20), work varchar(20)) CHARSET=utf8mb4", "create table `test`.`t1` (id int, name varchar(20) COLLATE utf8mb4_general_ci, work varchar(20)) CHARSET=utf8mb4", "create table `test`.`t1` (id int, name varchar(20) COLLATE utf8mb4_general_ci, work varchar(20) CHARACTER SET utf8mb4) CHARSET=utf8mb4", + "create table `test`.`t1` (name varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin) CHARSET=latin1 COLLATE=latin1_bin", } expectedSQLs := []string{ @@ -191,6 +192,7 @@ func TestAdjustTableCollation(t *testing.T) { "CREATE TABLE `test`.`t1` (`id` INT,`name` VARCHAR(20),`work` VARCHAR(20)) DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_GENERAL_CI", "CREATE TABLE `test`.`t1` (`id` INT,`name` VARCHAR(20) COLLATE utf8mb4_general_ci,`work` VARCHAR(20)) DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_GENERAL_CI", "CREATE TABLE `test`.`t1` (`id` INT,`name` VARCHAR(20) COLLATE utf8mb4_general_ci,`work` VARCHAR(20) CHARACTER SET UTF8MB4 COLLATE utf8mb4_general_ci) DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_GENERAL_CI", + "CREATE TABLE `test`.`t1` (`name` VARCHAR(20) CHARACTER SET UTF8MB4 COLLATE utf8mb4_bin) DEFAULT CHARACTER SET = LATIN1 DEFAULT COLLATE = LATIN1_BIN", } charsetAndDefaultCollationMap := map[string]string{"utf8mb4": "utf8mb4_general_ci"}