Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: Check duplicate when adding column #37

Merged
merged 2 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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