Skip to content

Commit

Permalink
Add option to avoid dumping AUTO_INCREMENT values (#14)
Browse files Browse the repository at this point in the history
A new "keep-auto-increment" attribute has been added within <table> configuration statements.

Setting it to "false" (with the default being "true") will remove the "AUTO_INCREMENT=..." value that MySQL's "SHOW CREATE TABLE" will insert by default. As a result, you can create schema dumps that will start off with fresh AUTO_INCREMENT values.
  • Loading branch information
mpdude committed Jun 27, 2016
1 parent 5190b6c commit 2b4d55d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ Example:
<!-- Dump the "media" table, omit BLOB fields. -->
<table name="media" dump="noblob" />

<!-- Dump the "users" table, hide names and email addresses. -->
<!-- Dump the "user" table, hide names and email addresses. -->
<table name="user" dump="full">
<column name="username" dump="masked" />
<column name="email" dump="masked" />
<column name="password" dump="replace" replacement="test" />
</table>

<!-- Dump the "document" table but do not pass the "AUTO_INCREMENT" parameter to the SQL query.
Instead start to increment from the beginning -->
<table name="document" dump="full" keep-auto-increment="false" />
</slimdump>
```

Expand Down
26 changes: 26 additions & 0 deletions src/Webfactory/Slimdump/Config/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Table
{
private $selector;
private $dump;

/** @var boolean */
private $keepAutoIncrement;

/** @var \SimpleXMLElement */
private $config;

Expand All @@ -41,12 +45,26 @@ public function __construct(\SimpleXMLElement $config)
throw new InvalidDumpTypeException(sprintf("Invalid dump type %s for table %s.", $attr->dump, $this->selector));
}

$this->keepAutoIncrement = self::attributeToBoolean($attr->{'keep-auto-increment'}, true);

foreach ($config->column as $columnConfig) {
$column = new Column($columnConfig);
$this->columns[$column->getSelector()] = $column;
}
}

/**
* @param \SimpleXMLElement[]|null $attribute
* @param boolean $defaultValue
* @return boolean
*/
private static function attributeToBoolean($attribute, $defaultValue) {
if ($attribute == null) {
return $defaultValue;
}
return ($attribute == 'true') ? true : false;
}

/**
* @return string
*/
Expand All @@ -71,6 +89,14 @@ public function isDataDumpRequired()
return $this->dump >= Config::NOBLOB;
}

/**
* @return boolean
*/
public function keepAutoIncrement()
{
return $this->keepAutoIncrement;
}

/**
* @param string $columnName
* @param boolean $isBlobColumn
Expand Down
19 changes: 13 additions & 6 deletions src/Webfactory/Slimdump/Database/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ public function enableForeignKeys()
}

/**
* @param $table
* @param Connection $db
* @param $table
* @param Connection $db
* @param boolean $keepAutoIncrement
*/
public function dumpSchema($table, Connection $db)
public function dumpSchema($table, Connection $db, $keepAutoIncrement = true)
{
$this->keepalive($db);
$this->output->writeln("-- BEGIN STRUCTURE $table", OutputInterface::OUTPUT_RAW);
$this->output->writeln("DROP TABLE IF EXISTS `$table`;", OutputInterface::OUTPUT_RAW);

$this->output->writeln($db->fetchColumn("SHOW CREATE TABLE `$table`", array(), 1).";\n", OutputInterface::OUTPUT_RAW);
$tableCreationCommand = $db->fetchColumn("SHOW CREATE TABLE `$table`", array(), 1);

if (!$keepAutoIncrement) {
$tableCreationCommand = preg_replace('/ AUTO_INCREMENT=[0-9]*/', '', $tableCreationCommand);
}

$this->output->writeln($tableCreationCommand.";\n", OutputInterface::OUTPUT_RAW);

$progress = new ProgressBar($this->output, 1);
$format = "Dumping schema <fg=cyan>$table</>: <fg=yellow>%percent:3s%%</>";
Expand Down Expand Up @@ -205,8 +212,8 @@ protected function rowLengthEstimate(array $row)
}
return $l;
}
private function keepalive(Connection $db)

private function keepalive(Connection $db)
{
if (false === $db->ping()) {
$db->close();
Expand Down
4 changes: 3 additions & 1 deletion src/Webfactory/Slimdump/SlimdumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
/**
* @param Config $config
* @param Connection $db
* @param OutputInterface $output
* @throws \Doctrine\DBAL\DBALException
*/
public function dump(Config $config, Connection $db, OutputInterface $output)
{
Expand All @@ -63,7 +65,7 @@ public function dump(Config $config, Connection $db, OutputInterface $output)
}

if ($tableConfig->isSchemaDumpRequired()) {
$dumper->dumpSchema($tableName, $db);
$dumper->dumpSchema($tableName, $db, $tableConfig->keepAutoIncrement());

if ($tableConfig->isDataDumpRequired()) {
$dumper->dumpData($tableName, $tableConfig, $db);
Expand Down
23 changes: 23 additions & 0 deletions test/Webfactory/Slimdump/Config/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,27 @@ public function testSelectConditionWhenConditionIsEmpty()
$this->assertEquals('', $table->getCondition());
}

public function testKeepAutoIncrementWhenNotSet()
{
$xml = '<?xml version="1.0" ?>
<table name="dicht*" dump="full" />';

$xmlElement = new \SimpleXMLElement($xml);

$table = new Table($xmlElement);

$this->assertTrue($table->keepAutoIncrement());
}

public function testKeepAutoIncrementWhenSet()
{
$xml = '<?xml version="1.0" ?>
<table name="dicht*" dump="full" keep-auto-increment="false" />';

$xmlElement = new \SimpleXMLElement($xml);

$table = new Table($xmlElement);

$this->assertFalse($table->keepAutoIncrement());
}
}

0 comments on commit 2b4d55d

Please sign in to comment.