Skip to content

Commit

Permalink
Allow verbose output for 'migrate' command
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Oct 21, 2016
1 parent 1d46455 commit 92e4362
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->setVerbosity(self::VERBOSITY_CHILD);
}

$this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output);

$migrationService = $this->getMigrationService();

$paths = $input->getOption('path');
Expand Down
112 changes: 112 additions & 0 deletions Core/EventListener/TracingStepExecutedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Kaliop\eZMigrationBundle\Core\EventListener;

use Kaliop\eZMigrationBundle\API\Event\StepExecutedEvent;
use \Kaliop\eZMigrationBundle\API\Collection\AbstractCollection;

use Symfony\Component\Console\Output\OutputInterface;

/**
* A listener designed to give feedback on the execution of migration steps
*
* @todo qdd proper support for plural forms, as well as for proper sentences when dealing with empty collections
*/
class TracingStepExecutedListener
{
protected $output;
protected $minVerbosityLevel = OutputInterface::VERBOSITY_VERBOSE;

public function setOutput(OutputInterface $output)
{
$this->output = $output;
}

/**
* NB: only works when using an OutputInterface to echo output
* @param int $level
*/
public function setMinVerbosity($level)
{
$this->minVerbosityLevel = $level;
}

public function onStepExecuted(StepExecutedEvent $event)
{
$obj = $event->getResult();
$type = $event->getStep()->type;
$dsl = $event->getStep()->dsl;
$action = isset($dsl['mode']) ? ($dsl['mode'] . 'd') : 'acted upon';

switch($type) {
case 'content':
case 'content_type':
case 'language':
case 'location':
case 'object_state':
case 'object_state_group':
case 'role':
case 'tag':
case 'user':
case 'user_group':
$out = $type . ' '. $this->getObjectIdentifierAsString($obj). ' has been ' . $action;
break;
case 'sql':
$out = 'sql has been executed';
break;
case 'php':
$out = "class '{$dsl['class']}' has been executed";
break;
default:
// custom migration step types...
$out = "migration step '$type' has been executed";
}

if ($this->output) {
if ($this->output->getVerbosity() >= $this->minVerbosityLevel) {
$this->output->writeln($out);
}
} else {
echo $out . "\n";
}
}

protected function getObjectIdentifierAsString($objOrCollection)
{
if ($objOrCollection instanceof AbstractCollection || is_array($objOrCollection)) {
$out = array();
foreach($objOrCollection as $obj) {
$out[] = $this->getObjectIdentifierAsString($obj);
}
return implode(", ", $out) ;
}

switch(gettype($objOrCollection)) {

case 'object':
if ($objOrCollection instanceof \eZ\Publish\API\Repository\Values\Content\Content ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\User\UserGroup)
return "'" . $objOrCollection->contentInfo->name . "'";
if ($objOrCollection instanceof \eZ\Publish\API\Repository\Values\ContentType\ContentType ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\ObjectState\ObjectState ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\User\Role ||
$objOrCollection instanceof \eZ\Publish\API\Repository\Values\Content\Section)
return "'" . $objOrCollection->identifier . "'";
if ($objOrCollection instanceof \eZ\Publish\API\Repository\Values\Content\Location)
return "'" . $objOrCollection->pathString . "'";
if ($objOrCollection instanceof \eZ\Publish\API\Repository\Values\Content\Language)
return "'" . $objOrCollection->languageCode . "'";
if ($objOrCollection instanceof \eZ\Publish\API\Repository\Values\User\User)
return "'" . $objOrCollection->login . "'";
// unknown objects - we can't know what the desired identifier is...
return '';

default:
// scalars: the identifier is the value...
/// @todo make readable NULL, true/false
return "'" . $objOrCollection . "'";
}
}
}
8 changes: 8 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ parameters:
ez_migration_bundle.helper.limitation_converter.class: Kaliop\eZMigrationBundle\Core\Helper\LimitationConverter
ez_migration_bundle.helper.console_io.class: Kaliop\eZMigrationBundle\Core\Helper\ConsoleIO

ez_migration_bundle.step_executed_listener.tracing.class: Kaliop\eZMigrationBundle\Core\EventListener\TracingStepExecutedListener

services:

ez_migration_bundle.migration_service:
Expand Down Expand Up @@ -424,6 +426,12 @@ services:

### misc

# This service is used for the verbose mode when executing migrations on the command line
ez_migration_bundle.step_executed_listener.tracing:
class: '%ez_migration_bundle.step_executed_listener.tracing.class%'
tags:
- { name: kernel.event_listener, event: ez_migration.step_executed, method: onStepExecuted }

ez_migration_bundle.helper.limitation_converter:
class: '%ez_migration_bundle.helper.limitation_converter.class%'
arguments:
Expand Down
7 changes: 5 additions & 2 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Version 3.0.0-beta3
* New: the 'migrate' command learned a `--separate-process` option, to run each migration it its own separate
php process. This should help when running many migrations in a single pass, and there are f.e. memory leaks.

* New: the 'migrate' command learned to give information ion the executed steps when using the `-v` option

* New: the 'migration' command learned a `--skip` option, to tag migrations as to be skipped

* New: it is now possible to create, update and delete Object States and Object State Groups
Expand Down Expand Up @@ -36,13 +38,14 @@ Version 3.0.0-beta3

* New: the content_type_group tags in ContentType creation will accept a Content Type Group identifier besides an id

* New: added 2 events to which you can listen to implement custom logic just-before and just-after migration steps are executed:
* New: added 2 events to which you can listen to implement custom logic just-before and just-after migration steps are
executed:

* ez_migration.before_execution => listeners receive a BeforeStepExecutionEvent event instance

* ez_migration.step_executed => listeners receive a StepExecutedEvent event instance

* Fixed: migrations will not silently swallow any more errors when creating users or assigning roles and an inexisting
* Fix: migrations will not silently swallow any more errors when creating users or assigning roles and a non-existing
group is specified

* Fix: (issue 78) when using transactions, do not report 'Migration can not be ended' if there is an error during the
Expand Down

0 comments on commit 92e4362

Please sign in to comment.