X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fdumpschema.php;h=05638cda4cb22aed28c57fe79916a8c1b1814a49;hb=d6b28c64830f632bb2f4b6f3c9369b9e56ad217a;hp=8b5277517f21beb43a3a958f5eb5ed4ac07c32fc;hpb=312b87ea79659b168bdc9569b735253d77994530;p=quix0rs-gnu-social.git diff --git a/scripts/dumpschema.php b/scripts/dumpschema.php old mode 100644 new mode 100755 index 8b5277517f..05638cda4c --- a/scripts/dumpschema.php +++ b/scripts/dumpschema.php @@ -23,18 +23,27 @@ define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); $helptext = <<getTableDef($tableName); + $schema = array(); + include INSTALLDIR . '/db/core.php'; + return $schema[$tableName]; +} + +function getCoreTables() +{ + $schema = array(); + include INSTALLDIR . '/db/core.php'; + return array_keys($schema); +} + +function dumpTable($tableName, $live) +{ + if ($live) { + $schema = Schema::get(); + $def = $schema->getTableDef($tableName); + } else { + // hack + $def = getCoreSchema($tableName); + } prettyDumpArray($def, $tableName); + print "\n"; +} + +function dumpBuildTable($tableName) +{ + echo "-- \n"; + echo "-- $tableName\n"; + echo "-- \n"; + + $schema = Schema::get(); + $def = getCoreSchema($tableName); + $sql = $schema->buildCreateTable($tableName, $def); + $sql[] = ''; + + echo implode(";\n", $sql); echo "\n"; } +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) +{ + 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) { - dumpTable($tableName); + if (have_option('diff')) { + 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 +}