ddl: Check duplicate when adding column
Add a duplicate column is not right.
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user