Skip to content

Commit 222ea14

Browse files
committed
Merge pull request #37 from pingcap/shenli/check-duplicate-column
ddl: Check duplicate when adding column
2 parents 0498db4 + 722b759 commit 222ea14

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

ddl/ddl.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ func (d *ddl) addColumn(ctx context.Context, schema model.CIStr, tbl table.Table
356356
cols := tbl.Cols()
357357
position := len(cols)
358358
name := spec.Column.Name
359+
// Check column name duplicate
360+
dc := column.FindCol(cols, name)
361+
if dc != nil {
362+
return errors.Errorf("Try to add a column with the same name of an already exists column.")
363+
}
359364
if spec.Position.Type == ColumnPositionFirst {
360365
position = 0
361366
} else if spec.Position.Type == ColumnPositionAfter {
@@ -367,7 +372,7 @@ func (d *ddl) addColumn(ctx context.Context, schema model.CIStr, tbl table.Table
367372
// insert position is after the mentioned column
368373
position = c.Offset + 1
369374
}
370-
// TODO: check duplicate and set constraint
375+
// TODO: Set constraint
371376
col, _, err := d.buildColumnAndConstraint(position, spec.Column)
372377
if err != nil {
373378
return errors.Trace(err)

ddl/ddl_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ func (ts *testSuite) TestT(c *C) {
107107
}
108108
}
109109
alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt)
110-
dd.AlterTable(ctx, tbIdent, alterStmt.Specs)
110+
err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs)
111+
c.Assert(err, IsNil)
111112
c.Assert(alterStmt.Specs[0].String(), Not(Equals), "")
113+
// Inserting a duplicated column will cause error.
114+
alterStmt = statement("alter table t add column bb int after b").(*stmts.AlterTableStmt)
115+
err = dd.AlterTable(ctx, tbIdent, alterStmt.Specs)
116+
c.Assert(err, NotNil)
112117

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

0 commit comments

Comments
 (0)