Skip to content

Commit

Permalink
Make use of the Symfony Console.
Browse files Browse the repository at this point in the history
  • Loading branch information
dritter committed Oct 25, 2015
1 parent f900e6d commit 3112f1d
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 82 deletions.
16 changes: 3 additions & 13 deletions bin/slimdump
@@ -1,12 +1,6 @@
#!/usr/bin/env php
<?php

array_shift($_SERVER['argv']);

if (!$_SERVER['argv']) {
fail("Usage: slimdump {DSN} {config.xml ...}");
}

$possibleAutoloadFiles = array(
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
Expand All @@ -18,11 +12,7 @@ foreach ($possibleAutoloadFiles as $autoloadFile) {
}


use \Webfactory\Slimdump\Config\ConfigBuilder;
use \Webfactory\Slimdump\Slimdump;

$db = connect(array_shift($_SERVER['argv']));
use \Webfactory\Slimdump\SlimdumpApplication;

$config = ConfigBuilder::createConfigurationFromConsecutiveFiles($_SERVER['argv']);
$slimdump = new Slimdump();
$slimdump->dump($config, $db);
$application = new SlimdumpApplication();
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -19,7 +19,8 @@
"bin/slimdump.php"
],
"require": {
"zendframework/zendframework1": "~1.11"
"zendframework/zendframework1": "~1.11",
"symfony/console": "^2.7"
},
"autoload": {
"files": [
Expand Down
61 changes: 59 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 38 additions & 21 deletions src/Webfactory/Slimdump/Database/Dumper.php
Expand Up @@ -2,24 +2,34 @@

namespace Webfactory\Slimdump\Database;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Webfactory\Slimdump\Config\Table;

class Dumper
{

/** @var OutputInterface */
protected $output;

public function __construct(OutputInterface $output)
{
$this->output = $output;
}

public function exportAsUTF8()
{
print "SET NAMES utf8;\n";
$this->output->writeln("SET NAMES utf8;");
}

public function disableForeignKeys()
{
print "SET FOREIGN_KEY_CHECKS = 0;\n\n";
$this->output->writeln("SET FOREIGN_KEY_CHECKS = 0;\n");
}

public function enableForeignKeys()
{
print "\nSET FOREIGN_KEY_CHECKS = 1;\n";
$this->output->writeln("\nSET FOREIGN_KEY_CHECKS = 1;");
}

/**
Expand All @@ -28,9 +38,9 @@ public function enableForeignKeys()
*/
public function dumpSchema($table, $db)
{
print "-- BEGIN STRUCTURE $table \n";
print "DROP TABLE IF EXISTS `$table`;\n";
print $db->query("SHOW CREATE TABLE `$table`")->fetchColumn(1) . ";\n\n";
$this->output->writeln("-- BEGIN STRUCTURE $table");
$this->output->writeln("DROP TABLE IF EXISTS `$table`;");
$this->output->writeln($db->query("SHOW CREATE TABLE `$table`")->fetchColumn(1) . ";\n");
}

/**
Expand Down Expand Up @@ -58,59 +68,66 @@ public function dumpData($table, Table $tableConfig, $db)
}
$s .= " FROM `$table`";

print "-- BEGIN DATA $table \n";
$this->output->writeln("-- BEGIN DATA $table");

$bufferSize = 0;
$max = 100 * 1024 * 1024; // 100 MB
$numRows = $db->fetchOne("SELECT COUNT(*) FROM $table");
$count = 0;

$progress = new ProgressBar($this->output, $numRows);
$progress->setFormat("Dumping <fg=cyan>$table</>: <fg=yellow>%percent:3s%%</> %remaining%/%estimated%");
$progress->setOverwrite(true);
$progress->setRedrawFrequency(1);
$progress->start();

$db->getConnection()->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

foreach ($db->query($s) as $row) {

fprintf(STDERR, "\rDumping $table: %3u%%", (100 * ++$count) / $numRows);

$b = $this->rowLengthEstimate($row);

// Start a new statement to ensure that the line does not get too long.
if ($bufferSize && $bufferSize + $b > $max) {
print ";\n";
$this->output->writeln(";");
$bufferSize = 0;
}

if ($bufferSize == 0) {
print $this->insertValuesStatement($table, $cols);
$this->output->write($this->insertValuesStatement($table, $cols));
} else {
print ",";
$this->output->write(",");
}

$firstCol = true;
print "\n(";
$this->output->write("\n(");

foreach ($row as $name => $value) {
$isBlobColumn = $this->isBlob($name, $cols);

if (!$firstCol) {
print ", ";
$this->output->write(", ");
}

print $tableConfig->getStringForInsertStatement($name, $value, $isBlobColumn, $db);
$this->output->write($tableConfig->getStringForInsertStatement($name, $value, $isBlobColumn, $db));
$firstCol = false;
}
print ")";
$this->output->write(")");
$bufferSize += $b;
$progress->advance();
}
$progress->setFormat("Dumping <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.
}

$db->getConnection()->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

if ($bufferSize) {
print ";\n";
$this->output->writeln(";");
}

fputs(STDERR, "\n");

print "\n";
$this->output->writeln('');
}

/**
Expand Down
39 changes: 0 additions & 39 deletions src/Webfactory/Slimdump/Slimdump.php

This file was deleted.

50 changes: 50 additions & 0 deletions src/Webfactory/Slimdump/SlimdumpApplication.php
@@ -0,0 +1,50 @@
<?php

namespace Webfactory\Slimdump;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;

class SlimdumpApplication extends Application
{
/**
* Gets the name of the command based on input.
*
* @param InputInterface $input The input interface
*
* @return string The command name
*/
protected function getCommandName(InputInterface $input)
{
return 'slimdump:dump';
}

/**
* Gets the default commands that should always be available.
*
* @return array An array of default Command instances
*/
protected function getDefaultCommands()
{
// Keep the core default commands to have the HelpCommand
// which is used when using the --help option
$defaultCommands = parent::getDefaultCommands();

$defaultCommands[] = new SlimdumpCommand();

return $defaultCommands;
}

/**
* Overridden so that the application doesn't expect the command
* name to be the first argument.
*/
public function getDefinition()
{
$inputDefinition = parent::getDefinition();
// clear out the normal first argument, which is the command name
$inputDefinition->setArguments();

return $inputDefinition;
}
}

0 comments on commit 3112f1d

Please sign in to comment.