Skip to content

Commit

Permalink
Adding option to ignore tables based in regular expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Cima committed Apr 11, 2016
1 parent ab8e44e commit e830b74
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ I chose to work with database creation scripts instead of working by connecting
##### Diff

```
$ php-mysql-diff diff <from> <to>
$ php-mysql-diff diff <from> <to> [-i <ignore-tables-file>]
```

where `from` is the path to the initial database creation script and `to` is the path to the target database creation script.

Use the `-i` option to ignore tables during comparison. The file format is a list of regular expressions to match the table names to be ignored, one per line.

The output will be like this:

```
Expand Down Expand Up @@ -100,11 +102,13 @@ Diff completed!
##### Migration Script

```
$ php-mysql-diff migrate <from> <to> [-o <output-file>]
$ php-mysql-diff migrate <from> <to> [-o <output-file>] [-i <ignore-tables-file>]
```

where `from` is the path to the initial database creation script and `to` is the path to the target database creation script.

Use the `-i` option to ignore tables during comparison. The file format is a list of regular expressions to match the table names to be ignored, one per line.

If the `-o` option is not used, the migration script will be output to the `stdout`.

The output (with the `-o` option) will be like this:
Expand Down
23 changes: 20 additions & 3 deletions src/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected function configure()
InputArgument::REQUIRED,
'File path of the creation script of the target database'
)
->addOption(
'ignore',
'i',
InputOption::VALUE_OPTIONAL,
'Table ignore list'
)
;
}

Expand All @@ -48,12 +54,23 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!file_exists($from)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $from) . '</error>');
exit;
exit(1);
}

if (!file_exists($to)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $to) . '</error>');
exit;
exit(1);
}

$ignoreTables = [];
if ($input->getOption('ignore')) {
$ignoreListFile = $input->getOption('ignore');
if (!file_exists($ignoreListFile)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $ignoreListFile) . '</error>');
exit(1);
}

$ignoreTables = file($ignoreListFile);
}

$parser = new Parser();
Expand All @@ -68,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->outputString('• Comparing databases ...........');
$differ = new Differ();
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase);
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase, $ignoreTables);
$this->outputLine(' <info>✓</info>');
$this->outputLine();

Expand Down
23 changes: 20 additions & 3 deletions src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
'Output migration script to a file'
)
->addOption(
'ignore',
'i',
InputOption::VALUE_OPTIONAL,
'Table ignore list'
)
;
}

Expand All @@ -54,12 +60,23 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!file_exists($from)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $from) . '</error>');
exit;
exit(1);
}

if (!file_exists($to)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $to) . '</error>');
exit;
exit(1);
}

$ignoreTables = [];
if ($input->getOption('ignore')) {
$ignoreListFile = $input->getOption('ignore');
if (!file_exists($ignoreListFile)) {
$this->outputLine('<error>' . sprintf('File not found: %s', $ignoreListFile) . '</error>');
exit(1);
}

$ignoreTables = file($ignoreListFile);
}

$parser = new Parser();
Expand All @@ -74,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->outputString('• Comparing databases ...........');
$differ = new Differ();
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase);
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase, $ignoreTables);
$this->outputLine(' <info>✓</info>');

if ($databaseDiff->isEmptyDifferences()) {
Expand Down
30 changes: 29 additions & 1 deletion src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ class Differ
/**
* @param Database $fromDatabase
* @param Database $toDatabase
* @param array $ignoreList
*
* @return DatabaseDiff
*/
public function diffDatabases(Database $fromDatabase, Database $toDatabase)
public function diffDatabases(Database $fromDatabase, Database $toDatabase, array $ignoreList = [])
{
$databaseDiff = new DatabaseDiff();

foreach ($fromDatabase->getTables() as $fromTable) {

if ($this->isTableIgnored($fromTable->getName(), $ignoreList)) {
continue;
}

if (!$toDatabase->hasTable($fromTable->getName())) {
$databaseDiff->addDeletedTable($fromTable);
continue;
Expand All @@ -34,6 +40,11 @@ public function diffDatabases(Database $fromDatabase, Database $toDatabase)
}

foreach ($toDatabase->getTables() as $toTable) {

if ($this->isTableIgnored($toTable->getName(), $ignoreList)) {
continue;
}

if (!$fromDatabase->hasTable($toTable->getName())) {
$databaseDiff->addNewTable($toTable);
}
Expand Down Expand Up @@ -228,4 +239,21 @@ public function generateMigrationScript(DatabaseDiff $databaseDiff)

return $migrationScript;
}

/**
* @param string $tableName
* @param array $ignoreList
*
* @return bool
*/
private function isTableIgnored($tableName, array $ignoreList)
{
foreach ($ignoreList as $ignoreRegExp) {
if (preg_match($ignoreRegExp, $tableName) === 1) {
return true;
}
}

return false;
}
}

0 comments on commit e830b74

Please # to comment.