Skip to content

Commit

Permalink
executor, ddl: get the correct result of "show create table" when add…
Browse files Browse the repository at this point in the history
… ing the first index to the table (#9388) (#9422)
  • Loading branch information
zimulala authored and zz-jason committed Feb 25, 2019
1 parent 08cde60 commit 9e3cee5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
32 changes: 21 additions & 11 deletions ddl/ddl_db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9e3cee5

Please sign in to comment.