From e830b74cb19dd740c2bfb60f89c21231efb0ef97 Mon Sep 17 00:00:00 2001 From: Carlos Cima Date: Mon, 11 Apr 2016 19:19:57 -0400 Subject: [PATCH] Adding option to ignore tables based in regular expressions. --- README.md | 8 ++++++-- src/Command/DiffCommand.php | 23 ++++++++++++++++++++--- src/Command/MigrateCommand.php | 23 ++++++++++++++++++++--- src/Differ.php | 30 +++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4532634..e27c404 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,13 @@ I chose to work with database creation scripts instead of working by connecting ##### Diff ``` -$ php-mysql-diff diff +$ php-mysql-diff diff [-i ] ``` 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: ``` @@ -100,11 +102,13 @@ Diff completed! ##### Migration Script ``` -$ php-mysql-diff migrate [-o ] +$ php-mysql-diff migrate [-o ] [-i ] ``` 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: diff --git a/src/Command/DiffCommand.php b/src/Command/DiffCommand.php index e54403e..4dcb0b0 100644 --- a/src/Command/DiffCommand.php +++ b/src/Command/DiffCommand.php @@ -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' + ) ; } @@ -48,12 +54,23 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!file_exists($from)) { $this->outputLine('' . sprintf('File not found: %s', $from) . ''); - exit; + exit(1); } if (!file_exists($to)) { $this->outputLine('' . sprintf('File not found: %s', $to) . ''); - exit; + exit(1); + } + + $ignoreTables = []; + if ($input->getOption('ignore')) { + $ignoreListFile = $input->getOption('ignore'); + if (!file_exists($ignoreListFile)) { + $this->outputLine('' . sprintf('File not found: %s', $ignoreListFile) . ''); + exit(1); + } + + $ignoreTables = file($ignoreListFile); } $parser = new Parser(); @@ -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(' '); $this->outputLine(); diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index 7ed8cdd..294781a 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -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' + ) ; } @@ -54,12 +60,23 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!file_exists($from)) { $this->outputLine('' . sprintf('File not found: %s', $from) . ''); - exit; + exit(1); } if (!file_exists($to)) { $this->outputLine('' . sprintf('File not found: %s', $to) . ''); - exit; + exit(1); + } + + $ignoreTables = []; + if ($input->getOption('ignore')) { + $ignoreListFile = $input->getOption('ignore'); + if (!file_exists($ignoreListFile)) { + $this->outputLine('' . sprintf('File not found: %s', $ignoreListFile) . ''); + exit(1); + } + + $ignoreTables = file($ignoreListFile); } $parser = new Parser(); @@ -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(' '); if ($databaseDiff->isEmptyDifferences()) { diff --git a/src/Differ.php b/src/Differ.php index 8b35c92..620e57e 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -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; @@ -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); } @@ -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; + } }