Skip to content

Commit

Permalink
*: fix show create table bug (pingcap#1689)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenli authored Sep 6, 2016
1 parent 9d2f62a commit 9257a41
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ func (e *ShowExec) fetchShowCreateTable() error {
if pkCol != nil {
// If PKIsHanle, pk info is not in tb.Indices(). We should handle it here.
buf.WriteString(",\n")
buf.WriteString(fmt.Sprintf(" PRIMARY KEY (`%s`) ", pkCol.Name.O))
buf.WriteString(fmt.Sprintf(" PRIMARY KEY (`%s`)", pkCol.Name.O))
}

if len(tb.Indices()) > 0 {
if len(tb.Indices()) > 0 || len(tb.Meta().ForeignKeys) > 0 {
buf.WriteString(",\n")
}

Expand All @@ -438,12 +438,15 @@ func (e *ShowExec) fetchShowCreateTable() error {
}
}

if len(tb.Indices()) > 0 && len(tb.Meta().ForeignKeys) > 0 {
buf.WriteString(",\n")
}

for _, fk := range tb.Meta().ForeignKeys {
if fk.State != model.StatePublic {
continue
}

buf.WriteString("\n")
cols := make([]string, 0, len(fk.Cols))
for _, c := range fk.Cols {
cols = append(cols, c.L)
Expand Down
8 changes: 4 additions & 4 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *testSuite) TestShow(c *C) {
row := result.Rows()[0]
// For issue https://github.com/pingcap/tidb/issues/1061
expectedRow := []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n PRIMARY KEY (`id`) \n) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28934 COMMENT='table_comment'"}
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=28934 COMMENT='table_comment'"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *testSuite) TestForeignKeyInShowCreateTable(c *C) {
c.Check(result.Rows(), HasLen, 1)
row := result.Rows()[0]
expectedRow := []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"}
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`),\n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand All @@ -129,7 +129,7 @@ func (s *testSuite) TestForeignKeyInShowCreateTable(c *C) {
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n) ENGINE=InnoDB"}
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand All @@ -141,7 +141,7 @@ func (s *testSuite) TestForeignKeyInShowCreateTable(c *C) {
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`) \n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"}
"show_test", "CREATE TABLE `show_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n PRIMARY KEY (`id`),\n CONSTRAINT `fk` FOREIGN KEY (`id`) REFERENCES `t1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE\n) ENGINE=InnoDB"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand Down

0 comments on commit 9257a41

Please sign in to comment.