From a8f358aac0600a93c74ea72d3ca796eb73a3921d Mon Sep 17 00:00:00 2001 From: shenli Date: Mon, 7 Sep 2015 15:54:52 +0800 Subject: [PATCH 1/2] ddl: Check duplicate when adding column Add a duplicate column is not right. --- ddl/ddl.go | 7 ++++++- ddl/ddl_test.go | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index d5cf04bc80ebf..285b8e9187b60 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -356,6 +356,11 @@ func (d *ddl) addColumn(ctx context.Context, schema model.CIStr, tbl table.Table cols := tbl.Cols() position := len(cols) name := spec.Column.Name + // Check column name duplicate + dc := column.FindCol(cols, name) + if dc != nil { + return errors.Errorf("Try to add a column with the same name of an already exists column.") + } if spec.Position.Type == ColumnPositionFirst { position = 0 } else if spec.Position.Type == ColumnPositionAfter { @@ -367,7 +372,7 @@ func (d *ddl) addColumn(ctx context.Context, schema model.CIStr, tbl table.Table // insert position is after the mentioned column position = c.Offset + 1 } - // TODO: check duplicate and set constraint + // TODO: Set constraint col, _, err := d.buildColumnAndConstraint(position, spec.Column) if err != nil { return errors.Trace(err) diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 953617988193d..f7e77312d5c85 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -107,8 +107,13 @@ func (ts *testSuite) TestT(c *C) { } } alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt) - dd.AlterTable(ctx, tbIdent, alterStmt.Specs) + err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) + c.Assert(err, IsNil) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") + // Insert a duplicate column will cause error + alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt) + err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) + c.Assert(err, NotNil) idxStmt := statement("CREATE INDEX idx_c ON t (c)").(*stmts.CreateIndexStmt) idxName := model.NewCIStr(idxStmt.IndexName) From 722b75914ba6f800d406a2f4beaddf433489ae16 Mon Sep 17 00:00:00 2001 From: shenli Date: Mon, 7 Sep 2015 17:42:46 +0800 Subject: [PATCH 2/2] ddl: Fix typo in ddl_test.go Address comment --- ddl/ddl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index f7e77312d5c85..dfbbe58a45e3b 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -110,7 +110,7 @@ func (ts *testSuite) TestT(c *C) { err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) c.Assert(err, IsNil) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") - // Insert a duplicate column will cause error + // Inserting a duplicated column will cause error. alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt) err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs) c.Assert(err, NotNil)