From: Brion Vibber Date: Wed, 13 Oct 2010 23:04:28 +0000 (-0700) Subject: Filter table definitions to scrub out unsupported features before trying to alter... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=229c7726344714187dad2641879cb9c4211cc85d;p=quix0rs-gnu-social.git Filter table definitions to scrub out unsupported features before trying to alter a table. This lets us skip those where we end up trying to change unsupported features. --- diff --git a/lib/mysqlschema.php b/lib/mysqlschema.php index 688bea3e2b..eeabae8cd3 100644 --- a/lib/mysqlschema.php +++ b/lib/mysqlschema.php @@ -523,4 +523,20 @@ class MysqlSchema extends Schema return parent::typeAndSize($column); } } + + /** + * Filter the given table definition array to match features available + * in this database. + * + * This lets us strip out unsupported things like comments, foreign keys, + * or type variants that we wouldn't get back from getTableDef(). + * + * @param array $tableDef + */ + function filterDef(array $tableDef) + { + // @fixme add foreign-key support for MySQL + unset($tableDef['foreign keys']); + return $tableDef; + } } diff --git a/lib/pgsqlschema.php b/lib/pgsqlschema.php index a8ce21b384..e8711a6f85 100644 --- a/lib/pgsqlschema.php +++ b/lib/pgsqlschema.php @@ -406,4 +406,29 @@ class PgsqlSchema extends Schema } } + /** + * Filter the given table definition array to match features available + * in this database. + * + * This lets us strip out unsupported things like comments, foreign keys, + * or type variants that we wouldn't get back from getTableDef(). + * + * @param array $tableDef + */ + function filterDef(array $tableDef) + { + foreach (array_keys($tableDef['fields']) as $name => &$col) { + // No convenient support for field descriptions + unset($col['description']); + + if (isset($col['size'])) { + // Don't distinguish between tinyint and int. + if ($col['size'] == 'tiny' && $col['type'] == 'int') { + unset($col['size']); + } + } + } + return $tableDef; + } + } diff --git a/lib/schema.php b/lib/schema.php index 04bd2d1d94..da43c0559a 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -143,6 +143,7 @@ class Schema */ public function buildCreateTable($name, $def) { + $def = $this->filterDef($def); $sql = array(); foreach ($def['fields'] as $col => $colDef) { @@ -487,17 +488,19 @@ class Schema } } + $old = $this->filterDef($old); + $def = $this->filterDef($def); + // @fixme check if not present $fields = $this->diffArrays($old['fields'], $def['fields'], array($this, 'columnsEqual')); $uniques = $this->diffArrays($old['unique keys'], $def['unique keys']); $indexes = $this->diffArrays($old['indexes'], $def['indexes']); - /* - if (count($toadd) + count($todrop) + count($tomod) == 0) { + $total = $fields['count'] + $uniques['count'] + $indexes['count']; + if ($total == 0) { // nothing to do - return true; + return array(); } - */ // For efficiency, we want this all in one // query, instead of using our methods. @@ -561,7 +564,8 @@ class Schema return array('add' => $toadd, 'del' => $todrop, 'mod' => $tomod, - 'keep' => $tokeep); + 'keep' => $tokeep, + 'count' => count($toadd) + count($todrop) + count($tomod)); } /** @@ -847,6 +851,20 @@ class Schema return $table; } + /** + * Filter the given table definition array to match features available + * in this database. + * + * This lets us strip out unsupported things like comments, foreign keys, + * or type variants that we wouldn't get back from getTableDef(). + * + * @param array $tableDef + */ + function filterDef(array $tableDef) + { + return $tableDef; + } + function isNumericType($type) { $type = strtolower($type); diff --git a/scripts/dumpschema.php b/scripts/dumpschema.php index 3c23636383..fcf2430d80 100644 --- a/scripts/dumpschema.php +++ b/scripts/dumpschema.php @@ -129,17 +129,19 @@ function dumpBuildTable($tableName) function dumpEnsureTable($tableName) { - echo "-- \n"; - echo "-- $tableName\n"; - echo "-- \n"; - $schema = Schema::get(); $def = getCoreSchema($tableName); $sql = $schema->buildEnsureTable($tableName, $def); - $sql[] = ''; - echo implode(";\n", $sql); - echo "\n"; + if ($sql) { + echo "-- \n"; + echo "-- $tableName\n"; + echo "-- \n"; + + $sql[] = ''; + echo implode(";\n", $sql); + echo "\n"; + } } function showDiff($a, $b)