Skip to content

Commit

Permalink
Merge pull request #37 from pingcap/shenli/check-duplicate-column
Browse files Browse the repository at this point in the history
ddl: Check duplicate when adding column
  • Loading branch information
shenli committed Sep 7, 2015
2 parents 0498db4 + 722b759 commit 222ea14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "")
// 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)

idxStmt := statement("CREATE INDEX idx_c ON t (c)").(*stmts.CreateIndexStmt)
idxName := model.NewCIStr(idxStmt.IndexName)
Expand Down

0 comments on commit 222ea14

Please sign in to comment.