Skip to content

Commit

Permalink
ddl: store default value as string when value type is binary or bit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and zz-jason committed Apr 1, 2019
1 parent 08346b7 commit c6a757b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,3 +1430,13 @@ func (s *testIntegrationSuite) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
" `b` varchar(50) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func (s *testIntegrationSuite) TestDefaultValueIsString(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
defer s.tk.MustExec("drop table if exists t")
s.tk.MustExec("create table t (a int default b'1');")
tbl := testGetTableByName(c, s.ctx, "test", "t")
c.Assert(tbl.Meta().Columns[0].DefaultValue, Equals, "1")
}
9 changes: 7 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -532,8 +533,12 @@ func getDefaultValue(ctx sessionctx.Context, colName string, c *ast.ColumnOption
// its raw string content here.
return v.GetBinaryLiteral().ToString(), nil
}
// For other kind of fields (e.g. INT), we supply its integer value so that it acts as integers.
return v.GetBinaryLiteral().ToInt(ctx.GetSessionVars().StmtCtx)
// For other kind of fields (e.g. INT), we supply its integer as string value.
value, err := v.GetBinaryLiteral().ToInt(ctx.GetSessionVars().StmtCtx)
if err != nil {
return nil, err
}
return strconv.FormatUint(value, 10), nil
}

if tp == mysql.TypeDuration {
Expand Down

0 comments on commit c6a757b

Please sign in to comment.