Skip to content

Commit c803576

Browse files
aripringleifsnop
authored andcommitted
add support for insert-ignore
1 parent 0083b09 commit c803576

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ $dumpSettingsDefault = array(
128128
'extended-insert' => true,
129129
'events' => false,
130130
'hex-blob' => true, /* faster than escaped content */
131+
'insert-ignore' => false,
131132
'net_buffer_length' => self::MAXLINESIZE,
132133
'no-autocommit' => true,
133134
'no-create-info' => false,
@@ -192,6 +193,8 @@ $this->_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dump
192193
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert
193194
- **hex-blob**
194195
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_hex-blob
196+
- **insert-ignore**
197+
- https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_insert-ignore
195198
- **lock-tables**
196199
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_lock-tables
197200
- **net_buffer_length**

src/Ifsnop/Mysqldump/Mysqldump.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public function __construct(
132132
'extended-insert' => true,
133133
'events' => false,
134134
'hex-blob' => true, /* faster than escaped content */
135+
'insert-ignore' => false,
135136
'net_buffer_length' => self::MAXLINESIZE,
136137
'no-autocommit' => true,
137138
'no-create-info' => false,
@@ -908,19 +909,21 @@ private function listValues($tableName)
908909
$resultSet = $this->dbHandler->query($stmt);
909910
$resultSet->setFetchMode(PDO::FETCH_ASSOC);
910911

912+
$ignore = $this->dumpSettings['insert-ignore'] ? ' IGNORE' : '';
913+
911914
foreach ($resultSet as $row) {
912915
$vals = $this->prepareColumnValues($tableName, $row);
913916
if ($onlyOnce || !$this->dumpSettings['extended-insert']) {
914917

915918
if ($this->dumpSettings['complete-insert']) {
916919
$lineSize += $this->compressManager->write(
917-
"INSERT INTO `$tableName` (".
920+
"INSERT$ignore INTO `$tableName` (".
918921
implode(", ", $colStmt).
919922
") VALUES (".implode(",", $vals).")"
920923
);
921924
} else {
922925
$lineSize += $this->compressManager->write(
923-
"INSERT INTO `$tableName` VALUES (".implode(",", $vals).")"
926+
"INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")"
924927
);
925928
}
926929
$onlyOnce = false;

tests/create_users.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mysql -u root -e "CREATE DATABASE test009;"
1111
mysql -u root -e "CREATE DATABASE test010;"
1212
mysql -u root -e "CREATE DATABASE test011;"
1313
mysql -u root -e "CREATE DATABASE test012;"
14+
mysql -u root -e "CREATE DATABASE test013;"
1415
mysql -u root -e "GRANT ALL PRIVILEGES ON test001.* TO 'travis'@'%' WITH GRANT OPTION;"
1516
mysql -u root -e "GRANT ALL PRIVILEGES ON test002.* TO 'travis'@'%' WITH GRANT OPTION;"
1617
mysql -u root -e "GRANT ALL PRIVILEGES ON test005.* TO 'travis'@'%' WITH GRANT OPTION;"
@@ -21,6 +22,7 @@ mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT O
2122
mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;"
2223
mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO 'travis'@'%' WITH GRANT OPTION;"
2324
mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO 'travis'@'%' WITH GRANT OPTION;"
25+
mysql -u root -e "GRANT ALL PRIVILEGES ON test013.* TO 'travis'@'%' WITH GRANT OPTION;"
2426
mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';"
2527
mysql -u root -e "GRANT SELECT ON mysql.proc to 'travis'@'%';"
2628
mysql -u root -e "FLUSH PRIVILEGES;"

tests/delete_users.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ mysql -e "DROP DATABASE test006a;"
77
mysql -e "DROP DATABASE test006b;"
88
mysql -e "DROP DATABASE test008;"
99
mysql -e "DROP DATABASE test009;"
10+
mysql -e "DROP DATABASE test010;"
11+
mysql -e "DROP DATABASE test011;"
12+
mysql -e "DROP DATABASE test012;"
13+
mysql -e "DROP DATABASE test013;"
1014

1115
mysql -e "DROP USER 'travis'";
1216

tests/test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,12 @@
133133
));
134134
$dump->start("mysqldump-php_test012_no-definer.sql");
135135

136+
print "starting mysql-php_test013.sql" . PHP_EOL;
137+
$dump = new IMysqldump\Mysqldump(
138+
"mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test013",
139+
"travis",
140+
"",
141+
array("insert-ignore" => true));
142+
$dump->start("mysqldump-php_test013.sql");
143+
136144
exit(0);

tests/test.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mysql -utravis < test009.src.sql; ret[((index++))]=$?
3333
mysql -utravis < test010.src.sql; ret[((index++))]=$?
3434
mysql -utravis < test011.src.sql; ret[((index++))]=$?
3535
mysql -utravis < test012.src.sql; ret[((index++))]=$?
36+
mysql -utravis < test013.src.sql; ret[((index++))]=$?
3637

3738
checksum_test001 > test001.src.checksum
3839
checksum_test002 > test002.src.checksum
@@ -69,6 +70,14 @@ mysqldump -utravis test012 \
6970
> mysqldump_test012.sql
7071
ret[((index++))]=$?
7172

73+
mysqldump -utravis test013 \
74+
--no-autocommit \
75+
--extended-insert=false \
76+
--hex-blob=true \
77+
--insert-ignore \
78+
> mysqldump_test013.sql
79+
ret[((index++))]=$?
80+
7281
php test.php || { echo "ERROR running test.php" && exit -1; }
7382
ret[((index++))]=$?
7483

@@ -93,10 +102,12 @@ cat test005.src.sql | grep ^INSERT > test005.filtered.sql
93102
cat test008.src.sql | grep FOREIGN > test008.filtered.sql
94103
cat test010.src.sql | grep CREATE | grep EVENT > test010.filtered.sql
95104
cat test011.src.sql | egrep "INSERT|GENERATED" > test011.filtered.sql
105+
cat test013.src.sql | egrep "INSERT" > test013.filtered.sql
96106
cat mysqldump_test001.sql | grep ^INSERT > mysqldump_test001.filtered.sql
97107
cat mysqldump_test002.sql | grep ^INSERT > mysqldump_test002.filtered.sql
98108
cat mysqldump_test005.sql | grep ^INSERT > mysqldump_test005.filtered.sql
99109
cat mysqldump_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'TRIGGER' | grep -v -e 'TABLE' -e 'CREATE VIEW' > mysqldump_test012.filtered.sql
110+
cat mysqldump_test013.sql | grep "INSERT" > mysqldump_test013.filtered.sql
100111
cat mysqldump-php_test001.sql | grep ^INSERT > mysqldump-php_test001.filtered.sql
101112
cat mysqldump-php_test002.sql | grep ^INSERT > mysqldump-php_test002.filtered.sql
102113
cat mysqldump-php_test005.sql | grep ^INSERT > mysqldump-php_test005.filtered.sql
@@ -105,6 +116,7 @@ cat mysqldump-php_test010.sql | grep CREATE | grep EVENT > mysqldump-php_test010
105116
cat mysqldump-php_test011a.sql | egrep "INSERT|GENERATED" > mysqldump-php_test011a.filtered.sql
106117
cat mysqldump-php_test011b.sql | egrep "INSERT|GENERATED" > mysqldump-php_test011b.filtered.sql
107118
cat mysqldump-php_test012.sql | grep -E -e '50001 (CREATE|VIEW)' -e '50013 DEFINER' -e 'CREATE.*TRIGGER' > mysqldump-php_test012.filtered.sql
119+
cat mysqldump-php_test013.sql | grep INSERT > mysqldump-php_test013.filtered.sql
108120

109121
diff test001.filtered.sql mysqldump_test001.filtered.sql
110122
ret[((index++))]=$?
@@ -151,14 +163,18 @@ ret[((index++))]=$?
151163
! grep 'DEFINER' mysqldump-php_test012_no-definer.sql
152164
ret[((index++))]=$?
153165

166+
# test INSERT IGNORE
167+
diff mysqldump_test013.filtered.sql mysqldump-php_test013.filtered.sql
168+
ret[((index++))]=$?
169+
154170
rm *.checksum 2> /dev/null
155171
rm *.filtered.sql 2> /dev/null
156172
rm mysqldump* 2> /dev/null
157173

158174
echo "Done $index tests"
159175

160176
retvalue=0
161-
for i in $(seq 0 35) ; do
177+
for i in $(seq 0 $index) ; do
162178
if [[ ${ret[$i]} -ne 0 ]]; then
163179
echo "test $i returned ${ret[$i]}"
164180
retvalue=${ret[$i]}

tests/test013.src.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DROP DATABASE IF EXISTS `test013`;
2+
CREATE DATABASE `test013`;
3+
USE `test013`;
4+
5+
DROP TABLE IF EXISTS test;
6+
CREATE TABLE test
7+
(
8+
col INT
9+
);
10+
11+
INSERT INTO `test` (`col`) VALUES (1);
12+

0 commit comments

Comments
 (0)