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
|
|
|
if (!count($migrationDefinitions)) { |
69
|
|
|
$output->writeln('<info>No migrations found</info>'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// filter away all migrations except 'to do' ones |
74
|
|
|
$toExecute = array(); |
75
|
|
|
foreach($migrationDefinitions as $name => $migrationDefinition) { |
76
|
|
|
if (!isset($migrations[$name]) || (($migration = $migrations[$name]) && $migration->status == Migration::STATUS_TODO)) { |
77
|
|
|
$toExecute[$name] = $migrationsService->parseMigrationDefinition($migrationDefinition); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
// just in case... |
81
|
|
|
ksort($toExecute); |
82
|
|
|
|
83
|
|
|
if (!count($toExecute)) { |
84
|
|
|
$output->writeln('<info>No migrations to execute</info>'); |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$output->writeln("\n <info>==</info> Migrations to be executed\n"); |
89
|
|
|
|
90
|
|
|
$data = array(); |
91
|
|
|
$i = 1; |
92
|
|
|
foreach($toExecute as $name => $migrationDefinition) { |
93
|
|
|
$notes = ''; |
94
|
|
|
if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) { |
95
|
|
|
$notes = '<error>' . $migrationDefinition->parsingError . '</error>'; |
96
|
|
|
} |
97
|
|
|
$data[] = array( |
98
|
|
|
$i++, |
99
|
|
|
$name, |
100
|
|
|
$notes |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$table = $this->getHelperSet()->get('table'); |
105
|
|
|
$table |
106
|
|
|
->setHeaders(array('#', 'Migration', 'Notes')) |
107
|
|
|
->setRows($data); |
108
|
|
|
$table->render($output); |
109
|
|
|
|
110
|
|
|
$output->writeln(''); |
111
|
|
|
// ask user for confirmation to make changes |
112
|
|
View Code Duplication |
if ($input->isInteractive() && !$input->getOption('no-interaction')) { |
|
|
|
|
113
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
114
|
|
|
if (!$dialog->askConfirmation( |
115
|
|
|
$output, |
116
|
|
|
'<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', |
117
|
|
|
false |
118
|
|
|
) |
119
|
|
|
) { |
120
|
|
|
$output->writeln('<error>Migration execution cancelled!</error>'); |
121
|
|
|
return 0; |
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
$output->writeln("=============================================\n"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
foreach($toExecute as $name => $migrationDefinition) { |
128
|
|
|
|
129
|
|
|
// let's skip migrations that we know are invalid - user was warned and he decide to proceed anyway |
130
|
|
|
if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) { |
131
|
|
|
$output->writeln("<comment>Skipping $name</comment>\n"); |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$output->writeln("<info>Processing $name</info>"); |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
$migrationsService->executeMigration($migrationDefinition); |
139
|
|
|
} catch(\Exception $e) { |
140
|
|
|
if ($input->getOption('ignore-failures')) { |
141
|
|
|
$output->writeln("\n<error>Migration failed! Reason: ".$this->getFullExceptionMessage($e)."</error>\n"); |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
$output->writeln("\n<error>Migration aborted! Reason: ".$this->getFullExceptionMessage($e)."</error>"); |
145
|
|
|
return 1; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$output->writeln(''); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($input->getOption('clear-cache')) { |
152
|
|
|
$command = $this->getApplication()->find('cache:clear'); |
153
|
|
|
$inputArray = new ArrayInput(array('command' => 'cache:clear')); |
154
|
|
|
$command->run($inputArray, $output); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Turns eZPublish cryptic exceptions into something more palatable for random devs |
160
|
|
|
* @todo should this be moved to a lower layer ? |
161
|
|
|
* |
162
|
|
|
* @param \Exception $e |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function getFullExceptionMessage(\Exception $e) |
166
|
|
|
{ |
167
|
|
|
$message = $e->getMessage(); |
168
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException') || |
169
|
|
|
is_a($e, '\eZ\Publish\API\Repository\Exceptions\ContentTypeFieldValidationException') || |
170
|
|
|
is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException') |
171
|
|
|
) { |
172
|
|
|
if (is_a($e, '\eZ\Publish\API\Repository\Exceptions\LimitationValidationException')) { |
173
|
|
|
$errorsArray = array($e->getValidationErrors()); |
174
|
|
|
} else { |
175
|
|
|
$errorsArray = $e->getFieldErrors(); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
foreach ($errorsArray as $errors) { |
179
|
|
|
foreach ($errors as $error) { |
180
|
|
|
/// @todo find out what is the proper eZ way of getting a translated message for these errors |
181
|
|
|
$translatableMessage = $error->getTranslatableMessage(); |
182
|
|
|
if (is_a($e, 'eZ\Publish\API\Repository\Values\Translation\Plural')) { |
183
|
|
|
$msgText = $translatableMessage->plural; |
184
|
|
|
} else { |
185
|
|
|
$msgText = $translatableMessage->message; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$message .= "\n" . $msgText . " - " . var_export($translatableMessage->values, true); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
return $message; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.