From cebc5c72ba7af3837c635ce577fb2839937d4e93 Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 5 Jul 2018 14:42:19 +0800 Subject: [PATCH 1/2] executor, ddl: get the correct result of "show create table" when running "add index" (#6993) --- ddl/ddl_db_change_test.go | 35 +++++++++++++++++++++++++++++++++++ executor/show.go | 16 ++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/ddl/ddl_db_change_test.go b/ddl/ddl_db_change_test.go index dedc59968a43d..7eb1be2d68850 100644 --- a/ddl/ddl_db_change_test.go +++ b/ddl/ddl_db_change_test.go @@ -74,6 +74,41 @@ func (s *testStateChangeSuite) TearDownSuite(c *C) { testleak.AfterTest(c)() } +// TestShowCreateTable tests the result of "show create table" when we are running "add index" or "add column". +func (s *testStateChangeSuite) TestShowCreateTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table t (id int, index idx (id))") + + var checkErr error + prevState := model.StateNone + callback := &ddl.TestDDLCallback{} + callback.OnJobUpdatedExported = func(job *model.Job) { + if job.SchemaState == prevState || checkErr != nil { + return + } + if job.SchemaState != model.StatePublic { + result := tk.MustQuery("show create table t") + got := result.Rows()[0][1] + var expected string + if job.Type == model.ActionAddIndex { + expected = "CREATE TABLE `t` (\n `id` int(11) DEFAULT NULL,\n KEY `idx` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin" + } else if job.Type == model.ActionAddColumn { + expected = "CREATE TABLE `t` (\n `id` int(11) DEFAULT NULL,\n KEY `idx` (`id`),\n KEY `idx1` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin" + } + if got != expected { + checkErr = errors.Errorf("got %s, expected %s", got, expected) + } + } + } + d := s.dom.DDL() + d.(ddl.DDLForTest).SetHook(callback) + tk.MustExec("alter table t add index idx1(id)") + c.Assert(checkErr, IsNil) + tk.MustExec("alter table t add column c int") + c.Assert(checkErr, IsNil) +} + func (s *testStateChangeSuite) TestTwoStates(c *C) { cnt := 5 // New the testExecInfo. diff --git a/executor/show.go b/executor/show.go index 48dac9f748919..3f18500167e5c 100644 --- a/executor/show.go +++ b/executor/show.go @@ -474,9 +474,6 @@ func (e *ShowExec) fetchShowCreateTable() error { var pkCol *table.Column var hasAutoIncID bool for i, col := range tb.Cols() { - if col.State != model.StatePublic { - continue - } buf.WriteString(fmt.Sprintf(" `%s` %s", col.Name.O, col.GetTypeDesc())) if col.IsGenerated() { // It's a generated column. @@ -540,11 +537,14 @@ func (e *ShowExec) fetchShowCreateTable() error { buf.WriteString(",\n") } - for i, idx := range tb.Indices() { - idxInfo := idx.Meta() - if idxInfo.State != model.StatePublic { - continue + publicIndices := make([]table.Index, 0, len(tb.Indices())) + for _, idx := range tb.Indices() { + if idx.Meta().State == model.StatePublic { + publicIndices = append(publicIndices, idx) } + } + for i, idx := range publicIndices { + idxInfo := idx.Meta() if idxInfo.Primary { buf.WriteString(" PRIMARY KEY ") } else if idxInfo.Unique { @@ -562,7 +562,7 @@ func (e *ShowExec) fetchShowCreateTable() error { cols = append(cols, colInfo) } buf.WriteString(fmt.Sprintf("(%s)", strings.Join(cols, ","))) - if i != len(tb.Indices())-1 { + if i != len(publicIndices)-1 { buf.WriteString(",\n") } } From 79e6d4b404d9c2d5ba1ebe66ad7b51363436baab Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 2 Aug 2018 13:55:42 +0800 Subject: [PATCH 2/2] ddl: tiny clean up --- ddl/ddl_db_change_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/ddl_db_change_test.go b/ddl/ddl_db_change_test.go index 7eb1be2d68850..2a819cc489a92 100644 --- a/ddl/ddl_db_change_test.go +++ b/ddl/ddl_db_change_test.go @@ -102,7 +102,7 @@ func (s *testStateChangeSuite) TestShowCreateTable(c *C) { } } d := s.dom.DDL() - d.(ddl.DDLForTest).SetHook(callback) + d.SetHook(callback) tk.MustExec("alter table t add index idx1(id)") c.Assert(checkErr, IsNil) tk.MustExec("alter table t add column c int")