Skip to content

Commit

Permalink
ddl: fix the enum default value by triming trailing space (#30356)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylzd committed Dec 6, 2021
1 parent e3e2e8d commit ffe9b38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3621,6 +3621,22 @@ func (s *testIntegrationSuite3) TestIssue29282(c *C) {
}
}

// See https://github.com/pingcap/tidb/issues/29327
func (s *testIntegrationSuite3) TestEnumDefaultValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE `t1` ( `a` enum('','a','b') NOT NULL DEFAULT 'b' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `a` enum('','a','b') COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'b'\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"))
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE `t1` ( `a` enum('','a','b') NOT NULL DEFAULT 'b ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `a` enum('','a','b') COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'b'\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"))
}

func (s *testIntegrationSuite3) TestIssue29326(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
4 changes: 3 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,13 @@ func getEnumDefaultValue(v types.Datum, col *table.Column) (string, error) {
v.SetMysqlEnum(enumVal, col.Collate)
return v.ToString()
}

str, err := v.ToString()
if err != nil {
return "", errors.Trace(err)
}
// Ref: https://dev.mysql.com/doc/refman/8.0/en/enum.html
// Trailing spaces are automatically deleted from ENUM member values in the table definition when a table is created.
str = strings.TrimRight(str, " ")
enumVal, err := types.ParseEnumName(col.Elems, str, col.Collate)
if err != nil {
return "", ErrInvalidDefaultValue.GenWithStackByArgs(col.Name.O)
Expand Down

0 comments on commit ffe9b38

Please sign in to comment.