Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Congratulations you have installed and ran DBDiff!
- **--config=config.yaml** - By default, DBDiff will look for a `.dbdiff` file in the current directory which is valid YAML, which may also be overridden with a config file that lists the database host, user, port and password of the source and target DBs in YAML format (instead of using the command line for it), or any of the other settings e.g. the format, template, type, include, nocomments. Please note: a command-line parameter will always override any config file.
- **server1.db1.table1:server2.db2.table3** or **server1.db1:server2.db2** - The penultimate parameter is what to compare. This tool can compare just one table or all tables (entire db) from the database
- **--output=./output-dir/today-up-schema.sql** - The last parameter is an output file and/or directory to output the diff to, which by default will output to the same directory the command is run in if no directory is specified. If a directory is specified, it should exist, otherwise an error will be thrown. If this path is not specified, the default file name becomes migration.sql in the current directory
- **--prefix=default** - Specifies the table prefix to use, will also add to tablesToIgnore e.g. default_table1

# Usage Examples

Expand Down Expand Up @@ -129,6 +130,8 @@ Instead of looking for `.dbdiff`, this would look for `config.conf` (which shou
- table1
- table2
- table3
tablesDataToIgnore:
- table1
fieldsToIgnore:
table1:
- field1
Expand Down
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dbdiff/dbdiff",
"name": "mbsauce/dbdiff",
"type": "project",
"license": "MIT",
"bin": ["./dbdiff"],
Expand All @@ -10,10 +10,6 @@
},
"require": {
"aura/cli": "2.*@dev",
"symfony/yaml": "2.8.*@dev",
"illuminate/database": "4.*",
"illuminate/view": "4.*",
"illuminate/container": "4.*",
"diff/diff": "2.0.*@dev"
},
"require-dev": {
Expand Down
46 changes: 46 additions & 0 deletions src/DB/Data/DBData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ function getDiff() {
$sourceTables = $this->manager->getTables('source');
$targetTables = $this->manager->getTables('target');

//Only get tables that start with a certain prefix, useful on shared DB
if (isset($params->prefix)) {

$prefix = $params->prefix.'_';

$sourceTables = preg_grep('/^'.$prefix.'.*/i', $sourceTables);
$targetTables = preg_grep('/^'.$prefix.'.*/i', $targetTables);

$tables = array();

foreach($params->tablesToIgnore as $table) {

$tables[] = $prefix.$table;
}

$params->tablesToIgnore = array_merge($params->tablesToIgnore, $tables);
}

//Only certain Data Tables
if (isset($params->tablesDataToIgnore)) {

$sourceTables = $this->ignoreDataTables($sourceTables, $params->tablesDataToIgnore);
$targetTables = $this->ignoreDataTables($targetTables, $params->tablesDataToIgnore);
}

if (isset($params->tablesToIgnore)) {
$sourceTables = array_diff($sourceTables, $params->tablesToIgnore);
$targetTables = array_diff($targetTables, $params->tablesToIgnore);
Expand Down Expand Up @@ -53,4 +78,25 @@ function getDiff() {
return $diffSequence;
}

function ignoreDataTables($tables_array, $exclude_array) {

$tables = array();

$pattern = implode('|', $exclude_array);

foreach($tables_array as $table) {

if(preg_match('/.*'.$pattern.'.*/', $table)) {

//skip

} else {

$tables[] = $table;
}
}

return $tables;
}

}
4 changes: 2 additions & 2 deletions src/DB/Data/LocalTableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function getChangeDiff($table, $key) {
$this->source->setFetchMode(\PDO::FETCH_NAMED);
$result = $this->source->select(
"SELECT * FROM (
SELECT $columnsAas, $columnsBas, MD5(concat($columnsA)) AS hash1,
MD5(concat($columnsB)) AS hash2 FROM {$db1}.{$table} as a
SELECT $columnsAas, $columnsBas, MD5(concat_ws('~', $columnsA)) AS hash1,
MD5(concat_ws('~', $columnsB)) AS hash2 FROM {$db1}.{$table} as a
INNER JOIN {$db2}.{$table} as b
ON $keyCols
) t WHERE hash1 <> hash2");
Expand Down
4 changes: 3 additions & 1 deletion src/Params/CLIGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getParams() {
$getopt = $context->getopt([
'server1::', 'server2::', 'format::',
'template::', 'type::', 'include::',
'nocomments::', 'config::', 'output::', 'debug::'
'nocomments::', 'config::', 'output::', 'debug::', 'prefix::'
]);

$input = $getopt->get(1);
Expand Down Expand Up @@ -45,6 +45,8 @@ public function getParams() {
$params->output = $getopt->get('--output');
if ($getopt->get('--debug'))
$params->debug = $getopt->get('--debug');
if ($getopt->get('--prefix'))
$params->prefix = $getopt->get('--prefix');

return $params;
}
Expand Down