From a421dd934aeb4a1064608bdeca44b5b5986e7358 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 13 Oct 2010 16:58:28 -0700 Subject: [PATCH] Nicer diff display for schema dump --- scripts/dumpschema.php | 82 ++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/scripts/dumpschema.php b/scripts/dumpschema.php index fcf2430d80..cab02d910e 100644 --- a/scripts/dumpschema.php +++ b/scripts/dumpschema.php @@ -24,14 +24,15 @@ $helptext = <<getTableDef($tableName); + } catch (Exception $e) { + // @fixme this is a terrible check :D + if (preg_match('/no such table/', $e->getMessage())) { + return dumpTable($tableName, false); + } else { + throw $e; + } + } - $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b'); - file_put_contents($fnameB, $b); + if ($filter) { + $old = $schema->filterDef($old); + $def = $schema->filterDef($def); + } - $cmd = sprintf('diff -U 100 %s %s', - escapeshellarg($fnameA), - escapeshellarg($fnameB)); - passthru($cmd); + // @hack + $old = tweakPrimaryKey($old); + $def = tweakPrimaryKey($def); - unlink($fnameA); - unlink($fnameB); + $sections = array_unique(array_merge(array_keys($old), array_keys($def))); + $final = array(); + foreach ($sections as $section) { + if ($section == 'fields') { + // this shouldn't be needed maybe... wait what? + } + $diff = $schema->diffArrays($old, $def, $section, $compare); + $chunks = array('del', 'mod', 'add'); + foreach ($chunks as $chunk) { + if ($diff[$chunk]) { + foreach ($diff[$chunk] as $key) { + if ($chunk == 'del') { + $final[$section]["DEL $key"] = $old[$section][$key]; + } else if ($chunk == 'add') { + $final[$section]["ADD $key"] = $def[$section][$key]; + } else if ($chunk == 'mod') { + $final[$section]["OLD $key"] = $old[$section][$key]; + $final[$section]["NEW $key"] = $def[$section][$key]; + } + } + } + } + } + + prettyDumpArray($final, $tableName); + print "\n"; +} + +function tweakPrimaryKey($def) +{ + if (isset($def['primary key'])) { + $def['primary keys'] = array('primary key' => $def['primary key']); + unset($def['primary key']); + } + return $def; } if (have_option('all')) { @@ -168,15 +214,7 @@ if (have_option('all')) { if (count($args)) { foreach ($args as $tableName) { if (have_option('diff')) { - ob_start(); - dumpTable($tableName, false); - $defined = ob_get_clean(); - - ob_start(); - dumpTable($tableName, true); - $detected = ob_get_clean(); - - showDiff($defined, $detected); + dumpDiff($tableName, !have_option('raw')); } else if (have_option('build')) { dumpBuildTable($tableName); } else if (have_option('update')) {