X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fdumpschema.php;h=05638cda4cb22aed28c57fe79916a8c1b1814a49;hb=d6b28c64830f632bb2f4b6f3c9369b9e56ad217a;hp=20812fe5b830f2b4e40a7d2a5d0aea2edc4b94d0;hpb=cc69ede1eeeb6c122b820367b07a56395a481557;p=quix0rs-gnu-social.git diff --git a/scripts/dumpschema.php b/scripts/dumpschema.php old mode 100644 new mode 100755 index 20812fe5b8..05638cda4c --- a/scripts/dumpschema.php +++ b/scripts/dumpschema.php @@ -23,10 +23,18 @@ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); $helptext = <<buildCreateTable($tableName, $def); + $sql[] = ''; + + echo implode(";\n", $sql); echo "\n"; } -function showDiff($a, $b) +function dumpEnsureTable($tableName) { - $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a'); - file_put_contents($fnameA, $a); + $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"; + } +} - $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b'); - file_put_contents($fnameB, $b); +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); + } - $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); + $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']); + } + if (isset($def['description'])) { + $def['descriptions'] = array('description' => $def['description']); + unset($def['description']); + } + return $def; +} + +function dumpChecksum($tableName) +{ + $schema = Schema::get(); + $def = getCoreSchema($tableName); + + $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"; + } +} + +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); } } } else { show_help($helptext); -} \ No newline at end of file +}