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

Console: add command "migrations:status" issue #28 #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Bridges/NetteDI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ private function createSymfonyCommandDefinitions($driver, $configuration)
->setClass('Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand')
->setArguments([$driver, $configuration])
->addTag('kdyby.console.command');

$builder->addDefinition($this->prefix('statusCommand'))
->setClass('Nextras\Migrations\Bridges\SymfonyConsole\StatusCommand')
->setArguments([$driver, $configuration])
->addTag('kdyby.console.command');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ public function load(array $configs, ContainerBuilder $container)
$resetCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\ResetCommand');
$resetCommandDefinition->setAutowired(TRUE);
$resetCommandDefinition->addTag('console.command');

$statusCommandDefinition = new Definition('Nextras\Migrations\Bridges\SymfonyConsole\StatusCommand');
$statusCommandDefinition->setAutowired(TRUE);
$statusCommandDefinition->addTag('console.command');

$container->addDefinitions([
'nextras_migrations.configuration' => $configurationDefinition,
'nextras_migrations.continue_command' => $continueCommandDefinition,
'nextras_migrations.create_command' => $createCommandDefinition,
'nextras_migrations.reset_command' => $resetCommandDefinition,
'nextras_migrations.status_command' => $statusCommandDefinition,
]);

if ($structureDiffGeneratorDefinition) {
Expand Down
36 changes: 36 additions & 0 deletions src/Bridges/SymfonyConsole/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the Nextras community extensions of Nette Framework
*
* @license New BSD License
* @link https://github.com/nextras/migrations
*/

namespace Nextras\Migrations\Bridges\SymfonyConsole;

use Nextras\Migrations\Engine\Runner;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class StatusCommand extends BaseCommand
{
/** @var string */
protected static $defaultName = 'migrations:status';


protected function configure()
{
$this->setName(self::$defaultName);
$this->setDescription('Show lists of completed or waiting migrations');
}


protected function execute(InputInterface $input, OutputInterface $output)
{
$this->runMigrations(Runner::MODE_STATUS, $this->config);
}

}

20 changes: 17 additions & 3 deletions src/Engine/OrderResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class OrderResolver
*/
public function resolve(array $migrations, array $groups, array $files, $mode)
{
$this->checkModeSupport($mode);

$groups = $this->getAssocGroups($groups);
$this->validateGroups($groups);

if ($mode === Runner::MODE_RESET) {
return $this->sortFiles($files, $groups);
} elseif ($mode !== Runner::MODE_CONTINUE) {
throw new LogicException('Unsupported mode.');
}

$migrations = $this->getAssocMigrations($migrations);
Expand Down Expand Up @@ -254,5 +254,19 @@ private function validateGroups(array $groups)
}
}
}

/**
* @param string $mode
*/
private function checkModeSupport($mode)
{
$supportedModes = [
Runner::MODE_CONTINUE,
Runner::MODE_RESET,
Runner::MODE_STATUS,
];

if (!in_array($mode, $supportedModes, true)) {
throw new LogicException('Unsupported mode.');
}
}
}
21 changes: 15 additions & 6 deletions src/Engine/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Runner
const MODE_CONTINUE = 'continue';
const MODE_RESET = 'reset';
const MODE_INIT = 'init';
const MODE_STATUS = 'status';


/** @var IPrinter */
private $printer;
Expand Down Expand Up @@ -82,9 +84,11 @@ public function addExtensionHandler($extension, IExtensionHandler $handler)


/**
* @param string $mode self::MODE_CONTINUE|self::MODE_RESET|self::MODE_INIT
* @param string $mode self::MODE_CONTINUE|self::MODE_RESET|self::MODE_INIT|self::MODE_STATUS
* @param IConfiguration $config
*
* @return void
* @throws Exception
*/
public function run($mode = self::MODE_CONTINUE, IConfiguration $config = NULL)
{
Expand Down Expand Up @@ -120,12 +124,17 @@ public function run($mode = self::MODE_CONTINUE, IConfiguration $config = NULL)
$migrations = $this->driver->getAllMigrations();
$files = $this->finder->find($this->groups, array_keys($this->extensionsHandlers));
$toExecute = $this->orderResolver->resolve($migrations, $this->groups, $files, $mode);
$this->printer->printToExecute($toExecute);

foreach ($toExecute as $file) {
$time = microtime(TRUE);
$queriesCount = $this->execute($file);
$this->printer->printExecute($file, $queriesCount, microtime(TRUE) - $time);
if ($mode === self::MODE_STATUS) {
$this->printer->printExecutedMigrations($migrations);
$this->printer->printToExecute($toExecute, true);
} else {
$this->printer->printToExecute($toExecute, false);
foreach ($toExecute as $file) {
$time = microtime(TRUE);
$queriesCount = $this->execute($file);
$this->printer->printExecute($file, $queriesCount, microtime(TRUE) - $time);
}
}

$this->driver->unlock();
Expand Down
28 changes: 20 additions & 8 deletions src/IPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nextras\Migrations;

use Nextras\Migrations\Entities\File;
use Nextras\Migrations\Entities\Migration;


/**
Expand All @@ -23,14 +24,25 @@ interface IPrinter
* - continue = Running new migrations.
* @param string $mode
*/
function printIntro($mode);


public function printIntro($mode);

/**
* List of migrations which executed has been completed.
* @param Migration[] $migrations
*
* @return void
*/
public function printExecutedMigrations(array $migrations);

/**
* List of migrations which should be executed has been completed.
*
* @param File[] $toExecute
* @param bool $withFileList
*
* @return void
*/
function printToExecute(array $toExecute);
public function printToExecute(array $toExecute, $withFileList = false);


/**
Expand All @@ -39,26 +51,26 @@ function printToExecute(array $toExecute);
* @param int $count number of executed queries
* @param float $time elapsed time in milliseconds
*/
function printExecute(File $file, $count, $time);
public function printExecute(File $file, $count, $time);


/**
* All migrations have been successfully executed.
*/
function printDone();
public function printDone();


/**
* An error has occurred during execution of a migration.
* @param Exception $e
*/
function printError(Exception $e);
public function printError(Exception $e);


/**
* Prints init source code.
* @param string $code
*/
function printSource($code);
public function printSource($code);

}
63 changes: 51 additions & 12 deletions src/Printers/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nextras\Migrations\Printers;

use Nextras\Migrations\Entities\File;
use Nextras\Migrations\Entities\Migration;
use Nextras\Migrations\Exception;
use Nextras\Migrations\IPrinter;

Expand All @@ -34,26 +35,60 @@ public function __construct()
{
$this->useColors = $this->detectColorSupport();
}



/**
* @inheritdoc
*/
public function printIntro($mode)
{
$this->output('Nextras Migrations');
$this->output(strtoupper($mode), self::COLOR_INTRO);
}


public function printToExecute(array $toExecute)

/**
* @inheritdoc
*/
public function printExecutedMigrations(array $migrations)
{
if ($migrations) {
$this->output('Executed migrations:');
/** @var Migration $migration */
foreach ($migrations as $migration) {
$this->output('- ' . $migration->group . '/' . $migration->filename . ' OK', self::COLOR_SUCCESS);
}
$this->output(' ');
} else {
$this->output('No migrations has executed yet.');
}
}

/**
* @inheritdoc
*/
public function printToExecute(array $toExecute, $withFileList = FALSE)
{
if ($toExecute) {
$count = count($toExecute);
$this->output($count . ' migration' . ($count > 1 ? 's' : '') . ' need' . ($count > 1 ? '' : 's') . ' to be executed.');
$this->output(
sprintf(
'%s migration%s need%s to be executed%s',
$count, $count > 1 ? 's' : '', $count > 1 ? '' : 's', ($withFileList ? ':' : '.')
)
);
if ($withFileList) {
/** @var File $file */
foreach ($toExecute as $file) {
$this->output('- ' . $file->group->name . '/' . $file->name, self::COLOR_INFO);
}
}
} else {
$this->output('No migration needs to be executed.');
}
}



/**
* @inheritdoc
*/
public function printExecute(File $file, $count, $time)
{
$this->output(
Expand All @@ -62,14 +97,18 @@ public function printExecute(File $file, $count, $time)
. $this->color(sprintf('%0.3f', $time), self::COLOR_INFO) . ' s'
);
}



/**
* @inheritdoc
*/
public function printDone()
{
$this->output('OK', self::COLOR_SUCCESS);
}



/**
* @inheritdoc
*/
public function printError(Exception $e)
{
$this->output('ERROR: ' . $e->getMessage(), self::COLOR_ERROR);
Expand Down
45 changes: 34 additions & 11 deletions src/Printers/DevNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nextras\Migrations\Printers;

use Nextras\Migrations\Entities\File;
use Nextras\Migrations\Entities\Migration;
use Nextras\Migrations\Exception;
use Nextras\Migrations\IPrinter;

Expand All @@ -20,32 +21,54 @@
*/
class DevNull implements IPrinter
{
/**
* @inheritdoc
*/
public function printIntro($mode)
{
}


public function printToExecute(array $toExecute)

/**
* @inheritdoc
*/
public function printToExecute(array $toExecute, $withFileList = false)
{
}



/**
* @inheritdoc
*/
public function printExecute(File $file, $count, $time)
{
}



/**
* @inheritdoc
*/
public function printDone()
{
}



/**
* @inheritdoc
*/
public function printError(Exception $e)
{
}



/**
* @inheritdoc
*/
public function printSource($code)
{
}

/**
* @inheritdoc
*/
public function printExecutedMigrations(array $migrations)
{

}
}

Loading