ddl: Check duplicate when adding column

Add a duplicate column is not right.
This commit is contained in:
shenli
2015-09-07 15:54:52 +08:00
parent 01d4a6dac1
commit a8f358aac0
2 changed files with 12 additions and 2 deletions

View File

@ -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)

View File

@ -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)