Skip to content

Commit

Permalink
ddl: Return an error when add or drop primary key (pingcap#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored May 3, 2017
1 parent f42570f commit d997cd2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ var (
ErrInvalidIndexState = terror.ClassDDL.New(codeInvalidIndexState, "invalid index state")
// ErrInvalidForeignKeyState returns for invalid foreign key state.
ErrInvalidForeignKeyState = terror.ClassDDL.New(codeInvalidForeignKeyState, "invalid foreign key state")
// ErrUnsupportedModifyPrimaryKey returns an error when add or drop the primary key.
// It's exported for testing.
ErrUnsupportedModifyPrimaryKey = terror.ClassDDL.New(codeUnsupportedModifyPrimaryKey, "unsupported %s primary key")

// ErrColumnBadNull returns for a bad null value.
ErrColumnBadNull = terror.ClassDDL.New(codeBadNull, "column cann't be null")
Expand Down Expand Up @@ -456,11 +459,12 @@ const (
codeInvalidIndexState = 103
codeInvalidForeignKeyState = 104

codeCantDropColWithIndex = 201
codeUnsupportedAddColumn = 202
codeUnsupportedModifyColumn = 203
codeUnsupportedDropPKHandle = 204
codeUnsupportedCharset = 205
codeCantDropColWithIndex = 201
codeUnsupportedAddColumn = 202
codeUnsupportedModifyColumn = 203
codeUnsupportedDropPKHandle = 204
codeUnsupportedCharset = 205
codeUnsupportedModifyPrimaryKey = 206

codeFileNotFound = 1017
codeErrorOnRename = 1025
Expand Down
4 changes: 4 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,8 @@ func (d *ddl) AlterTable(ctx context.Context, ident ast.Ident, specs []*ast.Alte
err = d.CreateIndex(ctx, ident, true, model.NewCIStr(constr.Name), spec.Constraint.Keys)
case ast.ConstraintForeignKey:
err = d.CreateForeignKey(ctx, ident, model.NewCIStr(constr.Name), spec.Constraint.Keys, spec.Constraint.Refer)
case ast.ConstraintPrimaryKey:
err = ErrUnsupportedModifyPrimaryKey.GenByArgs("add")
default:
// Nothing to do now.
}
Expand All @@ -776,6 +778,8 @@ func (d *ddl) AlterTable(ctx context.Context, ident ast.Ident, specs []*ast.Alte
case ast.AlterTableRenameTable:
newIdent := ast.Ident{Schema: spec.NewTable.Schema, Name: spec.NewTable.Name}
err = d.RenameTable(ctx, ident, newIdent)
case ast.AlterTableDropPrimaryKey:
err = ErrUnsupportedModifyPrimaryKey.GenByArgs("drop")
default:
// Nothing to do now.
}
Expand Down
13 changes: 13 additions & 0 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -739,6 +740,18 @@ LOOP:
c.Assert(count, Greater, int64(0))
}

func (s *testDBSuite) TestPrimaryKey(c *C) {
defer testleak.AfterTest(c)()
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)

s.mustExec(c, "create table primary_key_test (a int, b varchar(10))")
_, err := s.tk.Exec("alter table primary_key_test add primary key(a)")
c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue)
_, err = s.tk.Exec("alter table primary_key_test drop primary key")
c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue)
}

func (s *testDBSuite) TestChangeColumn(c *C) {
defer testleak.AfterTest(c)()
s.tk = testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit d997cd2

Please sign in to comment.