-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
MigrateCommand.php
409 lines (328 loc) · 13.2 KB
/
MigrateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\CoreBundle\Command;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Migration\MigrationCollection;
use Contao\CoreBundle\Migration\MigrationResult;
use Contao\InstallationBundle\Database\Installer;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;
class MigrateCommand extends Command
{
protected static $defaultName = 'contao:migrate';
private MigrationCollection $migrations;
private FileLocator $fileLocator;
private string $projectDir;
private ContaoFramework $framework;
private ?Installer $installer;
private ?SymfonyStyle $io = null;
public function __construct(MigrationCollection $migrations, FileLocator $fileLocator, string $projectDir, ContaoFramework $framework, Installer $installer = null)
{
$this->migrations = $migrations;
$this->fileLocator = $fileLocator;
$this->projectDir = $projectDir;
$this->framework = $framework;
$this->installer = $installer;
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('with-deletes', null, InputOption::VALUE_NONE, 'Execute all database migrations including DROP queries. Can be used together with --no-interaction.')
->addOption('schema-only', null, InputOption::VALUE_NONE, 'Only update the database schema.')
->addOption('migrations-only', null, InputOption::VALUE_NONE, 'Only execute the migrations.')
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show pending migrations and schema updates without executing them.')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, ndjson)', 'txt')
->setDescription('Executes migrations and updates the database schema.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
if ('ndjson' !== $input->getOption('format')) {
return $this->executeCommand($input);
}
try {
return $this->executeCommand($input);
} catch (\Throwable $exception) {
$this->writeNdjson('error', [
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString(),
]);
}
return 1;
}
private function executeCommand(InputInterface $input): int
{
$dryRun = (bool) $input->getOption('dry-run');
$asJson = 'ndjson' === $input->getOption('format');
if (!\in_array($input->getOption('format'), ['txt', 'ndjson'], true)) {
throw new InvalidOptionException(sprintf('Unsupported format "%s".', $input->getOption('format')));
}
if ($asJson && !$dryRun && $input->isInteractive()) {
throw new InvalidOptionException('Use --no-interaction or --dry-run together with --format=ndjson');
}
if ($input->getOption('migrations-only')) {
if ($input->getOption('schema-only')) {
throw new InvalidOptionException('--migrations-only cannot be combined with --schema-only');
}
if ($input->getOption('with-deletes')) {
throw new InvalidOptionException('--migrations-only cannot be combined with --with-deletes');
}
return $this->executeMigrations($dryRun, $asJson) ? 0 : 1;
}
if ($input->getOption('schema-only')) {
return $this->executeSchemaDiff($dryRun, $asJson, $input->getOption('with-deletes')) ? 0 : 1;
}
if (!$this->executeMigrations($dryRun, $asJson)) {
return 1;
}
if (!$this->executeSchemaDiff($dryRun, $asJson, $input->getOption('with-deletes'))) {
return 1;
}
if (!$dryRun && !$this->executeMigrations(false, $asJson)) {
return 1;
}
if (!$asJson) {
$this->io->success('All migrations completed.');
}
return 0;
}
private function executeMigrations(bool $dryRun, bool $asJson): bool
{
while (true) {
$first = true;
foreach ($this->migrations->getPendingNames() as $migration) {
if ($first) {
if (!$asJson) {
$this->io->section('Pending migrations');
}
$first = false;
}
if ($asJson) {
$this->writeNdjson('migration-pending', ['name' => $migration]);
} else {
$this->io->writeln(' * '.$migration);
}
}
$runOnceFiles = $this->getRunOnceFiles();
if ($runOnceFiles) {
trigger_deprecation('contao/core-bundle', '4.9', 'Using "runonce.php" files has been deprecated and will no longer work in Contao 5.0. Use the migration framework instead.');
}
foreach ($runOnceFiles as $file) {
if ($first) {
if (!$asJson) {
$this->io->section('Pending migrations');
}
$first = false;
}
if ($asJson) {
$this->writeNdjson('migration-pending', ['name' => 'Runonce file: '.$file]);
} else {
$this->io->writeln(' * Runonce file: '.$file);
}
}
if ($first || $dryRun) {
break;
}
if (!$asJson) {
if (!$this->io->confirm('Execute the listed migrations?')) {
return false;
}
$this->io->section('Execute migrations');
}
$count = 0;
/** @var MigrationResult $result */
foreach ($this->migrations->run() as $result) {
++$count;
if ($asJson) {
$this->writeNdjson('migration-result', [
'message' => $result->getMessage(),
'isSuccessful' => $result->isSuccessful(),
]);
} else {
$this->io->writeln(' * '.$result->getMessage());
if (!$result->isSuccessful()) {
$this->io->error('Migration failed');
}
}
}
foreach ($this->getRunOnceFiles() as $file) {
++$count;
$this->executeRunonceFile($file);
if ($asJson) {
$this->writeNdjson('migration-result', [
'message' => 'Executed runonce file: '.$file,
'isSuccessful' => true,
]);
} else {
$this->io->writeln(' * Executed runonce file: '.$file);
}
}
if (!$asJson) {
$this->io->success('Executed '.$count.' migrations.');
}
}
return true;
}
private function getRunOnceFiles(): array
{
try {
$files = $this->fileLocator->locate('config/runonce.php', null, false);
} catch (FileLocatorFileNotFoundException $e) {
return [];
}
return array_map(fn ($path) => Path::makeRelative($path, $this->projectDir), $files);
}
private function executeRunonceFile(string $file): void
{
$this->framework->initialize();
$filePath = Path::join($this->projectDir, $file);
include $filePath;
(new Filesystem())->remove($filePath);
}
private function executeSchemaDiff(bool $dryRun, bool $asJson, bool $withDeletesOption): bool
{
if (null === $this->installer) {
$this->io->error('Service "contao.installer" not found. The installation bundle needs to be installed in order to execute schema diff migrations.');
return false;
}
$commandsByHash = [];
while (true) {
$this->installer->compileCommands();
if (!$commands = $this->installer->getCommands(false)) {
return true;
}
$hasNewCommands = \count(array_filter(
array_keys($commands),
static fn ($hash) => !isset($commandsByHash[$hash])
));
if (!$hasNewCommands) {
return true;
}
if (!$asJson) {
$this->io->section('Pending database migrations');
}
$commandsByHash = $commands;
if ($asJson) {
$this->writeNdjson('schema-pending', [
'commands' => array_values($commandsByHash),
]);
} else {
$this->io->listing($commandsByHash);
}
if ($dryRun) {
return true;
}
$options = $withDeletesOption
? ['yes, with deletes', 'no']
: ['yes', 'yes, with deletes', 'no'];
if ($asJson) {
$answer = $options[0];
} else {
$answer = $this->io->choice('Execute the listed database updates?', $options, $options[0]);
}
if ('no' === $answer) {
return false;
}
if (!$asJson) {
$this->io->section('Execute database migrations');
}
$count = 0;
$commandHashes = $this->getCommandHashes($commands, 'yes, with deletes' === $answer);
do {
$commandExecuted = false;
$exceptions = [];
foreach ($commandHashes as $key => $hash) {
if ($asJson) {
$this->writeNdjson('schema-execute', [
'command' => $commandsByHash[$hash],
]);
} else {
$this->io->write(' * '.$commandsByHash[$hash]);
}
try {
$this->installer->execCommand($hash);
++$count;
$commandExecuted = true;
unset($commandHashes[$key]);
if ($asJson) {
$this->writeNdjson('schema-result', [
'command' => $commandsByHash[$hash],
'isSuccessful' => true,
]);
} else {
$this->io->writeln('');
}
} catch (\Throwable $e) {
$exceptions[] = $e;
if ($asJson) {
$this->writeNdjson('schema-result', [
'command' => $commandsByHash[$hash],
'isSuccessful' => false,
'message' => $e->getMessage(),
]);
} else {
$this->io->writeln('......FAILED');
}
}
}
} while ($commandExecuted);
if (!$asJson) {
$this->io->success('Executed '.$count.' SQL queries.');
if (\count($exceptions)) {
foreach ($exceptions as $exception) {
$this->io->error($exception->getMessage());
}
}
}
if (\count($exceptions)) {
return false;
}
}
}
private function getCommandHashes(array $commands, bool $withDrops): array
{
if (!$withDrops) {
foreach ($commands as $hash => $command) {
if (
preg_match('/^ALTER TABLE [^ ]+ DROP /', $command, $matches)
|| (0 === strncmp($command, 'DROP ', 5) && 0 !== strncmp($command, 'DROP INDEX', 10))
) {
unset($commands[$hash]);
}
}
}
return array_keys($commands);
}
private function writeNdjson(string $type, array $data): void
{
$this->io->writeln(
json_encode(
array_merge(['type' => $type], $data, ['type' => $type]),
JSON_INVALID_UTF8_SUBSTITUTE
)
);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \JsonException(json_last_error_msg());
}
}
}