Skip to content

Commit e830b74

Browse files
author
Carlos Cima
committed
Adding option to ignore tables based in regular expressions.
1 parent ab8e44e commit e830b74

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ I chose to work with database creation scripts instead of working by connecting
4545
##### Diff
4646

4747
```
48-
$ php-mysql-diff diff <from> <to>
48+
$ php-mysql-diff diff <from> <to> [-i <ignore-tables-file>]
4949
```
5050

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

53+
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.
54+
5355
The output will be like this:
5456

5557
```
@@ -100,11 +102,13 @@ Diff completed!
100102
##### Migration Script
101103

102104
```
103-
$ php-mysql-diff migrate <from> <to> [-o <output-file>]
105+
$ php-mysql-diff migrate <from> <to> [-o <output-file>] [-i <ignore-tables-file>]
104106
```
105107

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

110+
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.
111+
108112
If the `-o` option is not used, the migration script will be output to the `stdout`.
109113

110114
The output (with the `-o` option) will be like this:

src/Command/DiffCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ protected function configure()
2626
InputArgument::REQUIRED,
2727
'File path of the creation script of the target database'
2828
)
29+
->addOption(
30+
'ignore',
31+
'i',
32+
InputOption::VALUE_OPTIONAL,
33+
'Table ignore list'
34+
)
2935
;
3036
}
3137

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

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

5460
if (!file_exists($to)) {
5561
$this->outputLine('<error>' . sprintf('File not found: %s', $to) . '</error>');
56-
exit;
62+
exit(1);
63+
}
64+
65+
$ignoreTables = [];
66+
if ($input->getOption('ignore')) {
67+
$ignoreListFile = $input->getOption('ignore');
68+
if (!file_exists($ignoreListFile)) {
69+
$this->outputLine('<error>' . sprintf('File not found: %s', $ignoreListFile) . '</error>');
70+
exit(1);
71+
}
72+
73+
$ignoreTables = file($ignoreListFile);
5774
}
5875

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

6986
$this->outputString('• Comparing databases ...........');
7087
$differ = new Differ();
71-
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase);
88+
$databaseDiff = $differ->diffDatabases($fromDatabase, $toDatabase, $ignoreTables);
7289
$this->outputLine(' <info>✓</info>');
7390
$this->outputLine();
7491

src/Command/MigrateCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ protected function configure()
3232
InputOption::VALUE_OPTIONAL,
3333
'Output migration script to a file'
3434
)
35+
->addOption(
36+
'ignore',
37+
'i',
38+
InputOption::VALUE_OPTIONAL,
39+
'Table ignore list'
40+
)
3541
;
3642
}
3743

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

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

6066
if (!file_exists($to)) {
6167
$this->outputLine('<error>' . sprintf('File not found: %s', $to) . '</error>');
62-
exit;
68+
exit(1);
69+
}
70+
71+
$ignoreTables = [];
72+
if ($input->getOption('ignore')) {
73+
$ignoreListFile = $input->getOption('ignore');
74+
if (!file_exists($ignoreListFile)) {
75+
$this->outputLine('<error>' . sprintf('File not found: %s', $ignoreListFile) . '</error>');
76+
exit(1);
77+
}
78+
79+
$ignoreTables = file($ignoreListFile);
6380
}
6481

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

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

8097
if ($databaseDiff->isEmptyDifferences()) {

src/Differ.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ class Differ
1212
/**
1313
* @param Database $fromDatabase
1414
* @param Database $toDatabase
15+
* @param array $ignoreList
1516
*
1617
* @return DatabaseDiff
1718
*/
18-
public function diffDatabases(Database $fromDatabase, Database $toDatabase)
19+
public function diffDatabases(Database $fromDatabase, Database $toDatabase, array $ignoreList = [])
1920
{
2021
$databaseDiff = new DatabaseDiff();
2122

2223
foreach ($fromDatabase->getTables() as $fromTable) {
24+
25+
if ($this->isTableIgnored($fromTable->getName(), $ignoreList)) {
26+
continue;
27+
}
28+
2329
if (!$toDatabase->hasTable($fromTable->getName())) {
2430
$databaseDiff->addDeletedTable($fromTable);
2531
continue;
@@ -34,6 +40,11 @@ public function diffDatabases(Database $fromDatabase, Database $toDatabase)
3440
}
3541

3642
foreach ($toDatabase->getTables() as $toTable) {
43+
44+
if ($this->isTableIgnored($toTable->getName(), $ignoreList)) {
45+
continue;
46+
}
47+
3748
if (!$fromDatabase->hasTable($toTable->getName())) {
3849
$databaseDiff->addNewTable($toTable);
3950
}
@@ -228,4 +239,21 @@ public function generateMigrationScript(DatabaseDiff $databaseDiff)
228239

229240
return $migrationScript;
230241
}
242+
243+
/**
244+
* @param string $tableName
245+
* @param array $ignoreList
246+
*
247+
* @return bool
248+
*/
249+
private function isTableIgnored($tableName, array $ignoreList)
250+
{
251+
foreach ($ignoreList as $ignoreRegExp) {
252+
if (preg_match($ignoreRegExp, $tableName) === 1) {
253+
return true;
254+
}
255+
}
256+
257+
return false;
258+
}
231259
}

0 commit comments

Comments
 (0)