1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
10
|
|
|
use Kaliop\eZMigrationBundle\API\Value\Migration; |
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Command to execute the available migration definitions. |
15
|
|
|
*/ |
16
|
|
|
class MigrateCommand extends AbstractCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Set up the command. |
20
|
|
|
* |
21
|
|
|
* Define the name, options and help text. |
22
|
|
|
*/ |
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
parent::configure(); |
26
|
|
|
|
27
|
|
|
$this |
28
|
|
|
->setName('kaliop:migration:migrate') |
29
|
|
|
->setAliases(array('kaliop:migration:update')) |
30
|
|
|
->setDescription('Execute available migration definitions.') |
31
|
|
|
->addOption( |
32
|
|
|
'path', |
33
|
|
|
null, |
34
|
|
|
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
35
|
|
|
"The directory or file to load the migration definitions from" |
36
|
|
|
) |
37
|
|
|
->addOption('ignore-failures', null, InputOption::VALUE_NONE, "Keep executing migration even if one fails") |
38
|
|
|
->addOption('clear-cache', null, InputOption::VALUE_NONE, "Clear the cache after the command finishes") |
39
|
|
|
->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question") |
40
|
|
|
->setHelp(<<<EOT |
41
|
|
|
The <info>kaliop:migration:migrate</info> command loads and executes migrations: |
42
|
|
|
|
43
|
|
|
<info>./ezpublish/console kaliop:migration:migrate</info> |
44
|
|
|
|
45
|
|
|
You can optionally specify the path to migration definitions with <info>--path</info>: |
46
|
|
|
|
47
|
|
|
<info>./ezpublish/console kaliop:migrations:migrate --path=/path/to/bundle/version_directory --path=/path/to/bundle/version_directory/single_migration_file</info> |
48
|
|
|
EOT |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute the command. |
54
|
|
|
* |
55
|
|
|
* @param InputInterface $input |
56
|
|
|
* @param OutputInterface $output |
57
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
58
|
|
|
* |
59
|
|
|
* @todo Add functionality to work with specified version files not just directories. |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$migrationsService = $this->getMigrationService(); |
64
|
|
|
|
65
|
|
|
$migrationDefinitions = $migrationsService->getMigrationsDefinitions($input->getOption('path')); |
66
|
|
|
$migrations = $migrationsService->getMigrations(); |
67
|
|
|
|
68
|
|
|
// filter away all migrations except 'to do' ones |
69
|
|
|
$toExecute = array(); |
70
|
|
|
foreach($migrationDefinitions as $name => $migrationDefinition) { |
71
|
|
|
if (!isset($migrations[$name]) || (($migration = $migrations[$name]) && $migration->status == Migration::STATUS_TODO)) { |
72
|
|
|
$toExecute[$name] = $migrationsService->parseMigrationDefinition($migrationDefinition); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// if user wants to execute 'all' migrations: look for some which are registered in the database even if not |
77
|
|
|
// found by the loader |
78
|
|
|
if (empty($input->getOption('path'))) { |
79
|
|
|
foreach ($migrations as $migration) { |
80
|
|
|
if ($migration->status == Migration::STATUS_TODO && !isset($toExecute[$migration->name])) { |
81
|
|
|
$migrationDefinitions = $migrationsService->getMigrationsDefinitions(array($migration->path)); |
82
|
|
|
if (count($migrationDefinitions)) { |
83
|
|
|
$migrationDefinition = reset($migrationDefinitions); |
84
|
|
|
$toExecute[$migration->name] = $migrationsService->parseMigrationDefinition($migrationDefinition); |
85
|
|
|
} else { |
|
|
|
|
86
|
|
|
// q: shall we raise a warning here ? |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
ksort($toExecute); |
93
|
|
|
|
94
|
|
|
if (!count($toExecute)) { |
95
|
|
|
$output->writeln('<info>No migrations to execute</info>'); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$output->writeln("\n <info>==</info> Migrations to be executed\n"); |
100
|
|
|
|
101
|
|
|
$data = array(); |
102
|
|
|
$i = 1; |
103
|
|
|
foreach($toExecute as $name => $migrationDefinition) { |
104
|
|
|
$notes = ''; |
105
|
|
|
if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) { |
106
|
|
|
$notes = '<error>' . $migrationDefinition->parsingError . '</error>'; |
107
|
|
|
} |
108
|
|
|
$data[] = array( |
109
|
|
|
$i++, |
110
|
|
|
$name, |
111
|
|
|
$notes |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$table = $this->getHelperSet()->get('table'); |
116
|
|
|
$table |
117
|
|
|
->setHeaders(array('#', 'Migration', 'Notes')) |
118
|
|
|
->setRows($data); |
119
|
|
|
$table->render($output); |
120
|
|
|
|
121
|
|
|
$output->writeln(''); |
122
|
|
|
// ask user for confirmation to make changes |
123
|
|
View Code Duplication |
if ($input->isInteractive() && !$input->getOption('no-interaction')) { |
|
|
|
|
124
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
125
|
|
|
if (!$dialog->askConfirmation( |
126
|
|
|
$output, |
127
|
|
|
'<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', |
128
|
|
|
false |
129
|
|
|
) |
130
|
|
|
) { |
131
|
|
|
$output->writeln('<error>Migration execution cancelled!</error>'); |
132
|
|
|
return 0; |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$output->writeln("=============================================\n"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
foreach($toExecute as $name => $migrationDefinition) { |
139
|
|
|
|
140
|
|
|
// let's skip migrations that we know are invalid - user was warned and he decided to proceed anyway |
141
|
|
|
if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) { |
142
|
|
|
$output->writeln("<comment>Skipping $name</comment>\n"); |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$output->writeln("<info>Processing $name</info>"); |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
$migrationsService->executeMigration($migrationDefinition); |
150
|
|
|
} catch(\Exception $e) { |
151
|
|
|
if ($input->getOption('ignore-failures')) { |
152
|
|
|
$output->writeln("\n<error>Migration failed! Reason: ".$this->getFullExceptionMessage($e)."</error>\n"); |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
$output->writeln("\n<error>Migration aborted! Reason: ".$this->getFullExceptionMessage($e)."</error>"); |
156
|
|
|
return 1; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$output->writeln(''); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ($input->getOption('clear-cache')) { |
163
|
|
|
$command = $this->getApplication()->find('cache:clear'); |
164
|
|
|
$inputArray = new ArrayInput(array('command' => 'cache:clear')); |
165
|
|
|
$command->run($inputArray, $output); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Turns eZPublish cryptic exceptions into something more palatable for random devs |
171
|
|
|
* @todo should this be moved to a lower layer ? |
172
|
|
|
* |
173
|
|
|
* @param \Exception $e |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function getFullExceptionMessage(\Exception $e) |
177
|
|
|
{ |
178
|
|
|
$message = $e->getMessage(); |
179
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException') || |
180
|
|
|
is_a($e, '\eZ\Publish\API\Repository\Exceptions\ContentTypeFieldValidationException') || |
181
|
|
|
is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException') |
182
|
|
|
) { |
183
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException')) { |
184
|
|
|
$errorsArray = array($e->getValidationErrors()); |
185
|
|
|
} else { |
186
|
|
|
$errorsArray = $e->getFieldErrors(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
foreach ($errorsArray as $errors) { |
190
|
|
|
foreach ($errors as $error) { |
191
|
|
|
/// @todo find out what is the proper eZ way of getting a translated message for these errors |
192
|
|
|
$translatableMessage = $error->getTranslatableMessage(); |
193
|
|
|
if (is_a($e, 'eZ\Publish\API\Repository\Values\Translation\Plural')) { |
194
|
|
|
$msgText = $translatableMessage->plural; |
195
|
|
|
} else { |
196
|
|
|
$msgText = $translatableMessage->message; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$message .= "\n" . $msgText . " - " . var_export($translatableMessage->values, true); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
return $message; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.