|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Version; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
10
|
|
|
use Doctrine\Migrations\AbstractMigration; |
|
11
|
|
|
use Doctrine\Migrations\EventDispatcher; |
|
12
|
|
|
use Doctrine\Migrations\Events; |
|
13
|
|
|
use Doctrine\Migrations\Exception\SkipMigration; |
|
14
|
|
|
use Doctrine\Migrations\Metadata\MigrationPlan; |
|
15
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
|
16
|
|
|
use Doctrine\Migrations\MigratorConfiguration; |
|
17
|
|
|
use Doctrine\Migrations\ParameterFormatter; |
|
18
|
|
|
use Doctrine\Migrations\Provider\SchemaDiffProvider; |
|
19
|
|
|
use Doctrine\Migrations\Stopwatch; |
|
20
|
|
|
use Doctrine\Migrations\Tools\BytesFormatter; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
use Throwable; |
|
23
|
|
|
use function count; |
|
24
|
|
|
use function ucfirst; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The DbalExecutor class is responsible for executing a single migration version. |
|
28
|
|
|
* |
|
29
|
|
|
* @internal |
|
30
|
|
|
*/ |
|
31
|
|
|
final class DbalExecutor implements Executor |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var Connection */ |
|
34
|
|
|
private $connection; |
|
35
|
|
|
|
|
36
|
|
|
/** @var SchemaDiffProvider */ |
|
37
|
|
|
private $schemaProvider; |
|
38
|
|
|
|
|
39
|
|
|
/** @var ParameterFormatter */ |
|
40
|
|
|
private $parameterFormatter; |
|
41
|
|
|
|
|
42
|
|
|
/** @var Stopwatch */ |
|
43
|
|
|
private $stopwatch; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array<int, string> */ |
|
46
|
|
|
private $sql = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var mixed[] */ |
|
49
|
|
|
private $params = []; |
|
50
|
|
|
|
|
51
|
|
|
/** @var mixed[] */ |
|
52
|
|
|
private $types = []; |
|
53
|
|
|
|
|
54
|
|
|
/** @var MetadataStorage */ |
|
55
|
|
|
private $metadataStorage; |
|
56
|
|
|
|
|
57
|
|
|
/** @var LoggerInterface */ |
|
58
|
|
|
private $logger; |
|
59
|
|
|
|
|
60
|
|
|
/** @var EventDispatcher */ |
|
61
|
|
|
private $dispatcher; |
|
62
|
|
|
|
|
63
|
12 |
|
public function __construct( |
|
64
|
|
|
MetadataStorage $metadataStorage, |
|
65
|
|
|
EventDispatcher $dispatcher, |
|
66
|
|
|
Connection $connection, |
|
67
|
|
|
SchemaDiffProvider $schemaProvider, |
|
68
|
|
|
LoggerInterface $logger, |
|
69
|
|
|
ParameterFormatter $parameterFormatter, |
|
70
|
|
|
Stopwatch $stopwatch |
|
71
|
|
|
) { |
|
72
|
12 |
|
$this->connection = $connection; |
|
73
|
12 |
|
$this->schemaProvider = $schemaProvider; |
|
74
|
12 |
|
$this->parameterFormatter = $parameterFormatter; |
|
75
|
12 |
|
$this->stopwatch = $stopwatch; |
|
76
|
12 |
|
$this->metadataStorage = $metadataStorage; |
|
77
|
12 |
|
$this->logger = $logger; |
|
78
|
12 |
|
$this->dispatcher = $dispatcher; |
|
79
|
12 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string[] |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getSql() : array |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->sql; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return mixed[] |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getParams() : array |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->params; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return mixed[] |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function getTypes() : array |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->types; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param mixed[] $params |
|
107
|
|
|
* @param mixed[] $types |
|
108
|
|
|
*/ |
|
109
|
8 |
|
public function addSql(string $sql, array $params = [], array $types = []) : void |
|
110
|
|
|
{ |
|
111
|
8 |
|
$this->sql[] = $sql; |
|
112
|
|
|
|
|
113
|
8 |
|
if (count($params) === 0) { |
|
114
|
5 |
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
6 |
|
$this->addQueryParams($params, $types); |
|
118
|
6 |
|
} |
|
119
|
|
|
|
|
120
|
10 |
|
public function execute( |
|
121
|
|
|
MigrationPlan $plan, |
|
122
|
|
|
MigratorConfiguration $configuration |
|
123
|
|
|
) : ExecutionResult { |
|
124
|
10 |
|
$result = new ExecutionResult($plan->getVersion(), $plan->getDirection(), new DateTimeImmutable()); |
|
125
|
|
|
|
|
126
|
10 |
|
$this->startMigration($plan, $configuration); |
|
127
|
|
|
|
|
128
|
|
|
try { |
|
129
|
10 |
|
$this->executeMigration( |
|
130
|
10 |
|
$plan, |
|
131
|
10 |
|
$result, |
|
132
|
10 |
|
$configuration |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
7 |
|
$result->setSql($this->sql, $this->params, $this->types); |
|
136
|
3 |
|
} catch (SkipMigration $e) { |
|
137
|
1 |
|
$result->setSkipped(true); |
|
138
|
|
|
|
|
139
|
1 |
|
$this->migrationEnd($e, $plan, $result, $configuration); |
|
140
|
2 |
|
} catch (Throwable $e) { |
|
141
|
2 |
|
$result->setError(true, $e); |
|
142
|
|
|
|
|
143
|
2 |
|
$this->migrationEnd($e, $plan, $result, $configuration); |
|
144
|
|
|
|
|
145
|
2 |
|
throw $e; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
8 |
|
return $result; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
10 |
|
private function startMigration( |
|
152
|
|
|
MigrationPlan $plan, |
|
153
|
|
|
MigratorConfiguration $configuration |
|
154
|
|
|
) : void { |
|
155
|
10 |
|
$this->sql = []; |
|
156
|
10 |
|
$this->params = []; |
|
157
|
10 |
|
$this->types = []; |
|
158
|
|
|
|
|
159
|
10 |
|
$this->dispatcher->dispatchVersionEvent( |
|
160
|
10 |
|
Events::onMigrationsVersionExecuting, |
|
161
|
10 |
|
$plan, |
|
162
|
10 |
|
$configuration |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
10 |
|
if (! $plan->getMigration()->isTransactional()) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// only start transaction if in transactional mode |
|
170
|
10 |
|
$this->connection->beginTransaction(); |
|
171
|
10 |
|
} |
|
172
|
|
|
|
|
173
|
10 |
|
private function executeMigration( |
|
174
|
|
|
MigrationPlan $plan, |
|
175
|
|
|
ExecutionResult $result, |
|
176
|
|
|
MigratorConfiguration $configuration |
|
177
|
|
|
) : ExecutionResult { |
|
178
|
10 |
|
$stopwatchEvent = $this->stopwatch->start('execute'); |
|
179
|
|
|
|
|
180
|
10 |
|
$migration = $plan->getMigration(); |
|
181
|
10 |
|
$direction = $plan->getDirection(); |
|
182
|
|
|
|
|
183
|
10 |
|
$result->setState(State::PRE); |
|
184
|
|
|
|
|
185
|
10 |
|
$fromSchema = $this->getFromSchema($configuration); |
|
186
|
|
|
|
|
187
|
10 |
|
$migration->{'pre' . ucfirst($direction)}($fromSchema); |
|
188
|
|
|
|
|
189
|
10 |
|
$this->logger->info(...$this->getMigrationHeader($plan, $migration, $direction)); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
10 |
|
$result->setState(State::EXEC); |
|
192
|
|
|
|
|
193
|
10 |
|
$toSchema = $this->schemaProvider->createToSchema($fromSchema); |
|
194
|
|
|
|
|
195
|
10 |
|
$result->setToSchema($toSchema); |
|
196
|
|
|
|
|
197
|
10 |
|
$migration->$direction($toSchema); |
|
198
|
|
|
|
|
199
|
7 |
|
foreach ($migration->getSql() as $sqlData) { |
|
200
|
7 |
|
$this->addSql(...$sqlData); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
7 |
|
foreach ($this->schemaProvider->getSqlDiffToMigrate($fromSchema, $toSchema) as $sql) { |
|
204
|
|
|
$this->addSql($sql); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
7 |
|
if (count($this->sql) !== 0) { |
|
208
|
7 |
|
if (! $configuration->isDryRun()) { |
|
209
|
7 |
|
$this->executeResult($configuration); |
|
210
|
|
|
} else { |
|
211
|
7 |
|
foreach ($this->sql as $idx => $query) { |
|
212
|
|
|
$this->outputSqlQuery($idx, $query); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} else { |
|
216
|
|
|
$this->logger->warning('Migration {version} was executed but did not result in any SQL statements.', [ |
|
217
|
|
|
'version' => (string) $plan->getVersion(), |
|
218
|
|
|
]); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
7 |
|
$result->setState(State::POST); |
|
222
|
|
|
|
|
223
|
7 |
|
$migration->{'post' . ucfirst($direction)}($toSchema); |
|
224
|
7 |
|
$stopwatchEvent->stop(); |
|
225
|
|
|
|
|
226
|
7 |
|
$result->setTime($stopwatchEvent->getDuration()); |
|
227
|
7 |
|
$result->setMemory($stopwatchEvent->getMemory()); |
|
228
|
7 |
|
$plan->markAsExecuted($result); |
|
229
|
|
|
|
|
230
|
7 |
|
if (! $configuration->isDryRun()) { |
|
231
|
7 |
|
$this->metadataStorage->complete($result); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$params = [ |
|
235
|
7 |
|
'version' => (string) $plan->getVersion(), |
|
236
|
7 |
|
'time' => $stopwatchEvent->getDuration(), |
|
237
|
7 |
|
'memory' => BytesFormatter::formatBytes($stopwatchEvent->getMemory()), |
|
238
|
7 |
|
'direction' => $direction === Direction::UP ? 'migrated' : 'reverted', |
|
239
|
|
|
]; |
|
240
|
|
|
|
|
241
|
7 |
|
$this->logger->info('Migration {version} {direction} (took {time}ms, used {memory} memory)', $params); |
|
242
|
|
|
|
|
243
|
7 |
|
if ($migration->isTransactional()) { |
|
244
|
|
|
//commit only if running in transactional mode |
|
245
|
7 |
|
$this->connection->commit(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
7 |
|
$result->setState(State::NONE); |
|
249
|
|
|
|
|
250
|
7 |
|
$this->dispatcher->dispatchVersionEvent( |
|
251
|
7 |
|
Events::onMigrationsVersionExecuted, |
|
252
|
7 |
|
$plan, |
|
253
|
7 |
|
$configuration |
|
254
|
|
|
); |
|
255
|
|
|
|
|
256
|
7 |
|
return $result; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return mixed[] |
|
261
|
|
|
*/ |
|
262
|
10 |
|
private function getMigrationHeader(MigrationPlan $planItem, AbstractMigration $migration, string $direction) : array |
|
263
|
|
|
{ |
|
264
|
10 |
|
$versionInfo = (string) $planItem->getVersion(); |
|
265
|
10 |
|
$description = $migration->getDescription(); |
|
266
|
|
|
|
|
267
|
10 |
|
if ($description !== '') { |
|
268
|
1 |
|
$versionInfo .= ' (' . $description . ')'; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
10 |
|
$params = ['version_name' => $versionInfo]; |
|
272
|
|
|
|
|
273
|
10 |
|
if ($direction === Direction::UP) { |
|
274
|
8 |
|
return ['++ migrating {version_name}', $params]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
2 |
|
return ['++ reverting {version_name}', $params]; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
3 |
|
private function migrationEnd(Throwable $e, MigrationPlan $plan, ExecutionResult $result, MigratorConfiguration $configuration) : void |
|
281
|
|
|
{ |
|
282
|
3 |
|
$plan->markAsExecuted($result); |
|
283
|
3 |
|
$this->logResult($e, $result, $plan); |
|
284
|
|
|
|
|
285
|
3 |
|
$this->dispatcher->dispatchVersionEvent( |
|
286
|
3 |
|
Events::onMigrationsVersionSkipped, |
|
287
|
3 |
|
$plan, |
|
288
|
3 |
|
$configuration |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
3 |
|
$migration = $plan->getMigration(); |
|
292
|
|
|
|
|
293
|
3 |
|
if ($migration->isTransactional()) { |
|
294
|
|
|
//only rollback transaction if in transactional mode |
|
295
|
3 |
|
$this->connection->rollBack(); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
3 |
|
if ($configuration->isDryRun() || $result->isSkipped() || $result->hasError()) { |
|
299
|
3 |
|
return; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
$this->metadataStorage->complete($result); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
3 |
|
private function logResult(Throwable $e, ExecutionResult $result, MigrationPlan $plan) : void |
|
306
|
|
|
{ |
|
307
|
3 |
|
if ($result->isSkipped()) { |
|
308
|
1 |
|
$this->logger->error( |
|
309
|
1 |
|
'Migration {version} skipped during {state}. Reason: "{reason}"', |
|
310
|
|
|
[ |
|
311
|
1 |
|
'version' => (string) $plan->getVersion(), |
|
312
|
1 |
|
'reason' => $e->getMessage(), |
|
313
|
1 |
|
'state' => $this->getExecutionStateAsString($result->getState()), |
|
314
|
|
|
] |
|
315
|
|
|
); |
|
316
|
2 |
|
} elseif ($result->hasError()) { |
|
317
|
2 |
|
$this->logger->error( |
|
318
|
2 |
|
'Migration {version} failed during {state}. Error: "{error}"', |
|
319
|
|
|
[ |
|
320
|
2 |
|
'version' => (string) $plan->getVersion(), |
|
321
|
2 |
|
'error' => $e->getMessage(), |
|
322
|
2 |
|
'state' => $this->getExecutionStateAsString($result->getState()), |
|
323
|
|
|
] |
|
324
|
|
|
); |
|
325
|
|
|
} |
|
326
|
3 |
|
} |
|
327
|
|
|
|
|
328
|
7 |
|
private function executeResult(MigratorConfiguration $configuration) : void |
|
329
|
|
|
{ |
|
330
|
7 |
|
foreach ($this->sql as $key => $query) { |
|
331
|
7 |
|
$stopwatchEvent = $this->stopwatch->start('query'); |
|
332
|
|
|
|
|
333
|
7 |
|
$this->outputSqlQuery($key, $query); |
|
334
|
|
|
|
|
335
|
7 |
|
if (! isset($this->params[$key])) { |
|
336
|
5 |
|
$this->connection->executeQuery($query); |
|
337
|
|
|
} else { |
|
338
|
5 |
|
$this->connection->executeQuery($query, $this->params[$key], $this->types[$key]); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
7 |
|
$stopwatchEvent->stop(); |
|
342
|
|
|
|
|
343
|
7 |
|
if (! $configuration->getTimeAllQueries()) { |
|
344
|
2 |
|
continue; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
5 |
|
$this->logger->debug('{duration}ms', [ |
|
348
|
5 |
|
'duration' => $stopwatchEvent->getDuration(), |
|
349
|
|
|
]); |
|
350
|
|
|
} |
|
351
|
7 |
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param mixed[]|int $params |
|
355
|
|
|
* @param mixed[]|int $types |
|
356
|
|
|
*/ |
|
357
|
6 |
|
private function addQueryParams($params, $types) : void |
|
358
|
|
|
{ |
|
359
|
6 |
|
$index = count($this->sql) - 1; |
|
360
|
6 |
|
$this->params[$index] = $params; |
|
361
|
6 |
|
$this->types[$index] = $types; |
|
362
|
6 |
|
} |
|
363
|
|
|
|
|
364
|
7 |
|
private function outputSqlQuery(int $idx, string $query) : void |
|
365
|
|
|
{ |
|
366
|
7 |
|
$params = $this->parameterFormatter->formatParameters( |
|
367
|
7 |
|
$this->params[$idx] ?? [], |
|
368
|
7 |
|
$this->types[$idx] ?? [] |
|
369
|
|
|
); |
|
370
|
|
|
|
|
371
|
7 |
|
$this->logger->debug('{query} {params}', [ |
|
372
|
7 |
|
'query' => $query, |
|
373
|
7 |
|
'params' => $params, |
|
374
|
|
|
]); |
|
375
|
7 |
|
} |
|
376
|
|
|
|
|
377
|
10 |
|
private function getFromSchema(MigratorConfiguration $configuration) : Schema |
|
378
|
|
|
{ |
|
379
|
|
|
// if we're in a dry run, use the from Schema instead of reading the schema from the database |
|
380
|
10 |
|
if ($configuration->isDryRun() && $configuration->getFromSchema() !== null) { |
|
381
|
|
|
return $configuration->getFromSchema(); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
10 |
|
return $this->schemaProvider->createFromSchema(); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
3 |
|
private function getExecutionStateAsString(int $state) : string |
|
388
|
|
|
{ |
|
389
|
|
|
switch ($state) { |
|
390
|
3 |
|
case State::PRE: |
|
391
|
|
|
return 'Pre-Checks'; |
|
392
|
3 |
|
case State::POST: |
|
393
|
|
|
return 'Post-Checks'; |
|
394
|
3 |
|
case State::EXEC: |
|
395
|
3 |
|
return 'Execution'; |
|
396
|
|
|
default: |
|
397
|
|
|
return 'No State'; |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|