/** * Perform necessary database schema migrations * * @param SchemaUpdateType[] $schemaUpdateTypes List of permitted schema update types * @return SchemaUpdateResult Result of the schema update */ public function updateSchema(array $schemaUpdateTypes) { $expectedSchema = $this->expectedSchemaService->getExpectedDatabaseSchema(); $currentSchema = $this->schemaMigrationService->getFieldDefinitions_database(); $addCreateChange = $this->schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema); $dropRename = $this->schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema); $updateStatements = array(); ArrayUtility::mergeRecursiveWithOverrule($updateStatements, $this->schemaMigrationService->getUpdateSuggestions($addCreateChange)); ArrayUtility::mergeRecursiveWithOverrule($updateStatements, $this->schemaMigrationService->getUpdateSuggestions($dropRename, 'remove')); $updateResult = new SchemaUpdateResult(); foreach ($schemaUpdateTypes as $schemaUpdateType) { $statementTypes = $this->getStatementTypes($schemaUpdateType); foreach ($statementTypes as $statementType) { if (isset($updateStatements[$statementType])) { $statements = $updateStatements[$statementType]; $result = $this->schemaMigrationService->performUpdateQueries($statements, array_combine(array_keys($statements), array_fill(0, count($statements), TRUE))); if ($result === TRUE) { $updateResult->addPerformedUpdates($schemaUpdateType, count($statements)); } elseif (is_array($result)) { $updateResult->addErrors($schemaUpdateType, $result); } } } } return $updateResult; }
/** * Renders a table for a schema update result * * @param SchemaUpdateResult $result Result of the schema update * @return void */ protected function outputSchemaUpdateResult(SchemaUpdateResult $result) { $tableRows = array(); foreach ($result->getPerformedUpdates() as $type => $numberOfUpdates) { $tableRows[] = array($this->schemaUpdateTypeLabels[(string) $type], $numberOfUpdates); } $this->output->outputTable($tableRows, array('Type', 'Updates')); if ($result->hasErrors()) { foreach ($result->getErrors() as $type => $errors) { $this->output->outputLine(sprintf('<error>Errors during "%s" schema update:</error>', $this->schemaUpdateTypeLabels[(string) $type])); foreach ($errors as $error) { $this->output->outputFormatted('<error>' . $error . '</error>', array(), 2); } } } }