From cd9bd133db720e41fb6f1667cfeb3b66afe84ef1 Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 26 Dec 2016 10:52:16 +0800 Subject: [PATCH] ddl: fix issue 2293 (#2316) --- ddl/column.go | 1 + ddl/ddl.go | 9 +++++++++ ddl/ddl_db_test.go | 2 ++ 3 files changed, 12 insertions(+) diff --git a/ddl/column.go b/ddl/column.go index 133e202501..016b2453e2 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -307,6 +307,7 @@ func (d *ddl) addTableColumn(t table.Table, columnInfo *model.ColumnInfo, reorgI colMeta.defaultVal, _, err = table.GetColDefaultValue(ctx, columnInfo) if err != nil { job.State = model.JobCancelled + log.Errorf("[ddl] fatal: this case shouldn't happen, err:%v", err) return errors.Trace(err) } } else if mysql.HasNotNullFlag(columnInfo.Flag) { diff --git a/ddl/ddl.go b/ddl/ddl.go index c93df16a86..3164f85c8f 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -1021,6 +1021,15 @@ func (d *ddl) AddColumn(ctx context.Context, ti ast.Ident, spec *ast.AlterTableS return errors.Trace(err) } + // Check column default value. + colInfo := col.ToInfo() + if colInfo.DefaultValue != nil { + _, _, err := table.GetColDefaultValue(ctx, colInfo) + if err != nil { + return errors.Trace(err) + } + } + job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, diff --git a/ddl/ddl_db_test.go b/ddl/ddl_db_test.go index 708bec8b86..af029bbf34 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -505,6 +505,8 @@ func (s *testDBSuite) TestIssue2293(c *C) { s.tk.MustExec("create table t_issue_2293 (a int)") _, err := s.tk.Exec("alter table t add b int not null default ''") c.Assert(err, NotNil) + s.tk.MustExec("insert into t_issue_2293 value(1)") + s.tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1")) } func (s *testDBSuite) TestColumn(c *C) {