ddl: fix a bug that dropping primary key lead to implicit invisible primary key success (#19281)

Signed-off-by: wjhuang2016 <huangwenjun1997@gmail.com>
This commit is contained in:
wjHuang
2020-09-01 21:05:10 +08:00
committed by GitHub
parent 915d84d7ad
commit 7bdb785d0f
2 changed files with 17 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {