From 9e3cee50901f86f31bd97ff302324a2c34f85696 Mon Sep 17 00:00:00 2001 From: Lynn Date: Mon, 25 Feb 2019 14:01:24 +0800 Subject: [PATCH] executor, ddl: get the correct result of "show create table" when add ing the first index to the table (#9388) (#9422) --- ddl/ddl_db_change_test.go | 32 +++++++++++++++++++++----------- executor/show.go | 8 ++++---- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/ddl/ddl_db_change_test.go b/ddl/ddl_db_change_test.go index b8cd82f29ebbb..6854c254359c7 100644 --- a/ddl/ddl_db_change_test.go +++ b/ddl/ddl_db_change_test.go @@ -78,24 +78,34 @@ func (s *testStateChangeSuite) TearDownSuite(c *C) { 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))") + tk.MustExec("create table t (id int)") var checkErr error + testCases := []struct { + sql string + expectedRet string + }{ + {"alter table t add index idx(id)", + "CREATE TABLE `t` (\n `id` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}, + {"alter table t add index idx1(id)", + "CREATE TABLE `t` (\n `id` int(11) DEFAULT NULL,\n KEY `idx` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"}, + {"alter table t add column c int", + "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"}, + } prevState := model.StateNone callback := &ddl.TestDDLCallback{} + currTestCaseOffset := 0 callback.OnJobUpdatedExported = func(job *model.Job) { if job.SchemaState == prevState || checkErr != nil { return } + if job.State == model.JobStateDone { + currTestCaseOffset++ + } 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" - } + expected := testCases[currTestCaseOffset].expectedRet if got != expected { checkErr = errors.Errorf("got %s, expected %s", got, expected) } @@ -105,10 +115,10 @@ func (s *testStateChangeSuite) TestShowCreateTable(c *C) { originalCallback := d.GetHook() defer d.SetHook(originalCallback) 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") - c.Assert(checkErr, IsNil) + for _, tc := range testCases { + tk.MustExec(tc.sql) + c.Assert(checkErr, IsNil) + } } // TestDropNotNullColumn is used to test issue #8654. diff --git a/executor/show.go b/executor/show.go index b327a9684b8f9..c153cfc3964c6 100644 --- a/executor/show.go +++ b/executor/show.go @@ -549,16 +549,16 @@ func (e *ShowExec) fetchShowCreateTable() error { buf.WriteString(fmt.Sprintf(" PRIMARY KEY (%s)", escape(pkCol.Name, sqlMode))) } - if len(tb.Indices()) > 0 { - buf.WriteString(",\n") - } - publicIndices := make([]table.Index, 0, len(tb.Indices())) for _, idx := range tb.Indices() { if idx.Meta().State == model.StatePublic { publicIndices = append(publicIndices, idx) } } + if len(publicIndices) > 0 { + buf.WriteString(",\n") + } + for i, idx := range publicIndices { idxInfo := idx.Meta() if idxInfo.Primary {