Skip to content

Commit 5ebeb21

Browse files
authored
Merge pull request #10 from doesntmattr/feature/status-descriptions
Feature/status descriptions
2 parents c200d63 + 365ba92 commit 5ebeb21

File tree

4 files changed

+119
-39
lines changed

4 files changed

+119
-39
lines changed

src/AntiMattr/MongoDB/Migrations/Configuration/Configuration.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,38 @@ public function getMigratedVersions()
295295
return $versions;
296296
}
297297

298+
/**
299+
* Returns the time a migration occurred.
300+
*
301+
* @param string $version
302+
*
303+
* @return string
304+
*
305+
* @throws AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException Throws exception if migration version does not exist
306+
*/
307+
public function getMigratedTimestamp($version)
308+
{
309+
$this->createMigrationCollection();
310+
311+
$cursor = $this->getCollection()->find(
312+
array('v' => $version)
313+
);
314+
315+
if (!$cursor->count()) {
316+
throw new UnknownVersionException($version);
317+
}
318+
319+
if ($cursor->count() > 1) {
320+
throw \DomainException(
321+
'Unexpected duplicate version records in the database'
322+
);
323+
}
324+
325+
$returnVersion = $cursor->getNext();
326+
327+
return (string) $returnVersion['t'];
328+
}
329+
298330
/**
299331
* Return all migrated versions from versions collection that have migration files deleted.
300332
*

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/AbstractCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ abstract class AbstractCommand extends Command
2929
*/
3030
private $configuration;
3131

32+
/**
33+
* configure.
34+
*/
3235
protected function configure()
3336
{
34-
$this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
35-
$this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.');
37+
$this->addOption(
38+
'configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'
39+
);
40+
$this->addOption(
41+
'db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'
42+
);
3643
}
3744

3845
/**

src/AntiMattr/MongoDB/Migrations/Tools/Console/Command/StatusCommand.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Helper\Table;
2021

2122
/**
2223
* @author Matthew Fitzgerald <matthewfitz@gmail.com>
@@ -30,7 +31,12 @@ protected function configure()
3031
$this
3132
->setName($this->getName())
3233
->setDescription('View the status of a set of migrations.')
33-
->addOption('show-versions', null, InputOption::VALUE_NONE, 'This will display a list of all available migrations and their status')
34+
->addOption(
35+
'show-versions',
36+
null,
37+
InputOption::VALUE_NONE,
38+
'This will display a list of all available migrations and their status'
39+
)
3440
->setHelp(<<<'EOT'
3541
The <info>%command.name%</info> command outputs the status of a set of migrations:
3642
@@ -98,27 +104,58 @@ public function execute(InputInterface $input, OutputInterface $output)
98104
'Available Migrations' => $configMap['num_available_migrations'],
99105
'New Migrations' => $numNewMigrations > 0 ? '<question>'.$numNewMigrations.'</question>' : 0,
100106
);
107+
101108
foreach ($info as $name => $value) {
102109
$this->writeInfoLine($output, $name, $value);
103110
}
104111

105-
$showVersions = $input->getOption('show-versions') ? true : false;
106-
if ($showVersions === true) {
112+
if ($input->getOption('show-versions')) {
107113
if ($migrations = $configuration->getMigrations()) {
108114
$output->writeln("\n <info>==</info> Available Migration Versions\n");
115+
$rows = array();
109116
$migratedVersions = $configuration->getMigratedVersions();
117+
110118
foreach ($migrations as $version) {
111119
$isMigrated = in_array($version->getVersion(), $migratedVersions);
112-
$status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
113-
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($version->getVersion()).' (<comment>'.$version->getVersion().'</comment>)'.str_repeat(' ', 30 - strlen($name)).$status);
120+
$status = '<error>not migrated</error>';
121+
122+
if ($isMigrated) {
123+
$ts = $configuration->getMigratedTimestamp(
124+
$version->getVersion()
125+
);
126+
127+
$status = sprintf(
128+
'<info>%s</info>',
129+
date('Y-m-d H:i', $ts)
130+
);
131+
}
132+
133+
$versionTxt = sprintf('<comment>%s</comment>', $version->getVersion());
134+
$desc = $version->getMigration()->getDescription();
135+
if (strlen($desc) > 80) {
136+
$desc = substr($desc, 0, 78).'...';
137+
}
138+
139+
$rows[] = array($versionTxt, $status, $desc);
114140
}
141+
142+
$table = new Table($output);
143+
$table->setHeaders(array('Version', 'Date Migrated', 'Description'))
144+
->setRows($rows)
145+
->render();
115146
}
116147

117148
$executedUnavailableMigrations = $configuration->getUnavailableMigratedVersions();
118149
if ($executedUnavailableMigrations) {
119150
$output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
120151
foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
121-
$output->writeln(' <comment>>></comment> '.Configuration::formatVersion($executedUnavailableMigration).' (<comment>'.$executedUnavailableMigration.'</comment>)');
152+
$output->writeln(
153+
sprintf(
154+
' <comment>>></comment> %s (<comment>%s</comment>)',
155+
Configuration::formatVersion($executedUnavailableMigration),
156+
$executedUnavailableMigration
157+
)
158+
);
122159
}
123160
}
124161
}
@@ -141,6 +178,11 @@ protected function writeInfoLine(OutputInterface $output, $name, $value)
141178
);
142179
}
143180

181+
/**
182+
* getName.
183+
*
184+
* @return string
185+
*/
144186
public function getName()
145187
{
146188
return self::NAME;

tests/AntiMattr/Tests/MongoDB/Migrations/Tools/Console/Command/StatusCommandTest.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ protected function setUp()
2626
{
2727
$this->command = new StatusCommandStub();
2828
$this->output = $this->buildMock('Symfony\Component\Console\Output\OutputInterface');
29+
$this->outputFormatter = $this->buildMock(
30+
'Symfony\Component\Console\Formatter\OutputFormatterInterface'
31+
);
2932
$this->config = $this->buildMock('AntiMattr\MongoDB\Migrations\Configuration\Configuration');
30-
$this->migration = $this->buildMock('AntiMattr\MongoDB\Migrations\Migration');
33+
$this->migration = $this->buildMock('AntiMattr\MongoDB\Migrations\AbstractMigration');
3134
$this->version = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
35+
$this->version->expects($this->any())
36+
->method('getMigration')
37+
->will($this->returnValue($this->migration));
38+
3239
$this->version2 = $this->buildMock('AntiMattr\MongoDB\Migrations\Version');
40+
$this->version2->expects($this->any())
41+
->method('getMigration')
42+
->will($this->returnValue($this->migration));
3343

3444
$this->command->setMigrationConfiguration($this->config);
3545
}
@@ -239,17 +249,26 @@ public function testExecuteWithShowingVersions()
239249
$numNewMigrations = 1;
240250
$notMigratedVersion = '20140822185743';
241251
$migratedVersion = '20140822185745';
252+
$migrationDescription = 'drop all collections';
242253
$unavailableMigratedVersion = '20140822185744';
243254

244255
// Expectations
245-
$this->version->expects($this->exactly(3))
256+
$this->output
257+
->method('getFormatter')
258+
->will($this->returnValue($this->outputFormatter));
259+
260+
$this->version->expects($this->exactly(2))
246261
->method('getVersion')
247262
->will($this->returnValue($notMigratedVersion));
248263

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

268+
$this->migration
269+
->method('getDescription')
270+
->will($this->returnValue('drop all'));
271+
253272
$this->config->expects($this->once())
254273
->method('getDetailsMap')
255274
->will(
@@ -272,30 +291,29 @@ public function testExecuteWithShowingVersions()
272291
)
273292
;
274293
$this->config->expects($this->once())
275-
->method('getMigrations')
294+
->method('getUnavailableMigratedVersions')
276295
->will(
277296
$this->returnValue(
278-
array($this->version, $this->version2)
297+
array($unavailableMigratedVersion)
279298
)
280299
)
281300
;
282301
$this->config->expects($this->once())
283-
->method('getMigratedVersions')
302+
->method('getMigrations')
284303
->will(
285304
$this->returnValue(
286-
array($unavailableMigratedVersion, $migratedVersion)
305+
array($this->version, $this->version2)
287306
)
288307
)
289308
;
290309
$this->config->expects($this->once())
291-
->method('getUnavailableMigratedVersions')
310+
->method('getMigratedVersions')
292311
->will(
293312
$this->returnValue(
294-
array($unavailableMigratedVersion)
313+
array($unavailableMigratedVersion, $migratedVersion)
295314
)
296315
)
297316
;
298-
299317
$this->output->expects($this->at(0))
300318
->method('writeln')
301319
->with(
@@ -422,31 +440,12 @@ public function testExecuteWithShowingVersions()
422440
->method('writeln')
423441
->with("\n <info>==</info> Available Migration Versions\n")
424442
;
425-
$this->output->expects($this->at(15))
426-
->method('writeln')
427-
->with(
428-
sprintf(
429-
' <comment>>></comment> %s (<comment>%s</comment>) <error>not migrated</error>',
430-
\DateTime::createFromFormat('YmdHis', $notMigratedVersion)->format('Y-m-d H:i:s'),
431-
$notMigratedVersion
432-
)
433-
)
434-
;
435-
$this->output->expects($this->at(16))
436-
->method('writeln')
437-
->with(
438-
sprintf(
439-
' <comment>>></comment> %s (<comment>%s</comment>) <info>migrated</info>',
440-
\DateTime::createFromFormat('YmdHis', $migratedVersion)->format('Y-m-d H:i:s'),
441-
$migratedVersion
442-
)
443-
)
444-
;
445-
$this->output->expects($this->at(17))
443+
444+
$this->output->expects($this->at(39))
446445
->method('writeln')
447446
->with("\n <info>==</info> Previously Executed Unavailable Migration Versions\n")
448447
;
449-
$this->output->expects($this->at(18))
448+
$this->output->expects($this->at(40))
450449
->method('writeln')
451450
->with(
452451
sprintf(

0 commit comments

Comments
 (0)