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 #198

Merged
merged 1 commit into from
Jun 21, 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 @@ -70,7 +70,7 @@ script:
- ./typo3cms cache:flush
- ./typo3cms cache:listgroups
- ./typo3cms cleanup:updatereferenceindex
- ./typo3cms database:updateschema "*"
- ./typo3cms database:updateschema "*" --verbose
- ./typo3cms database:export > /dev/null
- echo "SELECT username from be_users where admin=1;" | ./typo3cms database:import
- ./typo3cms frontend:request / > /dev/null
Expand Down
6 changes: 4 additions & 2 deletions Classes/Command/DatabaseCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class DatabaseCommandController extends CommandController
* <b>Example:</b> <code>./typo3cms database:updateschema "*.add,*.change"</code>
*
* @param array $schemaUpdateTypes List of schema update types
* @param bool $verbose If set, database queries performed are shown in output
* @throws \TYPO3\CMS\Core\Type\Exception\InvalidEnumerationValueException
*/
public function updateSchemaCommand(array $schemaUpdateTypes)
public function updateSchemaCommand(array $schemaUpdateTypes, $verbose = false)
{
try {
$schemaUpdateTypes = SchemaUpdateType::expandSchemaUpdateTypes($schemaUpdateTypes);
Expand All @@ -80,7 +82,7 @@ public function updateSchemaCommand(array $schemaUpdateTypes)

if ($result->hasPerformedUpdates()) {
$this->output->outputLine('<info>The following schema updates were performed:</info>');
$this->schemaUpdateResultRenderer->render($result, $this->output);
$this->schemaUpdateResultRenderer->render($result, $this->output, $verbose);
} else {
$this->output->outputLine('No schema updates matching the given types were performed');
}
Expand Down
22 changes: 18 additions & 4 deletions Classes/Database/Schema/SchemaUpdateResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,39 @@ public function getPerformedUpdates()
return $this->performedUpdates;
}

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

/**
* Returns true if updates were performed, false otherwise
*
* @return bool
*/
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)
public function addPerformedUpdates(SchemaUpdateType $schemaUpdateType, array $updates)
{
$this->performedUpdates[(string)$schemaUpdateType] += $numberOfUpdates;
$this->performedUpdates[(string)$schemaUpdateType] = array_merge((array)$this->performedUpdates[(string)$schemaUpdateType], $updates);;
}

/**
Expand Down
40 changes: 34 additions & 6 deletions Classes/Database/Schema/SchemaUpdateResultRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@ class SchemaUpdateResultRenderer
*
* @param SchemaUpdateResult $result Result of the schema update
* @param ConsoleOutput $output
* @param bool $includeStatements
* @param int $maxStatementLength
*/
public function render(SchemaUpdateResult $result, ConsoleOutput $output)
public function render(SchemaUpdateResult $result, ConsoleOutput $output, $includeStatements = false, $maxStatementLength = 60)
{
$tableRows = array();
$tableRows = [];

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

$output->outputTable($tableRows, array('Type', 'Updates'));
$tableHeader = ['Type', 'Updates'];
if ($includeStatements) {
$tableHeader = ['Type', 'SQL Statements'];
}
$output->outputTable($tableRows, $tableHeader);

if ($result->hasErrors()) {
foreach ($result->getErrors() as $type => $errors) {
Expand All @@ -60,4 +69,23 @@ public function render(SchemaUpdateResult $result, ConsoleOutput $output)
}
}
}

/**
* 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 = [];
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/SchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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
8 changes: 7 additions & 1 deletion Documentation/CommandReference/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Command Reference
instead.


The following reference was automatically generated from code on 2016-06-21 18:55:35
The following reference was automatically generated from code on 2016-06-21 22:24:35


.. _`Command Reference: typo3_console`:
Expand Down Expand Up @@ -462,6 +462,12 @@ Arguments



Options
^^^^^^^

``--verbose``
If set, database queries performed are shown in output




Expand Down