Skip to content

Commit

Permalink
ddl: check if the dropping index is primary key when doing "drop inde…
Browse files Browse the repository at this point in the history
…x `primary` on...". (pingcap#14122)
  • Loading branch information
zimulala authored Jan 7, 2020
1 parent a6d26ac commit 1f103e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ func (s *testDBSuite5) TestAlterPrimaryKey(c *C) {
s.tk.MustGetErrCode("alter table test_add_pk add primary key(d);", mysql.ErrUnsupportedOnGeneratedColumn)
// The primary key name is the same as the existing index name.
s.tk.MustExec("alter table test_add_pk add primary key idx(e)")
s.tk.MustExec("alter table test_add_pk drop primary key")
s.tk.MustExec("drop index `primary` on test_add_pk")

// for describing table
s.tk.MustExec("create table test_add_pk1(a int, index idx(a))")
Expand Down Expand Up @@ -1456,6 +1456,7 @@ func (s *testDBSuite5) TestAlterPrimaryKey(c *C) {
s.tk.MustExec("alter table test_add_pk drop primary key")
// for not existing primary key
s.tk.MustGetErrCode("alter table test_add_pk drop primary key", mysql.ErrCantDropFieldOrKey)
s.tk.MustGetErrCode("drop index `primary` on test_add_pk", mysql.ErrCantDropFieldOrKey)

// for too many key parts specified
s.tk.MustGetErrCode("alter table test_add_pk add primary key idx_test(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17);",
Expand Down
12 changes: 7 additions & 5 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
case ast.AlterTableDropIndex:
err = d.DropIndex(ctx, ident, model.NewCIStr(spec.Name), spec.IfExists)
case ast.AlterTableDropPrimaryKey:
err = d.dropIndex(ctx, ident, true, model.NewCIStr(mysql.PrimaryKeyName), spec.IfExists)
err = d.DropIndex(ctx, ident, model.NewCIStr(mysql.PrimaryKeyName), spec.IfExists)
case ast.AlterTableRenameIndex:
err = d.RenameIndex(ctx, ident, spec)
case ast.AlterTableDropPartition:
Expand Down Expand Up @@ -3846,10 +3846,6 @@ func (d *ddl) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName model.
}

func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CIStr, ifExists bool) error {
return d.dropIndex(ctx, ti, false, indexName, ifExists)
}

func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, isPK bool, indexName model.CIStr, ifExists bool) error {
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
Expand All @@ -3861,6 +3857,12 @@ func (d *ddl) dropIndex(ctx sessionctx.Context, ti ast.Ident, isPK bool, indexNa
}

indexInfo := t.Meta().FindIndexByName(indexName.L)
var isPK bool
if indexName.L == strings.ToLower(mysql.PrimaryKeyName) &&
// Before we fixed #14243, there might be a general index named `primary` but not a primary key.
(indexInfo == nil || indexInfo.Primary) {
isPK = true
}
if isPK {
if !config.GetGlobalConfig().AlterPrimaryKey {
return ErrUnsupportedModifyPrimaryKey.GenWithStack("Unsupported drop primary key when alter-primary-key is false")
Expand Down
18 changes: 18 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
tk.MustExec("use test")

tk.MustExec("create table primary_key_test (a int, b varchar(10))")
tk.MustExec("create table primary_key_test_1 (a int, b varchar(10), primary key(a))")
_, err := tk.Exec("alter table primary_key_test add primary key(a)")
c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue)
_, err = tk.Exec("alter table primary_key_test drop primary key")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")
// for "drop index `primary` on ..." syntax
_, err = tk.Exec("drop index `primary` on primary_key_test")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")
_, err = tk.Exec("drop index `primary` on primary_key_test_1")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")

// Change the value of AlterPrimaryKey.
tk.MustExec("create table primary_key_test1 (a int, b varchar(10), primary key(a))")
Expand All @@ -115,6 +121,18 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
tk.MustExec("alter table primary_key_test2 drop primary key")
_, err = tk.Exec("alter table primary_key_test3 drop primary key")
c.Assert(err.Error(), Equals, "[ddl:1091]Can't DROP 'PRIMARY'; check that column/key exists")

// for "drop index `primary` on ..." syntax
tk.MustExec("create table primary_key_test4 (a int, b varchar(10), primary key(a))")
newCfg.AlterPrimaryKey = false
config.StoreGlobalConfig(&newCfg)
_, err = tk.Exec("drop index `primary` on primary_key_test4")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")
// for the index name is `primary`
tk.MustExec("create table tt(`primary` int);")
tk.MustExec("alter table tt add index (`primary`);")
_, err = tk.Exec("drop index `primary` on tt")
c.Assert(err.Error(), Equals, "[ddl:8200]Unsupported drop primary key when alter-primary-key is false")
}

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

0 comments on commit 1f103e6

Please sign in to comment.