From 593eed1eac58c31c407b8665df35a50a6bcf94ef Mon Sep 17 00:00:00 2001 From: crazycs Date: Wed, 1 Apr 2020 17:06:29 +0800 Subject: [PATCH] ddl: fix drop index failed when index contain auto_increment column and the auto_increment column is primary key (#15861) --- ddl/index.go | 6 +++++- ddl/serial_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ddl/index.go b/ddl/index.go index 634c9ba79d2c8..d18a0c61ae0f2 100755 --- a/ddl/index.go +++ b/ddl/index.go @@ -592,7 +592,8 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde func checkDropIndexOnAutoIncrementColumn(tblInfo *model.TableInfo, indexInfo *model.IndexInfo) error { cols := tblInfo.Columns for _, idxCol := range indexInfo.Columns { - if !mysql.HasAutoIncrementFlag(cols[idxCol.Offset].Flag) { + flag := cols[idxCol.Offset].Flag + if !mysql.HasAutoIncrementFlag(flag) { continue } // check the count of index on auto_increment column. @@ -605,6 +606,9 @@ func checkDropIndexOnAutoIncrementColumn(tblInfo *model.TableInfo, indexInfo *mo } } } + if tblInfo.PKIsHandle && mysql.HasPriKeyFlag(flag) { + count++ + } if count < 2 { return autoid.ErrWrongAutoKey } diff --git a/ddl/serial_test.go b/ddl/serial_test.go index b4a2f3ad80bb0..d7c85c95a868f 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -150,6 +150,14 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) { c.Assert(err.Error(), Equals, "[ddl:206]Unsupported drop primary key when alter-primary-key is false") } +func (s *testSerialSuite) TestDropAutoIncrementIndex(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t1 (a int(11) not null auto_increment key, b int(11), c bigint, unique key (a, b, c))") + tk.MustExec("alter table t1 drop index a") +} + func (s *testSerialSuite) TestMultiRegionGetTableEndHandle(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("drop database if exists test_get_endhandle")