Skip to content

Commit

Permalink
Allow to append values to enum/set (fixes pingcap#6398)
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Jun 29, 2018
1 parent 3c05d77 commit b162ae4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 15 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,21 @@ func modifiable(origin *types.FieldType, to *types.FieldType) error {
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
return nil
}
case mysql.TypeEnum:
case mysql.TypeEnum, mysql.TypeSet:
if origin.Tp == to.Tp {
if len(to.Elems) < len(origin.Elems) {
msg := fmt.Sprintf("elem length %d is less than origin %d", len(to.Elems), len(origin.Elems))
return errUnsupportedModifyColumn.GenByArgs(msg)
}
for index, originElem := range origin.Elems {
toElem := to.Elems[index]
if originElem != toElem {
msg := fmt.Sprintf("cannot modify value %s to %s", originElem, toElem)
return errUnsupportedModifyColumn.GenByArgs(msg)
}
}
return nil
}
return errUnsupportedModifyColumn.GenByArgs("modify enum column is not supported")
default:
if origin.Tp == to.Tp {
Expand Down
5 changes: 4 additions & 1 deletion ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@ func (s *testDBSuite) TestChangeColumn(c *C) {
c.Assert(hasNotNull, IsFalse)
// for enum
s.mustExec(c, "alter table t3 add column en enum('a', 'b', 'c') not null default 'a'")
s.mustExec(c, "alter table t3 modify column en enum('a', 'b', 'c', 'd') not null default 'a'")

// for failing tests
sql := "alter table t3 change aa a bigint default ''"
Expand All @@ -1254,7 +1255,9 @@ func (s *testDBSuite) TestChangeColumn(c *C) {
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
sql = "alter table t3 change aa a bigint not null"
s.testErrorCode(c, sql, tmysql.ErrUnknown)
sql = "alter table t3 modify en enum('a', 'z', 'b', 'c') not null default 'a'"
sql = "alter table t3 modify en enum('a', 'b', 'c') not null default 'a'"
s.testErrorCode(c, sql, tmysql.ErrUnknown)
sql = "alter table t3 modify en enum('a', 'z', 'b', 'c', 'd') not null default 'a'"
s.testErrorCode(c, sql, tmysql.ErrUnknown)
// Rename to an existing column.
s.mustExec(c, "alter table t3 add column a bigint")
Expand Down

0 comments on commit b162ae4

Please sign in to comment.