Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add verbose output to schema update #201

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ script:
- ./typo3cms backend:lock
- ./typo3cms backend:unlock
- ./typo3cms cleanup:updatereferenceindex
- ./typo3cms database:updateschema "*"
- ./typo3cms database:updateschema "*" --verbose
- rm -f typo3conf/PackageStates.php && ./typo3cms install:generatepackagestates && [ -f "typo3conf/PackageStates.php" ]
- rm typo3temp/index.html && ./typo3cms install:fixfolderstructure && [ -f "typo3temp/index.html" ]
44 changes: 37 additions & 7 deletions Classes/Command/DatabaseCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ class DatabaseCommandController extends CommandController {
* To avoid shell matching all types with wildcards should be quoted.
*
* @param array $schemaUpdateTypes List of schema update types
* @param bool $verbose If set, database queries performed are shown in output
*/
public function updateSchemaCommand(array $schemaUpdateTypes) {
public function updateSchemaCommand(array $schemaUpdateTypes, $verbose = FALSE) {
try {
$schemaUpdateTypes = $this->expandSchemaUpdateTypes($schemaUpdateTypes);
} catch (\UnexpectedValueException $e) {
Expand All @@ -86,7 +87,7 @@ public function updateSchemaCommand(array $schemaUpdateTypes) {

if ($result->hasPerformedUpdates()) {
$this->output->outputLine('<info>The following schema updates where performed:</info>');
$this->outputSchemaUpdateResult($result);
$this->outputSchemaUpdateResult($result, $verbose);
} else {
$this->output->outputLine('No schema updates matching the given types where performed');
}
Expand Down Expand Up @@ -134,25 +135,54 @@ protected function expandSchemaUpdateTypes(array $schemaUpdateTypes) {
* Renders a table for a schema update result
*
* @param SchemaUpdateResult $result Result of the schema update
* @param bool $includeStatements TRUE to include the performed statements in the output, FALSE otherwise
* @param int $maxStatementLength Wrap statements at the given number of characters
* @return void
*/
protected function outputSchemaUpdateResult(SchemaUpdateResult $result) {
protected function outputSchemaUpdateResult(SchemaUpdateResult $result, $includeStatements = FALSE, $maxStatementLength = 60) {
$tableRows = array();

foreach ($result->getPerformedUpdates() as $type => $numberOfUpdates) {
$tableRows[] = array($this->schemaUpdateTypeLabels[(string)$type], $numberOfUpdates);
foreach ($result->getPerformedUpdates() as $type => $performedUpdates) {
$row = array($this->schemaUpdateTypeLabels[(string)$type], count($performedUpdates));
if ($includeStatements) {
$row = array($this->schemaUpdateTypeLabels[(string)$type], implode(chr(10) . chr(10), $this->getTruncatedQueries($performedUpdates, $maxStatementLength)));
}
$tableRows[] = $row;
}

$tableHeader = array('Type', 'Updates');

if ($includeStatements) {
$tableHeader = array('Type', 'SQL Statements');
}

$this->output->outputTable($tableRows, array('Type', 'Updates'));
$this->output->outputTable($tableRows, $tableHeader);

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);
}
}
}
}

/**
* Truncate (wrap) query strings at a certain number of characters
*
* @param array $queries
* @param int $truncateAt
* @return array
*/
protected function getTruncatedQueries(array $queries, $truncateAt) {
foreach ($queries as &$query) {
$truncatedLines = array();
foreach (explode(chr(10), $query) as $line) {
$truncatedLines[] = wordwrap($line, $truncateAt, chr(10), true);
}
$query = implode(chr(10), $truncatedLines);
}
return $queries;
}
}
2 changes: 1 addition & 1 deletion Classes/Service/Database/Schema/SchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function updateSchema(array $schemaUpdateTypes) {
);

if ($result === TRUE) {
$updateResult->addPerformedUpdates($schemaUpdateType, count($statements));
$updateResult->addPerformedUpdates($schemaUpdateType, $statements);
} elseif (is_array($result)) {
$updateResult->addErrors($schemaUpdateType, $result);
}
Expand Down
21 changes: 17 additions & 4 deletions Classes/Service/Database/Schema/SchemaUpdateResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,36 @@ public function getPerformedUpdates() {
return $this->performedUpdates;
}

/**
* Returns the list of performed update types including the count
*
* @return array
*/
public function getPerformedUpdateTypes() {
$typesCount = array();
foreach ($this->performedUpdates as $type => $performedUpdates) {
$typesCount[$type] = count($performedUpdates);
}
return $typesCount;
}

/**
* Returns TRUE if updates where performed, FALSE otherwise
*
* @return boolean
*/
public function hasPerformedUpdates() {
return count($this->performedUpdates);
return count($this->performedUpdates) > 0;
}

/**
* Adds to the number of updates performed for a schema update type
*
* @param SchemaUpdateType $schemaUpdateType Schema update type
* @param int $numberOfUpdates Number of updates performed
* @param array $updates Updates performed
*/
public function addPerformedUpdates(SchemaUpdateType $schemaUpdateType, $numberOfUpdates) {
$this->performedUpdates[(string)$schemaUpdateType] += $numberOfUpdates;
public function addPerformedUpdates(SchemaUpdateType $schemaUpdateType, array $updates) {
$this->performedUpdates[(string)$schemaUpdateType] = array_merge((array)$this->performedUpdates[(string)$schemaUpdateType], $updates);
}

/**
Expand Down