diff --git a/ddl/ddl.go b/ddl/ddl.go index 2d18b9d338..8d16b6658e 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -502,7 +502,13 @@ func (d *ddl) AddColumn(ctx context.Context, ti table.Ident, spec *AlterSpecific return errors.Trace(ErrNotExists) } - var col *column.Col + // Check whether added column has existed. + colName := spec.Column.Name + col := column.FindCol(t.Cols(), colName) + if col != nil { + return errors.Errorf("column %s already exists", colName) + } + // ingore table constraints now, maybe return error later // we use length(t.Cols()) as the default offset first, later we will change the // column's offset later. @@ -536,6 +542,12 @@ func (d *ddl) DropColumn(ctx context.Context, ti table.Ident, colName model.CISt return errors.Trace(ErrNotExists) } + // Check whether dropped column has existed. + col := column.FindCol(t.Cols(), colName.L) + if col == nil { + return errors.Errorf("column %s doesn’t exist", colName.L) + } + 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 0d32010426..18255b7e1d 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -190,7 +190,7 @@ LOOP: delete(handles, h) } - c.Assert(len(handles), Equals, 0) + c.Assert(handles, HasLen, 0) } func (s *testDBSuite) testDropIndex(c *C) { @@ -258,7 +258,7 @@ LOOP: handles[h] = struct{}{} } - c.Assert(len(handles), Equals, 0) + c.Assert(handles, HasLen, 0) } func (s *testDBSuite) showColumns(c *C, tableName string) [][]interface{} {