|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria; |
|
8
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsToExecute; |
|
9
|
|
|
use Doctrine\Migrations\Exception\UnknownMigrationVersion; |
|
10
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigrationsList; |
|
11
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use function count; |
|
17
|
|
|
use function getcwd; |
|
18
|
|
|
use function in_array; |
|
19
|
|
|
use function is_string; |
|
20
|
|
|
use function is_writable; |
|
21
|
|
|
use function sprintf; |
|
22
|
|
|
use function substr; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The MigrateCommand class is responsible for executing a migration from the current version to another |
|
26
|
|
|
* version up or down. It will calculate all the migration versions that need to be executed and execute them. |
|
27
|
|
|
*/ |
|
28
|
|
|
final class MigrateCommand extends DoctrineCommand |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected static $defaultName = 'migrations:migrate'; |
|
32
|
|
|
|
|
33
|
20 |
|
protected function configure() : void |
|
34
|
|
|
{ |
|
35
|
|
|
$this |
|
36
|
20 |
|
->setAliases(['migrate']) |
|
37
|
20 |
|
->setDescription( |
|
38
|
20 |
|
'Execute a migration to a specified version or the latest available version.' |
|
39
|
|
|
) |
|
40
|
20 |
|
->addArgument( |
|
41
|
20 |
|
'version', |
|
42
|
20 |
|
InputArgument::OPTIONAL, |
|
43
|
20 |
|
'The version FQCN or alias (first, prev, next, latest) to migrate to.', |
|
44
|
20 |
|
'latest' |
|
45
|
|
|
) |
|
46
|
20 |
|
->addOption( |
|
47
|
20 |
|
'write-sql', |
|
48
|
20 |
|
null, |
|
49
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
50
|
20 |
|
'The path to output the migration SQL file instead of executing it. Defaults to current working directory.', |
|
51
|
20 |
|
false |
|
52
|
|
|
) |
|
53
|
20 |
|
->addOption( |
|
54
|
20 |
|
'dry-run', |
|
55
|
20 |
|
null, |
|
56
|
20 |
|
InputOption::VALUE_NONE, |
|
57
|
20 |
|
'Execute the migration as a dry run.' |
|
58
|
|
|
) |
|
59
|
20 |
|
->addOption( |
|
60
|
20 |
|
'query-time', |
|
61
|
20 |
|
null, |
|
62
|
20 |
|
InputOption::VALUE_NONE, |
|
63
|
20 |
|
'Time all the queries individually.' |
|
64
|
|
|
) |
|
65
|
20 |
|
->addOption( |
|
66
|
20 |
|
'allow-no-migration', |
|
67
|
20 |
|
null, |
|
68
|
20 |
|
InputOption::VALUE_NONE, |
|
69
|
20 |
|
'Do not throw an exception if no migration is available.' |
|
70
|
|
|
) |
|
71
|
20 |
|
->addOption( |
|
72
|
20 |
|
'all-or-nothing', |
|
73
|
20 |
|
null, |
|
74
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
75
|
20 |
|
'Wrap the entire migration in a transaction.', |
|
76
|
20 |
|
false |
|
77
|
|
|
) |
|
78
|
20 |
|
->setHelp(<<<EOT |
|
79
|
20 |
|
The <info>%command.name%</info> command executes a migration to a specified version or the latest available version: |
|
80
|
|
|
|
|
81
|
|
|
<info>%command.full_name%</info> |
|
82
|
|
|
|
|
83
|
|
|
You can optionally manually specify the version you wish to migrate to: |
|
84
|
|
|
|
|
85
|
|
|
<info>%command.full_name% FQCN</info> |
|
86
|
|
|
|
|
87
|
|
|
You can specify the version you wish to migrate to using an alias: |
|
88
|
|
|
|
|
89
|
|
|
<info>%command.full_name% prev</info> |
|
90
|
|
|
<info>These alias are defined : first, latest, prev, current and next</info> |
|
91
|
|
|
|
|
92
|
|
|
You can specify the version you wish to migrate to using an number against the current version: |
|
93
|
|
|
|
|
94
|
|
|
<info>%command.full_name% current+3</info> |
|
95
|
|
|
|
|
96
|
|
|
You can also execute the migration as a <comment>--dry-run</comment>: |
|
97
|
|
|
|
|
98
|
|
|
<info>%command.full_name% FQCN --dry-run</info> |
|
99
|
|
|
|
|
100
|
|
|
You can output the would be executed SQL statements to a file with <comment>--write-sql</comment>: |
|
101
|
|
|
|
|
102
|
|
|
<info>%command.full_name% FQCN --write-sql</info> |
|
103
|
|
|
|
|
104
|
|
|
Or you can also execute the migration without a warning message which you need to interact with: |
|
105
|
|
|
|
|
106
|
|
|
<info>%command.full_name% --no-interaction</info> |
|
107
|
|
|
|
|
108
|
|
|
You can also time all the different queries if you wanna know which one is taking so long: |
|
109
|
|
|
|
|
110
|
|
|
<info>%command.full_name% --query-time</info> |
|
111
|
|
|
|
|
112
|
|
|
Use the --all-or-nothing option to wrap the entire migration in a transaction. |
|
113
|
|
|
EOT |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
20 |
|
parent::configure(); |
|
117
|
20 |
|
} |
|
118
|
|
|
|
|
119
|
20 |
|
protected function execute(InputInterface $input, OutputInterface $output) : int |
|
120
|
|
|
{ |
|
121
|
20 |
|
$migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory(); |
|
122
|
20 |
|
$migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input); |
|
123
|
|
|
|
|
124
|
20 |
|
$question = 'WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue?'; |
|
125
|
20 |
|
if (! $migratorConfiguration->isDryRun() && ! $this->canExecute($question, $input)) { |
|
126
|
3 |
|
$this->io->error('Migration cancelled!'); |
|
127
|
|
|
|
|
128
|
3 |
|
return 3; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
17 |
|
$this->getDependencyFactory()->getMetadataStorage()->ensureInitialized(); |
|
132
|
|
|
|
|
133
|
17 |
|
$allowNoMigration = $input->getOption('allow-no-migration'); |
|
134
|
17 |
|
$versionAlias = $input->getArgument('version'); |
|
135
|
17 |
|
$path = $input->getOption('write-sql'); |
|
136
|
|
|
|
|
137
|
|
|
try { |
|
138
|
17 |
|
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias($versionAlias); |
|
|
|
|
|
|
139
|
5 |
|
} catch (UnknownMigrationVersion|NoMigrationsToExecute|NoMigrationsFoundWithCriteria $e) { |
|
140
|
5 |
|
return $this->errorForAlias($versionAlias, $allowNoMigration); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
12 |
|
$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator(); |
|
144
|
12 |
|
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator(); |
|
145
|
12 |
|
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations(); |
|
146
|
|
|
|
|
147
|
12 |
|
if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input) === false) { |
|
148
|
1 |
|
return 3; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
11 |
|
$plan = $planCalculator->getPlanUntilVersion($version); |
|
152
|
|
|
|
|
153
|
11 |
|
if (count($plan) === 0) { |
|
154
|
5 |
|
return $this->errorForAlias($versionAlias, $allowNoMigration); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
6 |
|
$migrator = $this->getDependencyFactory()->getMigrator(); |
|
158
|
6 |
|
if ($migratorConfiguration->isDryRun()) { |
|
159
|
2 |
|
$sql = $migrator->migrate($plan, $migratorConfiguration); |
|
160
|
|
|
|
|
161
|
2 |
|
$path = is_string($path) ? $path : getcwd(); |
|
162
|
|
|
|
|
163
|
2 |
|
if (! is_string($path) || ! is_writable($path)) { |
|
|
|
|
|
|
164
|
|
|
$this->io->error('Path not writeable!'); |
|
165
|
|
|
|
|
166
|
|
|
return 1; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
$writer = $this->getDependencyFactory()->getQueryWriter(); |
|
170
|
2 |
|
$writer->write($path, $plan->getDirection(), $sql); |
|
171
|
|
|
|
|
172
|
2 |
|
return 0; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
4 |
|
$this->getDependencyFactory()->getLogger()->notice( |
|
176
|
4 |
|
'Migrating' . ($migratorConfiguration->isDryRun() ? ' (dry-run)' : '') . ' {direction} to {to}', |
|
177
|
|
|
[ |
|
178
|
4 |
|
'direction' => $plan->getDirection(), |
|
179
|
4 |
|
'to' => (string) $version, |
|
180
|
|
|
] |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
4 |
|
$migrator->migrate($plan, $migratorConfiguration); |
|
184
|
|
|
|
|
185
|
4 |
|
$this->io->newLine(); |
|
186
|
|
|
|
|
187
|
4 |
|
return 0; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
12 |
|
private function checkExecutedUnavailableMigrations( |
|
191
|
|
|
ExecutedMigrationsList $executedUnavailableMigrations, |
|
192
|
|
|
InputInterface $input |
|
193
|
|
|
) : bool { |
|
194
|
12 |
|
if (count($executedUnavailableMigrations) !== 0) { |
|
195
|
1 |
|
$this->io->warning(sprintf( |
|
196
|
1 |
|
'You have %s previously executed migrations in the database that are not registered migrations.', |
|
197
|
1 |
|
count($executedUnavailableMigrations) |
|
198
|
|
|
)); |
|
199
|
|
|
|
|
200
|
1 |
|
foreach ($executedUnavailableMigrations->getItems() as $executedUnavailableMigration) { |
|
201
|
1 |
|
$this->io->text(sprintf( |
|
202
|
1 |
|
'<comment>>></comment> %s (<comment>%s</comment>)', |
|
203
|
1 |
|
$executedUnavailableMigration->getExecutedAt() !== null |
|
204
|
|
|
? $executedUnavailableMigration->getExecutedAt()->format('Y-m-d H:i:s') |
|
205
|
1 |
|
: null, |
|
206
|
1 |
|
$executedUnavailableMigration->getVersion() |
|
207
|
|
|
)); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
1 |
|
$question = 'Are you sure you wish to continue?'; |
|
211
|
|
|
|
|
212
|
1 |
|
if (! $this->canExecute($question, $input)) { |
|
213
|
1 |
|
$this->io->error('Migration cancelled!'); |
|
214
|
|
|
|
|
215
|
1 |
|
return false; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
11 |
|
return true; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
10 |
|
private function errorForAlias(string $versionAlias, bool $allowNoMigration) : int |
|
223
|
|
|
{ |
|
224
|
10 |
|
if (in_array($versionAlias, ['first', 'next', 'latest'], true) || substr($versionAlias, 0, 7) === 'current') { |
|
225
|
8 |
|
$version = $this->getDependencyFactory()->getVersionAliasResolver()->resolveVersionAlias('current'); |
|
226
|
|
|
|
|
227
|
8 |
|
$message = sprintf( |
|
228
|
8 |
|
'The version "%s" couldn\'t be reached, you are at version "%s"', |
|
229
|
8 |
|
$versionAlias, |
|
230
|
8 |
|
(string) $version |
|
231
|
|
|
); |
|
232
|
|
|
|
|
233
|
8 |
|
if ($allowNoMigration) { |
|
234
|
4 |
|
$this->io->warning($message); |
|
235
|
|
|
|
|
236
|
4 |
|
return 0; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
4 |
|
$this->io->error($message); |
|
240
|
|
|
|
|
241
|
4 |
|
return 1; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
2 |
|
$this->io->error(sprintf( |
|
245
|
2 |
|
'Unknown version: %s', |
|
246
|
2 |
|
OutputFormatter::escape($versionAlias) |
|
247
|
|
|
)); |
|
248
|
|
|
|
|
249
|
2 |
|
return 1; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|