Skip to content

Commit

Permalink
ddl: fix bug (pingcap#2720)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and hanfei1991 committed Feb 23, 2017
1 parent 0282310 commit b3ba1f7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
11 changes: 1 addition & 10 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,11 +920,7 @@ func setDefaultAndComment(ctx context.Context, col *table.Column, options []*ast
return nil
}

var (
hasDefaultValue bool
hasNotNull bool
setOnUpdateNow bool
)
var hasDefaultValue, setOnUpdateNow bool
for _, opt := range options {
switch opt.Tp {
case ast.ColumnOptionDefaultValue:
Expand All @@ -941,7 +937,6 @@ func setDefaultAndComment(ctx context.Context, col *table.Column, options []*ast
}
case ast.ColumnOptionNotNull:
col.Flag |= mysql.NotNullFlag
hasNotNull = true
case ast.ColumnOptionNull:
col.Flag &= ^uint(mysql.NotNullFlag)
case ast.ColumnOptionOnUpdate:
Expand All @@ -959,10 +954,6 @@ func setDefaultAndComment(ctx context.Context, col *table.Column, options []*ast
}

setTimestampDefaultValue(col, hasDefaultValue, setOnUpdateNow)
if col.Tp == mysql.TypeTimestamp && hasNotNull {
return errors.Trace(errInvalidUseOfNull)
}

if hasDefaultValue {
return errors.Trace(checkDefaultValue(ctx, col, true))
}
Expand Down
10 changes: 4 additions & 6 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,20 +753,18 @@ func (s *testDBSuite) TestChangeColumn(c *C) {
s.tk.MustQuery("select b from t3").Check(testkit.Rows(rowStr, rowStr1, rowStr2))
// for timestamp
s.mustExec(c, "alter table t3 add column c timestamp not null")
s.mustExec(c, "alter table t3 change c c timestamp default '2017-02-11' comment 'col c comment' on update current_timestamp")
s.mustExec(c, "alter table t3 change c c timestamp not null default '2017-02-11' comment 'col c comment' on update current_timestamp")
is = sessionctx.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC := tblInfo.Columns[2]
c.Assert(colC.Comment, Equals, "col c comment")
hasNotNull = tmysql.HasNotNullFlag(colC.Flag)
c.Assert(hasNotNull, IsTrue)

// for failing tests
sql := "alter table t3 change c c timestamp not null"
s.testErrorCode(c, sql, tmysql.ErrInvalidUseOfNull)
sql = "alter table t3 change c c timestamp not null null"
s.testErrorCode(c, sql, tmysql.ErrInvalidUseOfNull)
sql = "alter table t3 change aa a bigint default ''"
sql := "alter table t3 change aa a bigint default ''"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "alter table t3 change a testx.t3.aa bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongDBName)
Expand Down

0 comments on commit b3ba1f7

Please sign in to comment.