if ($options['lint-check']) { $matchType = $sourceType; $matchSchema = $sourceSchema; unset($sourceSchema); $sourceSchema = loadLintSchema($matchSchema, $cli); } else { $matchSchema = loadDatabaseSchema($matchType, $matchDBHost, $matchDBUser, $matchDBPassword, $matchDBSocket, $matchDB, $cli); if (!$matchSchema) { $cli->error("Failed to load schema from match database"); $script->shutdown(1); } } if ($options['reverse']) { $differences = eZDbSchemaChecker::diff($sourceSchema->schema(), $matchSchema->schema(), $sourceType, $matchType); if (!$options['check-only']) { $cli->output("-- Difference in SQL commands for " . $sourceSchema->schemaName()); $sql = $sourceSchema->generateUpgradeFile($differences); $cli->output($sql); } } else { $differences = eZDbSchemaChecker::diff($matchSchema->schema(), $sourceSchema->schema(), $matchType, $sourceType); if (!$options['check-only']) { $cli->output("-- Difference in SQL commands from " . $sourceSchema->schemaName() . " to " . $matchSchema->schemaName()); $sql = $matchSchema->generateUpgradeFile($differences); $cli->output($sql); } } if (count($differences) > 0) { $script->setExitCode(1); } $script->shutdown();
static function diffTable($table1, $table2, $schema1Type, $schema2Type) { $table_diff = array(); /* See if all the fields in table 1 exist in table 2 */ foreach ($table2['fields'] as $name => $def) { if (!isset($table1['fields'][$name])) { $table_diff['added_fields'][$name] = $def; } } /* See if there are any removed fields in table 2 */ foreach ($table1['fields'] as $name => $def) { if (!isset($table2['fields'][$name])) { $table_diff['removed_fields'][$name] = true; } else { if (isset($table2['fields'][$name]['removed']) and $table2['fields'][$name]['removed']) { $table_diff['removed_fields'][$name] = true; } } } /* See if there are any changed definitions */ foreach ($table1['fields'] as $name => $def) { if (isset($table2['fields'][$name])) { if (is_array($field_diff = eZDbSchemaChecker::diffField($def, $table2['fields'][$name], $schema1Type, $schema2Type))) { $table_diff['changed_fields'][$name] = $field_diff; } } } $table1Indexes = $table1['indexes']; $table2Indexes = $table2['indexes']; /* See if all the indexes in table 1 exist in table 2 */ foreach ($table2Indexes as $name => $def) { if (!isset($table1Indexes[$name])) { $table_diff['added_indexes'][$name] = $def; } } /* See if there are any removed indexes in table 2 */ foreach ($table1Indexes as $name => $def) { if (!isset($table2Indexes[$name])) { $table_diff['removed_indexes'][$name] = $def; } else { if (isset($table2Indexes[$name]['removed']) and $table2Indexes[$name]['removed']) { if (isset($table2Indexes[$name]['comments'])) { $def['comments'] = array_merge(isset($def['comments']) ? $def['comments'] : array(), $table2Indexes[$name]['comments']); } $table_diff['removed_indexes'][$name] = $def; } } } /* See if there are any changed definitions */ foreach ($table1Indexes as $name => $def) { if (isset($table2Indexes[$name])) { if (is_array($index_diff = eZDbSchemaChecker::diffIndex($def, $table2Indexes[$name], $schema1Type, $schema2Type))) { $table_diff['changed_indexes'][$name] = $index_diff; } } } return $table_diff; }
} } if ($Module->isCurrentAction('DBCheck')) { $db = eZDB::instance(); $dbSchema = eZDbSchema::instance(); // read original schema from dba file $originalSchema = eZDbSchema::read('share/db_schema.dba'); // merge schemas from all active extensions that declare some db schema $extensionsdir = eZExtension::baseDirectory(); foreach (eZExtension::activeExtensions() as $activeextension) { if (file_exists($extensionsdir . '/' . $activeextension . '/share/db_schema.dba')) { if ($extensionschema = eZDbSchema::read($extensionsdir . '/' . $activeextension . '/share/db_schema.dba')) { $originalSchema = eZDbSchema::merge($originalSchema, $extensionschema); } } } // transform schema to 'localized' version for current db // (we might as well convert $dbSchema to generic format and diff in generic format, // but eZDbSchemaChecker::diff does not know how to re-localize the generated sql $dbSchema->transformSchema($originalSchema, true); $differences = eZDbSchemaChecker::diff($dbSchema->schema(array('format' => 'local', 'force_autoincrement_rebuild' => true)), $originalSchema); $sqlDiff = $dbSchema->generateUpgradeFile($differences); if (strlen($sqlDiff) == 0) { $tpl->setVariable('upgrade_sql', 'ok'); } else { $tpl->setVariable('upgrade_sql', $sqlDiff); } } $Result = array(); $Result['content'] = $tpl->fetch("design:setup/systemupgrade.tpl"); $Result['path'] = array(array('url' => false, 'text' => ezpI18n::tr('kernel/setup', 'System Upgrade')));