1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Migrations; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
23
|
|
|
use Doctrine\DBAL\Migrations\Provider\LazySchemaDiffProvider; |
24
|
|
|
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProvider; |
25
|
|
|
use Doctrine\DBAL\Migrations\Provider\SchemaDiffProviderInterface; |
26
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class which wraps a migration version and allows execution of the |
30
|
|
|
* individual migration version up or down method. |
31
|
|
|
* |
32
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
33
|
|
|
* @link www.doctrine-project.org |
34
|
|
|
* @since 2.0 |
35
|
|
|
* @author Jonathan H. Wage <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class Version |
38
|
|
|
{ |
39
|
|
|
const STATE_NONE = 0; |
40
|
|
|
const STATE_PRE = 1; |
41
|
|
|
const STATE_EXEC = 2; |
42
|
|
|
const STATE_POST = 3; |
43
|
|
|
|
44
|
|
|
const DIRECTION_UP = 'up'; |
45
|
|
|
const DIRECTION_DOWN = 'down'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The Migrations Configuration instance for this migration |
49
|
|
|
* |
50
|
|
|
* @var Configuration |
51
|
|
|
*/ |
52
|
|
|
private $configuration; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The OutputWriter object instance used for outputting information |
56
|
|
|
* |
57
|
|
|
* @var OutputWriter |
58
|
|
|
*/ |
59
|
|
|
private $outputWriter; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The version in timestamp format (YYYYMMDDHHMMSS) |
63
|
|
|
* |
64
|
|
|
* @param int |
65
|
|
|
*/ |
66
|
|
|
private $version; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The migration instance for this version |
70
|
|
|
* |
71
|
|
|
* @var AbstractMigration |
72
|
|
|
*/ |
73
|
|
|
private $migration; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var \Doctrine\DBAL\Connection |
77
|
|
|
*/ |
78
|
|
|
private $connection; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $class; |
84
|
|
|
|
85
|
|
|
/** The array of collected SQL statements for this version */ |
86
|
|
|
private $sql = []; |
87
|
|
|
|
88
|
|
|
/** The array of collected parameters for SQL statements for this version */ |
89
|
|
|
private $params = []; |
90
|
|
|
|
91
|
|
|
/** The array of collected types for SQL statements for this version */ |
92
|
|
|
private $types = []; |
93
|
|
|
|
94
|
|
|
/** The time in seconds that this migration version took to execute */ |
95
|
|
|
private $time; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var int |
99
|
|
|
*/ |
100
|
|
|
private $state = self::STATE_NONE; |
101
|
|
|
|
102
|
|
|
/** @var SchemaDiffProviderInterface */ |
103
|
|
|
private $schemaProvider; |
104
|
|
|
|
105
|
74 |
|
public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider=null) |
106
|
|
|
{ |
107
|
74 |
|
$this->configuration = $configuration; |
108
|
74 |
|
$this->outputWriter = $configuration->getOutputWriter(); |
109
|
74 |
|
$this->class = $class; |
110
|
74 |
|
$this->connection = $configuration->getConnection(); |
111
|
74 |
|
$this->migration = new $class($this); |
112
|
74 |
|
$this->version = $version; |
113
|
|
|
|
114
|
74 |
|
if ($schemaProvider !== null) { |
115
|
1 |
|
$this->schemaProvider = $schemaProvider; |
116
|
1 |
|
} |
117
|
74 |
|
if($schemaProvider === null) { |
118
|
73 |
|
$schemaProvider = new SchemaDiffProvider($this->connection->getSchemaManager(), |
119
|
73 |
|
$this->connection->getDatabasePlatform()); |
120
|
73 |
|
$this->schemaProvider = new LazySchemaDiffProvider(new LazyLoadingValueHolderFactory(), $schemaProvider); |
121
|
73 |
|
} |
122
|
74 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the string version in the format YYYYMMDDHHMMSS |
126
|
|
|
* |
127
|
|
|
* @return string $version |
128
|
|
|
*/ |
129
|
40 |
|
public function getVersion() |
130
|
|
|
{ |
131
|
40 |
|
return $this->version; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the Migrations Configuration object instance |
136
|
|
|
* |
137
|
|
|
* @return Configuration $configuration |
138
|
|
|
*/ |
139
|
71 |
|
public function getConfiguration() |
140
|
|
|
{ |
141
|
71 |
|
return $this->configuration; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Check if this version has been migrated or not. |
146
|
|
|
* |
147
|
|
|
* @return boolean |
148
|
|
|
*/ |
149
|
15 |
|
public function isMigrated() |
150
|
|
|
{ |
151
|
15 |
|
return $this->configuration->hasVersionMigrated($this); |
152
|
|
|
} |
153
|
|
|
|
154
|
26 |
|
public function markMigrated() |
155
|
|
|
{ |
156
|
26 |
|
$this->markVersion('up'); |
157
|
26 |
|
} |
158
|
|
|
|
159
|
26 |
|
private function markVersion($direction) |
160
|
|
|
{ |
161
|
26 |
|
if ($direction == 'up') { |
162
|
26 |
|
$action = 'insert'; |
163
|
26 |
|
} else { |
164
|
9 |
|
$action = 'delete'; |
165
|
|
|
} |
166
|
26 |
|
$this->configuration->createMigrationTable(); |
167
|
26 |
|
$this->connection->$action( |
168
|
26 |
|
$this->configuration->getMigrationsTableName(), |
169
|
26 |
|
[$this->configuration->getMigrationsColumnName() => $this->version] |
170
|
26 |
|
); |
171
|
26 |
|
} |
172
|
|
|
|
173
|
9 |
|
public function markNotMigrated() |
174
|
|
|
{ |
175
|
9 |
|
$this->markVersion('down'); |
176
|
9 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Add some SQL queries to this versions migration |
180
|
|
|
* |
181
|
|
|
* @param array|string $sql |
182
|
|
|
* @param array $params |
183
|
|
|
* @param array $types |
184
|
|
|
*/ |
185
|
25 |
|
public function addSql($sql, array $params = [], array $types = []) |
186
|
|
|
{ |
187
|
25 |
|
if (is_array($sql)) { |
188
|
24 |
|
foreach ($sql as $key => $query) { |
189
|
13 |
|
$this->sql[] = $query; |
190
|
13 |
|
if (isset($params[$key]) && !empty($params[$key])) { |
191
|
1 |
|
$queryTypes = isset($types[$key]) ? $types[$key] : []; |
192
|
1 |
|
$this->addQueryParams($params[$key], $queryTypes); |
193
|
1 |
|
} |
194
|
24 |
|
} |
195
|
24 |
|
} else { |
196
|
17 |
|
$this->sql[] = $sql; |
197
|
17 |
|
if (!empty($params)) { |
198
|
8 |
|
$this->addQueryParams($params, $types); |
199
|
8 |
|
} |
200
|
|
|
} |
201
|
25 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed[] $params Array of prepared statement parameters |
205
|
|
|
* @param string[] $types Array of the types of each statement parameters |
206
|
|
|
*/ |
207
|
9 |
|
private function addQueryParams($params, $types) |
208
|
|
|
{ |
209
|
9 |
|
$index = count($this->sql) - 1; |
210
|
9 |
|
$this->params[$index] = $params; |
211
|
9 |
|
$this->types[$index] = $types; |
212
|
9 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Write a migration SQL file to the given path |
216
|
|
|
* |
217
|
|
|
* @param string $path The path to write the migration SQL file. |
218
|
|
|
* @param string $direction The direction to execute. |
219
|
|
|
* |
220
|
|
|
* @return boolean $written |
221
|
|
|
*/ |
222
|
8 |
|
public function writeSqlFile($path, $direction = self::DIRECTION_UP) |
223
|
|
|
{ |
224
|
8 |
|
$queries = $this->execute($direction, true); |
225
|
|
|
|
226
|
8 |
|
if ( ! empty($this->params)) { |
227
|
1 |
|
throw MigrationException::migrationNotConvertibleToSql($this->class); |
228
|
|
|
} |
229
|
|
|
|
230
|
7 |
|
$this->outputWriter->write("\n# Version " . $this->version . "\n"); |
231
|
|
|
|
232
|
7 |
|
$sqlQueries = [$this->version => $queries]; |
233
|
7 |
|
$sqlWriter = new SqlFileWriter( |
234
|
7 |
|
$this->configuration->getMigrationsColumnName(), |
235
|
7 |
|
$this->configuration->getMigrationsTableName(), |
236
|
7 |
|
$path, |
237
|
7 |
|
$this->outputWriter |
238
|
7 |
|
); |
239
|
|
|
|
240
|
7 |
|
return $sqlWriter->write($sqlQueries, $direction); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return AbstractMigration |
245
|
|
|
*/ |
246
|
2 |
|
public function getMigration() |
247
|
|
|
{ |
248
|
2 |
|
return $this->migration; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Execute this migration version up or down and and return the SQL. |
253
|
|
|
* We are only allowing the addSql call and the schema modification to take effect in the up and down call. |
254
|
|
|
* This is necessary to ensure that the migration is revertable. |
255
|
|
|
* The schema is passed to the pre and post method only to be able to test the presence of some table, And the |
256
|
|
|
* connection that can get used trough it allow for the test of the presence of records. |
257
|
|
|
* |
258
|
|
|
* @param string $direction The direction to execute the migration. |
259
|
|
|
* @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. |
260
|
|
|
* @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
261
|
|
|
* |
262
|
|
|
* @return array $sql |
263
|
|
|
* |
264
|
|
|
* @throws \Exception when migration fails |
265
|
|
|
*/ |
266
|
25 |
|
public function execute($direction, $dryRun = false, $timeAllQueries = false) |
267
|
|
|
{ |
268
|
25 |
|
$this->sql = []; |
269
|
|
|
|
270
|
25 |
|
$transaction = $this->migration->isTransactional(); |
271
|
25 |
|
if ($transaction) { |
272
|
|
|
//only start transaction if in transactional mode |
273
|
25 |
|
$this->connection->beginTransaction(); |
274
|
25 |
|
} |
275
|
|
|
|
276
|
|
|
try { |
277
|
25 |
|
$migrationStart = microtime(true); |
278
|
|
|
|
279
|
25 |
|
$this->state = self::STATE_PRE; |
280
|
25 |
|
$fromSchema = $this->schemaProvider->createFromSchema(); |
281
|
|
|
|
282
|
25 |
|
$this->migration->{'pre' . ucfirst($direction)}($fromSchema); |
283
|
|
|
|
284
|
24 |
|
if ($direction === self::DIRECTION_UP) { |
285
|
22 |
|
$this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n"); |
286
|
22 |
|
} else { |
287
|
8 |
|
$this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n"); |
288
|
|
|
} |
289
|
|
|
|
290
|
24 |
|
$this->state = self::STATE_EXEC; |
291
|
|
|
|
292
|
24 |
|
$toSchema = $this->schemaProvider->createToSchema($fromSchema); |
293
|
24 |
|
$this->migration->$direction($toSchema); |
294
|
|
|
|
295
|
23 |
|
$this->addSql($this->schemaProvider->getSqlDiffToMigrate($fromSchema, $toSchema)); |
296
|
|
|
|
297
|
23 |
|
$this->executeRegisteredSql($dryRun, $timeAllQueries); |
298
|
|
|
|
299
|
23 |
|
$this->state = self::STATE_POST; |
300
|
23 |
|
$this->migration->{'post' . ucfirst($direction)}($toSchema); |
301
|
|
|
|
302
|
23 |
|
if (! $dryRun) { |
303
|
17 |
|
if ($direction === self::DIRECTION_UP) { |
304
|
17 |
|
$this->markMigrated(); |
305
|
17 |
|
} else { |
306
|
6 |
|
$this->markNotMigrated(); |
307
|
|
|
} |
308
|
17 |
|
} |
309
|
|
|
|
310
|
23 |
|
$migrationEnd = microtime(true); |
311
|
23 |
|
$this->time = round($migrationEnd - $migrationStart, 2); |
312
|
23 |
|
if ($direction === self::DIRECTION_UP) { |
313
|
21 |
|
$this->outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->time)); |
314
|
21 |
|
} else { |
315
|
8 |
|
$this->outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->time)); |
316
|
|
|
} |
317
|
|
|
|
318
|
23 |
|
if ($transaction) { |
319
|
|
|
//commit only if running in transactional mode |
320
|
23 |
|
$this->connection->commit(); |
321
|
23 |
|
} |
322
|
|
|
|
323
|
23 |
|
$this->state = self::STATE_NONE; |
324
|
|
|
|
325
|
23 |
|
return $this->sql; |
326
|
6 |
|
} catch (SkipMigrationException $e) { |
327
|
5 |
|
if ($transaction) { |
328
|
|
|
//only rollback transaction if in transactional mode |
329
|
5 |
|
$this->connection->rollback(); |
330
|
5 |
|
} |
331
|
|
|
|
332
|
5 |
|
if ($dryRun === false) { |
333
|
|
|
// now mark it as migrated |
334
|
4 |
|
if ($direction === self::DIRECTION_UP) { |
335
|
4 |
|
$this->markMigrated(); |
336
|
4 |
|
} else { |
337
|
1 |
|
$this->markNotMigrated(); |
338
|
|
|
} |
339
|
4 |
|
} |
340
|
|
|
|
341
|
5 |
|
$this->outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage())); |
342
|
|
|
|
343
|
5 |
|
$this->state = self::STATE_NONE; |
344
|
|
|
|
345
|
5 |
|
return []; |
346
|
1 |
|
} catch (\Exception $e) { |
347
|
|
|
|
348
|
1 |
|
$this->outputWriter->write(sprintf( |
349
|
1 |
|
'<error>Migration %s failed during %s. Error %s</error>', |
350
|
1 |
|
$this->version, $this->getExecutionState(), $e->getMessage() |
351
|
1 |
|
)); |
352
|
|
|
|
353
|
1 |
|
if ($transaction) { |
354
|
|
|
//only rollback transaction if in transactional mode |
355
|
1 |
|
$this->connection->rollback(); |
356
|
1 |
|
} |
357
|
|
|
|
358
|
1 |
|
$this->state = self::STATE_NONE; |
359
|
1 |
|
throw $e; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
8 |
|
public function getExecutionState() |
364
|
|
|
{ |
365
|
8 |
|
switch ($this->state) { |
366
|
8 |
|
case self::STATE_PRE: |
367
|
1 |
|
return 'Pre-Checks'; |
368
|
7 |
|
case self::STATE_POST: |
369
|
1 |
|
return 'Post-Checks'; |
370
|
6 |
|
case self::STATE_EXEC: |
371
|
2 |
|
return 'Execution'; |
372
|
4 |
|
default: |
373
|
4 |
|
return 'No State'; |
374
|
4 |
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
18 |
|
private function outputQueryTime($queryStart, $timeAllQueries = false) |
378
|
|
|
{ |
379
|
18 |
|
if ($timeAllQueries !== false) { |
380
|
1 |
|
$queryEnd = microtime(true); |
381
|
1 |
|
$queryTime = round($queryEnd - $queryStart, 4); |
382
|
|
|
|
383
|
1 |
|
$this->outputWriter->write(sprintf(" <info>%ss</info>", $queryTime)); |
384
|
1 |
|
} |
385
|
18 |
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Returns the time this migration version took to execute |
389
|
|
|
* |
390
|
|
|
* @return integer $time The time this migration version took to execute |
391
|
|
|
*/ |
392
|
12 |
|
public function getTime() |
393
|
|
|
{ |
394
|
12 |
|
return $this->time; |
395
|
|
|
} |
396
|
|
|
|
397
|
2 |
|
public function __toString() |
398
|
|
|
{ |
399
|
2 |
|
return $this->version; |
400
|
|
|
} |
401
|
|
|
|
402
|
23 |
|
private function executeRegisteredSql($dryRun = false, $timeAllQueries = false) |
403
|
|
|
{ |
404
|
23 |
|
if (! $dryRun) { |
405
|
17 |
|
if (!empty($this->sql)) { |
406
|
16 |
|
foreach ($this->sql as $key => $query) { |
407
|
16 |
|
$queryStart = microtime(true); |
408
|
|
|
|
409
|
16 |
|
if ( ! isset($this->params[$key])) { |
410
|
15 |
|
$this->outputWriter->write(' <comment>-></comment> ' . $query); |
411
|
15 |
|
$this->connection->executeQuery($query); |
412
|
15 |
|
} else { |
413
|
7 |
|
$this->outputWriter->write(sprintf(' <comment>-</comment> %s (with parameters)', $query)); |
414
|
7 |
|
$this->connection->executeQuery($query, $this->params[$key], $this->types[$key]); |
415
|
|
|
} |
416
|
|
|
|
417
|
16 |
|
$this->outputQueryTime($queryStart, $timeAllQueries); |
418
|
16 |
|
} |
419
|
16 |
|
} else { |
420
|
1 |
|
$this->outputWriter->write(sprintf( |
421
|
1 |
|
'<error>Migration %s was executed but did not result in any SQL statements.</error>', |
422
|
1 |
|
$this->version |
423
|
1 |
|
)); |
424
|
|
|
} |
425
|
17 |
|
} else { |
426
|
6 |
|
foreach ($this->sql as $query) { |
427
|
6 |
|
$this->outputWriter->write(' <comment>-></comment> ' . $query); |
428
|
6 |
|
} |
429
|
|
|
} |
430
|
23 |
|
} |
431
|
|
|
} |
432
|
|
|
|