diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index d5dbc68d..106e1dcb 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -299,7 +299,7 @@ public function restore($path) $buffer = ''; while ( !feof($handle) ) { - $line = trim(fgets($handle)); + $line = fgets($handle); if (substr($line, 0, 2) == '--' || !$line) { continue; // skip comments @@ -433,6 +433,12 @@ public function start($filename = '') // Write some basic info to output file $this->compressManager->write($this->getDumpFileHeader()); + // initiate a transaction at global level to create a consistent snapshot + if ($this->dumpSettings['single-transaction']) { + $this->dbHandler->exec($this->typeAdapter->setup_transaction()); + $this->dbHandler->exec($this->typeAdapter->start_transaction()); + } + // Store server settings and use sanner defaults to dump $this->compressManager->write( $this->typeAdapter->backup_parameters() @@ -484,6 +490,12 @@ public function start($filename = '') $this->compressManager->write( $this->typeAdapter->restore_parameters() ); + + // end transaction + if ($this->dumpSettings['single-transaction']) { + $this->dbHandler->exec($this->typeAdapter->commit_transaction()); + } + // Write some stats to output file. $this->compressManager->write($this->getDumpFileFooter()); // Close output file. @@ -694,6 +706,8 @@ private function matches($table, $arr) */ private function exportTables() { + + // Exporting tables one by one foreach ($this->tables as $table) { if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { @@ -1131,7 +1145,6 @@ private function listValues($tableName) $this->prepareListValues($tableName); $onlyOnce = true; - $lineSize = 0; // colStmt is used to form a query to obtain row values $colStmt = $this->getColumnStmt($tableName); @@ -1161,35 +1174,33 @@ private function listValues($tableName) $ignore = $this->dumpSettings['insert-ignore'] ? ' IGNORE' : ''; $count = 0; + $line = ''; foreach ($resultSet as $row) { $count++; $vals = $this->prepareColumnValues($tableName, $row); if ($onlyOnce || !$this->dumpSettings['extended-insert']) { if ($this->dumpSettings['complete-insert']) { - $lineSize += $this->compressManager->write( - "INSERT$ignore INTO `$tableName` (". + $line .= "INSERT$ignore INTO `$tableName` (". implode(", ", $colNames). - ") VALUES (".implode(",", $vals).")" - ); + ") VALUES (".implode(",", $vals).")"; } else { - $lineSize += $this->compressManager->write( - "INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")" - ); + $line .= "INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")"; } $onlyOnce = false; } else { - $lineSize += $this->compressManager->write(",(".implode(",", $vals).")"); + $line .= ",(".implode(",", $vals).")"; } - if (($lineSize > $this->dumpSettings['net_buffer_length']) || + if ((strlen($line) > $this->dumpSettings['net_buffer_length']) || !$this->dumpSettings['extended-insert']) { $onlyOnce = true; - $lineSize = $this->compressManager->write(";".PHP_EOL); + $this->compressManager->write($line . ";".PHP_EOL); + $line = ''; } } $resultSet->closeCursor(); - if (!$onlyOnce) { - $this->compressManager->write(";".PHP_EOL); + if ('' !== $line) { + $this->compressManager->write($line. ";".PHP_EOL); } $this->endListValues($tableName, $count); @@ -1216,11 +1227,6 @@ public function prepareListValues($tableName) ); } - if ($this->dumpSettings['single-transaction']) { - $this->dbHandler->exec($this->typeAdapter->setup_transaction()); - $this->dbHandler->exec($this->typeAdapter->start_transaction()); - } - if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { $this->typeAdapter->lock_table($tableName); } @@ -1269,10 +1275,6 @@ public function endListValues($tableName, $count = 0) ); } - if ($this->dumpSettings['single-transaction']) { - $this->dbHandler->exec($this->typeAdapter->commit_transaction()); - } - if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { $this->typeAdapter->unlock_table($tableName); } diff --git a/tests/create_users.sh b/tests/create_users.sh index 124955ee..7dccbbcb 100755 --- a/tests/create_users.sh +++ b/tests/create_users.sh @@ -21,6 +21,7 @@ mysql -u root -e "GRANT ALL PRIVILEGES ON test009.* TO 'travis'@'%' WITH GRANT O mysql -u root -e "GRANT ALL PRIVILEGES ON test010.* TO 'travis'@'%' WITH GRANT OPTION;" mysql -u root -e "GRANT ALL PRIVILEGES ON test011.* TO 'travis'@'%' WITH GRANT OPTION;" mysql -u root -e "GRANT ALL PRIVILEGES ON test012.* TO 'travis'@'%' WITH GRANT OPTION;" +mysql -u root -e "GRANT ALL PRIVILEGES ON test014.* TO 'travis'@'%' WITH GRANT OPTION;" mysql -u root -e "GRANT SUPER,LOCK TABLES ON *.* TO 'travis'@'%';" mysql -u root -e "GRANT SELECT ON mysql.proc to 'travis'@'%';" mysql -u root -e "FLUSH PRIVILEGES;" diff --git a/tests/test.php b/tests/test.php index 49a6f4ff..f806b29b 100644 --- a/tests/test.php +++ b/tests/test.php @@ -162,4 +162,17 @@ )); $dump->start("mysqldump-php_test013.sql"); +print "starting mysql-php_test014.sql" . PHP_EOL; +$dump = new IMysqldump\Mysqldump( + "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=test014", + "travis", + "", + array( + "insert-ignore" => true, + "extended-insert" => true, + )); +$timer=microtime(true); +$dump->start("mysqldump-php_test014.sql"); +print round(microtime(true) - $timer,3) . " seconds" . PHP_EOL; + exit(0); diff --git a/tests/test.sh b/tests/test.sh index 7ad22257..f829f7d3 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -113,6 +113,9 @@ mysqldump -utravis test001 \ > mysqldump_test013.sql errCode=$?; ret[((index++))]=$errCode +#speed test +gzip -dc test014.src.sql.gz | mysql -utravis + php test.php || { echo "ERROR running test.php" && exit -1; } errCode=$?; ret[((index++))]=$errCode diff --git a/tests/test014.src.sql.gz b/tests/test014.src.sql.gz new file mode 100644 index 00000000..69c8b341 Binary files /dev/null and b/tests/test014.src.sql.gz differ