1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
5
|
|
|
* |
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command; |
13
|
|
|
|
14
|
|
|
use AntiMattr\MongoDB\Migrations\Configuration\Configuration; |
15
|
|
|
use AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException; |
16
|
|
|
use AntiMattr\MongoDB\Migrations\Migration; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class VersionCommand extends AbstractCommand |
26
|
|
|
{ |
27
|
|
|
protected static $defaultName = 'mongodb:migrations:version'; |
28
|
|
|
|
29
|
6 |
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
6 |
|
->setDescription('Manually add and delete migration versions from the version table.') |
33
|
6 |
|
->addArgument('version', InputArgument::REQUIRED, 'The version to add or delete.', null) |
34
|
6 |
|
->addOption('add', null, InputOption::VALUE_NONE, 'Add the specified version.') |
35
|
6 |
|
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the specified version.') |
36
|
6 |
|
->setHelp(<<<'EOT' |
37
|
6 |
|
The <info>%command.name%</info> command allows you to manually add and delete migration versions from the version table: |
38
|
|
|
|
39
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --add</info> |
40
|
|
|
|
41
|
|
|
If you want to delete a version you can use the <comment>--delete</comment> option: |
42
|
|
|
|
43
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --delete</info> |
44
|
|
|
EOT |
45
|
|
|
); |
46
|
|
|
|
47
|
6 |
|
parent::configure(); |
48
|
6 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface |
52
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface |
53
|
|
|
* |
54
|
|
|
* @throws UnknownVersionException Throws exception if migration version does not exist |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
6 |
|
public function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
6 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
60
|
6 |
|
$migration = $this->createMigration($configuration); |
|
|
|
|
61
|
|
|
|
62
|
6 |
|
if (false === $input->getOption('add') && false === $input->getOption('delete')) { |
63
|
1 |
|
throw new \InvalidArgumentException('You must specify whether you want to --add or --delete the specified version.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
$version = $input->getArgument('version'); |
67
|
5 |
|
$markMigrated = $input->getOption('add') ? true : false; |
68
|
|
|
|
69
|
5 |
|
if (!$configuration->hasVersion($version)) { |
|
|
|
|
70
|
1 |
|
throw new UnknownVersionException($version); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
$version = $configuration->getVersion($version); |
|
|
|
|
74
|
4 |
|
if ($markMigrated && $configuration->hasVersionMigrated($version)) { |
75
|
1 |
|
throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version collection.', $version)); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
if (!$markMigrated && !$configuration->hasVersionMigrated($version)) { |
79
|
1 |
|
throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version collection.', $version)); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if ($markMigrated) { |
83
|
1 |
|
$version->markMigrated(); |
84
|
|
|
} else { |
85
|
1 |
|
$version->markNotMigrated(); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function createMigration(Configuration $configuration) |
92
|
|
|
{ |
93
|
|
|
return new Migration($configuration); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|