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\Migration; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ExecuteCommand extends AbstractCommand |
25
|
|
|
{ |
26
|
|
|
protected static $defaultName = 'mongodb:migrations:execute'; |
27
|
|
|
|
28
|
4 |
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
4 |
|
->setDescription('Execute a single migration version up or down manually.') |
32
|
4 |
|
->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null) |
33
|
4 |
|
->addOption('up', null, InputOption::VALUE_NONE, 'Execute the migration up.') |
34
|
4 |
|
->addOption('down', null, InputOption::VALUE_NONE, 'Execute the migration down.') |
35
|
4 |
|
->addOption('replay', null, InputOption::VALUE_NONE, 'Replay an \'up\' migration and avoid the duplicate exception.') |
36
|
4 |
|
->setHelp(<<<'EOT' |
37
|
4 |
|
The <info>%command.name%</info> command executes a single migration version up or down manually: |
38
|
|
|
|
39
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS</info> |
40
|
|
|
|
41
|
|
|
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up: |
42
|
|
|
|
43
|
|
|
<info>%command.full_name% YYYYMMDDHHMMSS --down</info> |
44
|
|
|
|
45
|
|
|
Or you can also execute the migration without a warning message which you need to interact with: |
46
|
|
|
|
47
|
|
|
<info>%command.full_name% --no-interaction</info> |
48
|
|
|
EOT |
49
|
|
|
); |
50
|
|
|
|
51
|
4 |
|
parent::configure(); |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface |
56
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface |
57
|
|
|
*/ |
58
|
4 |
|
public function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
4 |
|
$version = $input->getArgument('version'); |
61
|
4 |
|
$direction = $input->getOption('down') ? 'down' : 'up'; |
62
|
4 |
|
$replay = $input->getOption('replay'); |
63
|
|
|
|
64
|
4 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
65
|
4 |
|
$version = $configuration->getVersion($version); |
|
|
|
|
66
|
|
|
|
67
|
4 |
|
if (!$input->isInteractive()) { |
68
|
2 |
|
$version->execute($direction, $replay); |
69
|
|
|
} else { |
70
|
2 |
|
$question = new ConfirmationQuestion( |
71
|
2 |
|
'<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/[n])</question> ', |
72
|
2 |
|
false |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$confirmation = $this |
76
|
2 |
|
->getHelper('question') |
77
|
2 |
|
->ask($input, $output, $question); |
78
|
|
|
|
79
|
2 |
|
if (true === $confirmation) { |
80
|
1 |
|
$version->execute($direction, $replay); |
81
|
|
|
} else { |
82
|
1 |
|
$output->writeln('<error>Migration cancelled!</error>'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
4 |
|
} |
86
|
|
|
} |
87
|
|
|
|