diff --git a/ddl/index.go b/ddl/index.go index 916b7b9148..1725ce9521 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -687,6 +687,21 @@ func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Inde return nil, nil, autoid.ErrWrongAutoKey } + // Check that drop primary index will not cause invisible implicit primary index. + newIndices := make([]*model.IndexInfo, 0, len(tblInfo.Indices)) + for _, idx := range tblInfo.Indices { + if idx.Name.L != indexInfo.Name.L { + newIndices = append(newIndices, idx) + } + } + newTbl := tblInfo.Clone() + newTbl.Indices = newIndices + err = checkInvisibleIndexOnPK(newTbl) + if err != nil { + job.State = model.JobStateCancelled + return nil, nil, errors.Trace(err) + } + return tblInfo, indexInfo, nil } diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 1066c93dc2..2b2122734b 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -1379,6 +1379,8 @@ func (s *testSerialSuite) TestInvisibleIndex(c *C) { tk.MustExec("insert into t6 values (1, 2)") tk.MustQuery("select * from t6").Check(testkit.Rows("1 2")) tk.MustGetErrCode("alter table t6 drop primary key", errno.ErrPKIndexCantBeInvisible) + res := tk.MustQuery("show index from t6 where Key_name='PRIMARY';") + c.Check(len(res.Rows()), Equals, 1) } func (s *testSerialSuite) TestCreateClusteredIndex(c *C) {