1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Version\Direction; |
8
|
|
|
use Doctrine\Migrations\Version\Version; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use function getcwd; |
14
|
|
|
use function is_string; |
15
|
|
|
use function is_writable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The ExecutCommand class is responsible for executing a single migration version up or down. |
19
|
|
|
*/ |
20
|
|
|
class ExecuteCommand extends DoctrineCommand |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
protected static $defaultName = 'migrations:execute'; |
24
|
|
|
|
25
|
4 |
|
protected function configure() : void |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
4 |
|
->setAliases(['execute']) |
29
|
4 |
|
->setDescription( |
30
|
4 |
|
'Execute a single migration version up or down manually.' |
31
|
|
|
) |
32
|
4 |
|
->addArgument( |
33
|
4 |
|
'version', |
34
|
4 |
|
InputArgument::REQUIRED, |
35
|
4 |
|
'The version to execute.', |
36
|
4 |
|
null |
37
|
|
|
) |
38
|
4 |
|
->addOption( |
39
|
4 |
|
'write-sql', |
40
|
4 |
|
null, |
41
|
4 |
|
InputOption::VALUE_OPTIONAL, |
42
|
4 |
|
'The path to output the migration SQL file instead of executing it. Defaults to current working directory.', |
43
|
4 |
|
false |
44
|
|
|
) |
45
|
4 |
|
->addOption( |
46
|
4 |
|
'dry-run', |
47
|
4 |
|
null, |
48
|
4 |
|
InputOption::VALUE_NONE, |
49
|
4 |
|
'Execute the migration as a dry run.' |
50
|
|
|
) |
51
|
4 |
|
->addOption( |
52
|
4 |
|
'up', |
53
|
4 |
|
null, |
54
|
4 |
|
InputOption::VALUE_NONE, |
55
|
4 |
|
'Execute the migration up.' |
56
|
|
|
) |
57
|
4 |
|
->addOption( |
58
|
4 |
|
'down', |
59
|
4 |
|
null, |
60
|
4 |
|
InputOption::VALUE_NONE, |
61
|
4 |
|
'Execute the migration down.' |
62
|
|
|
) |
63
|
4 |
|
->addOption( |
64
|
4 |
|
'query-time', |
65
|
4 |
|
null, |
66
|
4 |
|
InputOption::VALUE_NONE, |
67
|
4 |
|
'Time all the queries individually.' |
68
|
|
|
) |
69
|
4 |
|
->setHelp(<<<EOT |
70
|
4 |
|
The <info>%command.name%</info> command executes a single migration version up or down manually: |
71
|
|
|
|
72
|
|
|
<info>%command.full_name% FQCN</info> |
73
|
|
|
|
74
|
|
|
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up: |
75
|
|
|
|
76
|
|
|
<info>%command.full_name% FQCN --down</info> |
77
|
|
|
|
78
|
|
|
You can also execute the migration as a <comment>--dry-run</comment>: |
79
|
|
|
|
80
|
|
|
<info>%command.full_name% FQCN --dry-run</info> |
81
|
|
|
|
82
|
|
|
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>: |
83
|
|
|
|
84
|
|
|
<info>%command.full_name% FQCN --write-sql</info> |
85
|
|
|
|
86
|
|
|
Or you can also execute the migration without a warning message which you need to interact with: |
87
|
|
|
|
88
|
|
|
<info>%command.full_name% FQCN --no-interaction</info> |
89
|
|
|
EOT |
90
|
|
|
); |
91
|
|
|
|
92
|
4 |
|
parent::configure(); |
93
|
4 |
|
} |
94
|
|
|
|
95
|
4 |
|
public function execute(InputInterface $input, OutputInterface $output) : ?int |
96
|
|
|
{ |
97
|
4 |
|
$version = $input->getArgument('version'); |
98
|
4 |
|
$path = $input->getOption('write-sql'); |
99
|
4 |
|
$direction = $input->getOption('down') !== false |
100
|
4 |
|
? Direction::DOWN |
101
|
4 |
|
: Direction::UP; |
102
|
|
|
|
103
|
4 |
|
$migrator = $this->getDependencyFactory()->getMigrator(); |
104
|
4 |
|
$plan = $this->getDependencyFactory()->getMigrationPlanCalculator()->getPlanForExactVersion(new Version($version), $direction); |
|
|
|
|
105
|
|
|
|
106
|
4 |
|
$migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory(); |
107
|
4 |
|
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input); |
108
|
|
|
|
109
|
4 |
|
if ($path !== false) { |
110
|
2 |
|
$migratorConfiguration->setDryRun(true); |
111
|
2 |
|
$sql = $migrator->migrate($plan, $migratorConfiguration); |
112
|
|
|
|
113
|
2 |
|
$path = is_string($path) ? $path : getcwd(); |
114
|
|
|
|
115
|
2 |
|
if (! is_string($path) || ! is_writable($path)) { |
|
|
|
|
116
|
|
|
$output->writeln('<error>Path not writeable!</error>'); |
117
|
|
|
|
118
|
|
|
return 1; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$writer = $this->getDependencyFactory()->getQueryWriter(); |
122
|
2 |
|
$writer->write($path, $direction, $sql); |
123
|
|
|
|
124
|
2 |
|
return 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)'; |
128
|
|
|
|
129
|
2 |
|
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input, $output)) { |
130
|
1 |
|
$output->writeln('<error>Migration cancelled!</error>'); |
131
|
|
|
|
132
|
1 |
|
return 1; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized(); |
136
|
1 |
|
$migrator->migrate($plan, $migratorConfiguration); |
137
|
|
|
|
138
|
1 |
|
return 0; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|