Skip to content

Commit 147292a

Browse files
author
Bharathy Satish
committed
Bug #21907297 MYSQLPUMP CREATES INCORRECT ALTER TABLE STATEMENT FOR FOREIGN KEY
During dump process mysqlpump extracts key columns from table definition and creates an ALTER TABLE syntax. During this parsing of SHOW CREATE TABLE output there was an problem which results in wrong syntax to be dumped, thus causing restore to fail. This patch fixes this issue.
1 parent fd30c18 commit 147292a

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

client/dump/table.cc

+8-17
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ Table::Table(uint64 id, const std::string& name, const std::string& schema,
3232
m_data_lenght(data_lenght)
3333
{
3434
using Detail::Pattern_matcher;
35-
bool pk_present= false;
36-
bool constraint_present= false;
3735

3836
std::vector<std::string> definition_lines;
3937
boost::split(definition_lines, sql_formatted_definition,
@@ -42,6 +40,7 @@ Table::Table(uint64 id, const std::string& name, const std::string& schema,
4240
it != definition_lines.end(); ++it)
4341
{
4442
boost::trim_left(*it);
43+
boost::trim_if(*it, boost::is_any_of(","));
4544
// TODO: Look up INFORMATION_SCHEMA and get the table details.
4645
if (boost::starts_with(*it, "KEY ")
4746
|| boost::starts_with(*it, "INDEX ")
@@ -50,28 +49,20 @@ Table::Table(uint64 id, const std::string& name, const std::string& schema,
5049
|| boost::starts_with(*it, "FULLTEXT KEY ")
5150
|| boost::starts_with(*it, "FULLTEXT INDEX ")
5251
|| boost::starts_with(*it, "SPATIAL KEY ")
53-
|| boost::starts_with(*it, "SPATIAL INDEX "))
52+
|| boost::starts_with(*it, "SPATIAL INDEX ")
53+
|| boost::starts_with(*it, "CONSTRAINT "))
5454
{
55-
pk_present= true;
56-
*it= boost::algorithm::replace_last_copy(*it, "),", ")");
57-
m_indexes_sql_definition.push_back(*it);
58-
}
59-
else if (boost::starts_with(*it, "CONSTRAINT "))
60-
{
61-
constraint_present= true;
62-
*it= boost::algorithm::replace_last_copy(*it, ",", "");
6355
m_indexes_sql_definition.push_back(*it);
6456
}
6557
else
6658
{
67-
if (pk_present || constraint_present)
59+
if ((it+1) == definition_lines.end())
6860
{
69-
if ((it+1) == definition_lines.end())
70-
{
71-
std::string &sql_def = m_sql_definition_without_indexes;
72-
sql_def = boost::algorithm::replace_last_copy(sql_def, ",", "");
73-
}
61+
std::string &sql_def = m_sql_definition_without_indexes;
62+
sql_def = boost::algorithm::replace_last_copy(sql_def, ",", "");
7463
}
64+
else if (it != definition_lines.begin())
65+
*it+= ",";
7566
m_sql_definition_without_indexes+= *it + '\n';
7667
}
7768
}

mysql-test/r/mysqlpump_basic.result

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ FOREIGN KEY (num) REFERENCES t9 (v_id)
6060
ALTER TABLE t10 ADD COLUMN v_id INT NOT NULL AFTER c_id;
6161
ALTER TABLE t10 ADD FOREIGN KEY fk_t9(v_id) REFERENCES
6262
t9(v_id) ON DELETE NO ACTION ON UPDATE CASCADE;
63+
CREATE TABLE t12 (
64+
ID bigint NOT NULL DEFAULT '0',
65+
v bigint NOT NULL,
66+
PRIMARY KEY (ID,v)
67+
);
68+
CREATE TABLE t13 (
69+
ID bigint NOT NULL DEFAULT '0',
70+
k varchar(30) NOT NULL DEFAULT '',
71+
v bigint NOT NULL,
72+
PRIMARY KEY (ID, v, k),
73+
CONSTRAINT relation_tags_ibfk_1 FOREIGN KEY (ID, v) REFERENCES t12 (ID,v)
74+
);
6375
INSERT INTO t4 (name) VALUES ('disk_temptable_create_cost');
6476
INSERT INTO t4 (name) VALUES ('disk_temptable_row_cost');
6577
INSERT INTO t4 (name) VALUES ('key_compare_cost');
@@ -105,6 +117,8 @@ TABLE_NAME
105117
t1
106118
t10
107119
t11
120+
t12
121+
t13
108122
t2
109123
t3
110124
t4
@@ -150,6 +164,8 @@ TABLE_NAME
150164
t1
151165
t10
152166
t11
167+
t12
168+
t13
153169
t2
154170
t3
155171
t4

mysql-test/t/mysqlpump_basic.test

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ ALTER TABLE t10 ADD COLUMN v_id INT NOT NULL AFTER c_id;
7878
ALTER TABLE t10 ADD FOREIGN KEY fk_t9(v_id) REFERENCES
7979
t9(v_id) ON DELETE NO ACTION ON UPDATE CASCADE;
8080

81+
CREATE TABLE t12 (
82+
ID bigint NOT NULL DEFAULT '0',
83+
v bigint NOT NULL,
84+
PRIMARY KEY (ID,v)
85+
);
86+
87+
CREATE TABLE t13 (
88+
ID bigint NOT NULL DEFAULT '0',
89+
k varchar(30) NOT NULL DEFAULT '',
90+
v bigint NOT NULL,
91+
PRIMARY KEY (ID, v, k),
92+
CONSTRAINT relation_tags_ibfk_1 FOREIGN KEY (ID, v) REFERENCES t12 (ID,v)
93+
);
8194

8295
INSERT INTO t4 (name) VALUES ('disk_temptable_create_cost');
8396
INSERT INTO t4 (name) VALUES ('disk_temptable_row_cost');

0 commit comments

Comments
 (0)