X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fdumpschema.php;h=9c5e3cf8b38c2fd9808803ba9c2c8437fcddfb89;hb=0e439117a75698629c5066d0241ab41dc07b5ee1;hp=818a4d5eae4cbe4ee567ca8554e3da8b9f3cc5b4;hpb=443be8a99c52b05b6a3c6acdbdc944b537faeff6;p=quix0rs-gnu-social.git diff --git a/scripts/dumpschema.php b/scripts/dumpschema.php index 818a4d5eae..9c5e3cf8b3 100644 --- a/scripts/dumpschema.php +++ b/scripts/dumpschema.php @@ -23,19 +23,27 @@ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); $helptext = <<buildCreateTable($tableName, $def); + $sql[] = ''; + + echo implode(";\n", $sql); echo "\n"; } -function showDiff($a, $b) +function dumpEnsureTable($tableName) +{ + $schema = Schema::get(); + $def = getCoreSchema($tableName); + $sql = $schema->buildEnsureTable($tableName, $def); + + if ($sql) { + echo "-- \n"; + echo "-- $tableName\n"; + echo "-- \n"; + + $sql[] = ''; + echo implode(";\n", $sql); + echo "\n"; + } +} + +function dumpDiff($tableName, $filter) +{ + $schema = Schema::get(); + $def = getCoreSchema($tableName); + try { + $old = $schema->getTableDef($tableName); + } catch (Exception $e) { + // @fixme this is a terrible check :D + if (preg_match('/no such table/i', $e->getMessage())) { + return dumpTable($tableName, false); + } else { + throw $e; + } + } + + if ($filter) { + //$old = $schema->filterDef($old); + $def = $schema->filterDef($def); + } + + // @hack + $old = tweakPrimaryKey($old); + $def = tweakPrimaryKey($def); + + $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); + $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) { - $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a'); - file_put_contents($fnameA, $a); + if (isset($def['primary key'])) { + $def['primary keys'] = array('primary key' => $def['primary key']); + unset($def['primary key']); + } + if (isset($def['description'])) { + $def['descriptions'] = array('description' => $def['description']); + unset($def['description']); + } + return $def; +} - $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b'); - file_put_contents($fnameB, $b); +function dumpChecksum($tableName) +{ + $schema = Schema::get(); + $def = getCoreSchema($tableName); - $cmd = sprintf('diff -U 100 %s %s', - escapeshellarg($fnameA), - escapeshellarg($fnameB)); - passthru($cmd); + $updater = new SchemaUpdater($schema); + $checksum = $updater->checksum($def); + $old = @$updater->checksums[$tableName]; + + if ($old == $checksum) { + echo "OK $checksum $tableName\n"; + } else if (!$old) { + echo "NEW $checksum $tableName\n"; + } else { + echo "MOD $checksum $tableName (was $old)\n"; + } +} - unlink($fnameA); - unlink($fnameB); +if (have_option('all')) { + $args = getCoreTables(); } 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('create')) { + dumpBuildTable($tableName); + } else if (have_option('update')) { + dumpEnsureTable($tableName); + } else if (have_option('checksum')) { + dumpChecksum($tableName); } else { dumpTable($tableName, true); }