Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor, ddl: get the correct result of "show create table" when running "add index" (#6993) #7243

Merged
merged 3 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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