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: fix drop index failed when index contain auto_increment column and the auto_increment column is primary key #15861

Merged
merged 9 commits into from
Apr 1, 2020
6 changes: 5 additions & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,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.
Expand All @@ -676,6 +677,9 @@ func checkDropIndexOnAutoIncrementColumn(tblInfo *model.TableInfo, indexInfo *mo
}
}
}
if tblInfo.PKIsHandle && mysql.HasPriKeyFlag(flag) {
count++
}
if count < 2 {
return autoid.ErrWrongAutoKey
}
Expand Down
8 changes: 8 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ func (s *testSerialSuite) TestPrimaryKey(c *C) {
c.Assert(err.Error(), Equals, "[ddl:8200]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")
Expand Down