Skip to content

Commit

Permalink
executor, ddl: get the correct result of "show create table" when run…
Browse files Browse the repository at this point in the history
…ning "add index" (#6993) (#7243)
  • Loading branch information
zimulala authored and coocood committed Aug 2, 2018
1 parent 3d19f33 commit 50fdc54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
35 changes: 35 additions & 0 deletions ddl/ddl_db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.
Expand Down
16 changes: 8 additions & 8 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
}
Expand Down

0 comments on commit 50fdc54

Please sign in to comment.