Skip to content

Commit

Permalink
ddl: disallow drop clustered primary key even if allow-alter-pk is tr…
Browse files Browse the repository at this point in the history
…ue (#19525)

Co-authored-by: Evan Zhou <coocood@gmail.com>
  • Loading branch information
tangenta and coocood authored Aug 31, 2020
1 parent b133ae9 commit 1a3103a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4724,6 +4724,9 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI
if t.Meta().PKIsHandle {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when the table's pkIsHandle is true")
}
if t.Meta().IsCommonHandle {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when the table is using clustered index")
}
}
if indexInfo == nil {
err = ErrCantDropFieldOrKey.GenWithStack("index %s doesn't exist", indexName)
Expand Down
14 changes: 14 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
_, err = tk.Exec("create table t1(c1 int not null, primary key(c1) invisible);")
c.Assert(ddl.ErrPKIndexCantBeInvisible.Equal(err), IsTrue)
tk.MustExec("create table t2 (a int, b int not null, primary key(a), unique(b) invisible);")

// Test drop clustered primary key.
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})
tk.MustExec("drop table if exists t;")
tk.MustExec("set tidb_enable_clustered_index=1")
tk.MustExec("create table t(a int, b varchar(64), primary key(b));")
tk.MustExec("insert into t values(1,'a'), (2, 'b');")
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = true
})
errMsg := "[ddl:8200]Unsupported drop primary key when the table is using clustered index"
tk.MustGetErrMsg("alter table t drop primary key;", errMsg)
}

func (s *testSerialSuite) TestDropAutoIncrementIndex(c *C) {
Expand Down

0 comments on commit 1a3103a

Please sign in to comment.