Example #1
0
 /**
  * @param            $table
  * @param Table      $tableConfig
  * @param Connection $db
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 public function dumpData($table, Table $tableConfig, Connection $db)
 {
     $this->keepalive($db);
     $cols = $this->cols($table, $db);
     $s = "SELECT ";
     $first = true;
     foreach (array_keys($cols) as $name) {
         $isBlobColumn = $this->isBlob($name, $cols);
         if (!$first) {
             $s .= ', ';
         }
         $s .= $tableConfig->getSelectExpression($name, $isBlobColumn);
         $s .= " AS `{$name}`";
         $first = false;
     }
     $s .= " FROM `{$table}`";
     $s .= $tableConfig->getCondition();
     $this->output->writeln("-- BEGIN DATA {$table}", OutputInterface::OUTPUT_RAW);
     $bufferSize = 0;
     $max = 100 * 1024 * 1024;
     // 100 MB
     $numRows = $db->fetchColumn("SELECT COUNT(*) FROM {$table}");
     if ($numRows == 0) {
         // Fail fast: No data to dump.
         return;
     }
     $progress = new ProgressBar($this->output, $numRows);
     $progress->setFormat("Dumping data <fg=cyan>{$table}</>: <fg=yellow>%percent:3s%%</> %remaining%/%estimated%");
     $progress->setOverwrite(true);
     $progress->setRedrawFrequency(max($numRows / 100, 1));
     $progress->start();
     /** @var PDOConnection $wrappedConnection */
     $wrappedConnection = $db->getWrappedConnection();
     $wrappedConnection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
     foreach ($db->query($s) as $row) {
         $b = $this->rowLengthEstimate($row);
         // Start a new statement to ensure that the line does not get too long.
         if ($bufferSize && $bufferSize + $b > $max) {
             $this->output->writeln(";", OutputInterface::OUTPUT_RAW);
             $bufferSize = 0;
         }
         if ($bufferSize == 0) {
             $this->output->write($this->insertValuesStatement($table, $cols), false, OutputInterface::OUTPUT_RAW);
         } else {
             $this->output->write(",", false, OutputInterface::OUTPUT_RAW);
         }
         $firstCol = true;
         $this->output->write("\n(", false, OutputInterface::OUTPUT_RAW);
         foreach ($row as $name => $value) {
             $isBlobColumn = $this->isBlob($name, $cols);
             if (!$firstCol) {
                 $this->output->write(", ", false, OutputInterface::OUTPUT_RAW);
             }
             $this->output->write($tableConfig->getStringForInsertStatement($name, $value, $isBlobColumn, $db), false, OutputInterface::OUTPUT_RAW);
             $firstCol = false;
         }
         $this->output->write(")", false, OutputInterface::OUTPUT_RAW);
         $bufferSize += $b;
         $progress->advance();
     }
     $progress->setFormat("Dumping data <fg=green>{$table}</>: <fg=green>%percent:3s%%</> Took: %elapsed%");
     $progress->finish();
     if ($this->output instanceof \Symfony\Component\Console\Output\ConsoleOutput) {
         $this->output->getErrorOutput()->write("\n");
         // write a newline after the progressbar.
     }
     $wrappedConnection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
     if ($bufferSize) {
         $this->output->writeln(";", OutputInterface::OUTPUT_RAW);
     }
     $this->output->writeln('', OutputInterface::OUTPUT_RAW);
 }