From fccaf9b1f52e2e5262463a8374d570a913bc98b3 Mon Sep 17 00:00:00 2001 From: qiuyesuifeng Date: Thu, 12 Nov 2015 11:49:44 +0800 Subject: [PATCH 1/3] ddl: add column exist check. --- ddl/ddl.go | 8 +++++++- ddl/ddl_db_test.go | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index 2d18b9d338..d0753f1e84 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("Try to add a column with the same name of an already exists column - %s", 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. diff --git a/ddl/ddl_db_test.go b/ddl/ddl_db_test.go index 02b6237345..8ea55ec53a 100644 --- a/ddl/ddl_db_test.go +++ b/ddl/ddl_db_test.go @@ -187,7 +187,7 @@ LOOP: delete(handles, h) } - c.Assert(len(handles), Equals, 0) + c.Assert(handles, HasLen, 0) } func (s *testDBSuite) testDropIndex(c *C) { @@ -255,7 +255,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{} { From cbf4732a540ffe34379279ab236475a3afc47288 Mon Sep 17 00:00:00 2001 From: qiuyesuifeng Date: Thu, 12 Nov 2015 12:28:34 +0800 Subject: [PATCH 2/3] ddl: drop column exist check. --- ddl/ddl.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ddl/ddl.go b/ddl/ddl.go index d0753f1e84..93f5bf97a8 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -542,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("Try to drop a none exist column - %s", colName.L) + } + job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, From 840249541f3710d244221f3e4064583f5e868ef5 Mon Sep 17 00:00:00 2001 From: qiuyesuifeng Date: Thu, 12 Nov 2015 12:42:31 +0800 Subject: [PATCH 3/3] ddl: address comment. --- ddl/ddl.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index 93f5bf97a8..8d16b6658e 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -506,7 +506,7 @@ func (d *ddl) AddColumn(ctx context.Context, ti table.Ident, spec *AlterSpecific colName := spec.Column.Name col := column.FindCol(t.Cols(), colName) if col != nil { - return errors.Errorf("Try to add a column with the same name of an already exists column - %s", colName) + return errors.Errorf("column %s already exists", colName) } // ingore table constraints now, maybe return error later @@ -545,7 +545,7 @@ func (d *ddl) DropColumn(ctx context.Context, ti table.Ident, colName model.CISt // Check whether dropped column has existed. col := column.FindCol(t.Cols(), colName.L) if col == nil { - return errors.Errorf("Try to drop a none exist column - %s", colName.L) + return errors.Errorf("column %s doesn’t exist", colName.L) } job := &model.Job{