Skip to content

Commit

Permalink
Merge pull request #10 from doesntmattr/feature/status-descriptions
Browse files Browse the repository at this point in the history
Feature/status descriptions
  • Loading branch information
caciobanu authored Sep 14, 2016
2 parents c200d63 + 365ba92 commit 5ebeb21
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 39 deletions.
32 changes: 32 additions & 0 deletions src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,38 @@ public function getMigratedVersions()
return $versions;
}

/**
* Returns the time a migration occurred.
*
* @param string $version
*
* @return string
*
* @throws AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException Throws exception if migration version does not exist
*/
public function getMigratedTimestamp($version)
{
$this->createMigrationCollection();

$cursor = $this->getCollection()->find(
array('v' => $version)
);

if (!$cursor->count()) {
throw new UnknownVersionException($version);
}

if ($cursor->count() > 1) {
throw \DomainException(
'Unexpected duplicate version records in the database'
);
}

$returnVersion = $cursor->getNext();

return (string) $returnVersion['t'];
}

/**
* Return all migrated versions from versions collection that have migration files deleted.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ abstract class AbstractCommand extends Command
*/
private $configuration;

/**
* configure.
*/
protected function configure()
{
$this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
$this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.');
$this->addOption(
'configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'
);
$this->addOption(
'db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

/**
* @author Matthew Fitzgerald <[email protected]>
Expand All @@ -30,7 +31,12 @@ protected function configure()
$this
->setName($this->getName())
->setDescription('View the status of a set of migrations.')
->addOption('show-versions', null, InputOption::VALUE_NONE, 'This will display a list of all available migrations and their status')
->addOption(
'show-versions',
null,
InputOption::VALUE_NONE,
'This will display a list of all available migrations and their status'
)
->setHelp(<<<'EOT'
The <info>%command.name%</info> command outputs the status of a set of migrations:
Expand Down Expand Up @@ -98,27 +104,58 @@ public function execute(InputInterface $input, OutputInterface $output)
'Available Migrations' => $configMap['num_available_migrations'],
'New Migrations' => $numNewMigrations > 0 ? '<question>'.$numNewMigrations.'</question>' : 0,
);

foreach ($info as $name => $value) {
$this->writeInfoLine($output, $name, $value);
}

$showVersions = $input->getOption('show-versions') ? true : false;
if ($showVersions === true) {
if ($input->getOption('show-versions')) {
if ($migrations = $configuration->getMigrations()) {
$output->writeln("\n <info>==</info> Available Migration Versions\n");
$rows = array();
$migratedVersions = $configuration->getMigratedVersions();

foreach ($migrations as $version) {
$isMigrated = in_array($version->getVersion(), $migratedVersions);
$status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($version->getVersion()).' (<comment>'.$version->getVersion().'</comment>)'.str_repeat(' ', 30 - strlen($name)).$status);
$status = '<error>not migrated</error>';

if ($isMigrated) {
$ts = $configuration->getMigratedTimestamp(
$version->getVersion()
);

$status = sprintf(
'<info>%s</info>',
date('Y-m-d H:i', $ts)
);
}

$versionTxt = sprintf('<comment>%s</comment>', $version->getVersion());
$desc = $version->getMigration()->getDescription();
if (strlen($desc) > 80) {
$desc = substr($desc, 0, 78).'...';
}

$rows[] = array($versionTxt, $status, $desc);
}

$table = new Table($output);
$table->setHeaders(array('Version', 'Date Migrated', 'Description'))
->setRows($rows)
->render();
}

$executedUnavailableMigrations = $configuration->getUnavailableMigratedVersions();
if ($executedUnavailableMigrations) {
$output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($executedUnavailableMigration).' (<comment>'.$executedUnavailableMigration.'</comment>)');
$output->writeln(
sprintf(
' <comment>>></comment> %s (<comment>%s</comment>)',
Configuration::formatVersion($executedUnavailableMigration),
$executedUnavailableMigration
)
);
}
}
}
Expand All @@ -141,6 +178,11 @@ protected function writeInfoLine(OutputInterface $output, $name, $value)
);
}

/**
* getName.
*
* @return string
*/
public function getName()
{
return self::NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ protected function setUp()
{
$this->command = new StatusCommandStub();
$this->output = $this->buildMock('Symfony\Component\Console\Output\OutputInterface');
$this->outputFormatter = $this->buildMock(
'Symfony\Component\Console\Formatter\OutputFormatterInterface'
);
$this->config = $this->buildMock('AntiMattr\MongoDB\Migrations\Configuration\Configuration');
$this->migration = $this->buildMock('AntiMattr\MongoDB\Migrations\Migration');
$this->migration = $this->buildMock('AntiMattr\MongoDB\Migrations\AbstractMigration');
$this->version = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
$this->version->expects($this->any())
->method('getMigration')
->will($this->returnValue($this->migration));

$this->version2 = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
$this->version2->expects($this->any())
->method('getMigration')
->will($this->returnValue($this->migration));

$this->command->setMigrationConfiguration($this->config);
}
Expand Down Expand Up @@ -239,17 +249,26 @@ public function testExecuteWithShowingVersions()
$numNewMigrations = 1;
$notMigratedVersion = '20140822185743';
$migratedVersion = '20140822185745';
$migrationDescription = 'drop all collections';
$unavailableMigratedVersion = '20140822185744';

// Expectations
$this->version->expects($this->exactly(3))
$this->output
->method('getFormatter')
->will($this->returnValue($this->outputFormatter));

$this->version->expects($this->exactly(2))
->method('getVersion')
->will($this->returnValue($notMigratedVersion));

$this->version2->expects($this->exactly(3))
->method('getVersion')
->will($this->returnValue($migratedVersion));

$this->migration
->method('getDescription')
->will($this->returnValue('drop all'));

$this->config->expects($this->once())
->method('getDetailsMap')
->will(
Expand All @@ -272,30 +291,29 @@ public function testExecuteWithShowingVersions()
)
;
$this->config->expects($this->once())
->method('getMigrations')
->method('getUnavailableMigratedVersions')
->will(
$this->returnValue(
array($this->version, $this->version2)
array($unavailableMigratedVersion)
)
)
;
$this->config->expects($this->once())
->method('getMigratedVersions')
->method('getMigrations')
->will(
$this->returnValue(
array($unavailableMigratedVersion, $migratedVersion)
array($this->version, $this->version2)
)
)
;
$this->config->expects($this->once())
->method('getUnavailableMigratedVersions')
->method('getMigratedVersions')
->will(
$this->returnValue(
array($unavailableMigratedVersion)
array($unavailableMigratedVersion, $migratedVersion)
)
)
;

$this->output->expects($this->at(0))
->method('writeln')
->with(
Expand Down Expand Up @@ -422,31 +440,12 @@ public function testExecuteWithShowingVersions()
->method('writeln')
->with("\n <info>==</info> Available Migration Versions\n")
;
$this->output->expects($this->at(15))
->method('writeln')
->with(
sprintf(
' <comment>>></comment> %s (<comment>%s</comment>) <error>not migrated</error>',
\DateTime::createFromFormat('YmdHis', $notMigratedVersion)->format('Y-m-d H:i:s'),
$notMigratedVersion
)
)
;
$this->output->expects($this->at(16))
->method('writeln')
->with(
sprintf(
' <comment>>></comment> %s (<comment>%s</comment>) <info>migrated</info>',
\DateTime::createFromFormat('YmdHis', $migratedVersion)->format('Y-m-d H:i:s'),
$migratedVersion
)
)
;
$this->output->expects($this->at(17))

$this->output->expects($this->at(39))
->method('writeln')
->with("\n <info>==</info> Previously Executed Unavailable Migration Versions\n")
;
$this->output->expects($this->at(18))
$this->output->expects($this->at(40))
->method('writeln')
->with(
sprintf(
Expand Down

0 comments on commit 5ebeb21

Please sign in to comment.