/
Updater.php
480 lines (449 loc) · 18.1 KB
/
Updater.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
declare(strict_types=1);
namespace bizley\migration;
use bizley\migration\table\TableChange;
use bizley\migration\table\TableColumn;
use bizley\migration\table\TablePlan;
use bizley\migration\table\TablePrimaryKey;
use bizley\migration\table\TableStructure;
use Yii;
use yii\base\ErrorException;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\console\controllers\MigrateController;
use yii\db\Query;
use yii\helpers\ArrayHelper;
/**
* Class Updater
* @package bizley\migration
*
* @property-read TableStructure $oldTable
* @property TablePlan $plan
*/
class Updater extends Generator
{
/**
* @var string Name of the table for keeping applied migration information.
*/
public $migrationTable = '{{%migration}}';
/**
* @var string Directory storing the migration classes. This can be either a path alias or a directory.
*/
public $migrationPath = '@app/migrations';
/**
* @var bool Whether to only display changes instead of create updating migration.
*/
public $showOnly = false;
/**
* @var array List of migration from the history that should be skipped during the update process.
* Here you can place migrations containing actions that can not be covered by extractor.
* @since 2.1.1
*/
public $skipMigrations = [];
private $_currentTable;
/**
* Sets current table name and clears skipped migrations names.
* @throws InvalidConfigException
*/
public function init(): void
{
parent::init();
$this->_currentTable = $this->tableName;
foreach ($this->skipMigrations as $index => $migration) {
$this->skipMigrations[$index] = trim($migration, '\\');
}
}
/**
* Sets dummy Migration class.
* @throws InvalidArgumentException
*/
protected function setDummyMigrationClass(): void
{
Yii::$classMap['yii\db\Migration'] = Yii::getAlias('@bizley/migration/dummy/Migration.php');
}
/**
* Returns the migration history.
* This is slightly modified MigrateController::getMigrationHistory() method.
* Migrations are fetched from newest to oldest.
* @return array the migration history
*/
public function fetchHistory(): array
{
if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) {
return [];
}
$rows = (new Query())
->select(['version', 'apply_time'])
->from($this->migrationTable)
->orderBy(['apply_time' => SORT_DESC, 'version' => SORT_DESC])
->all($this->db);
$history = [];
foreach ($rows as $key => $row) {
if ($row['version'] === MigrateController::BASE_MIGRATION) {
continue;
}
if (preg_match('/m?(\d{6}_?\d{6})(\D.*)?$/is', $row['version'], $matches)) {
$time = str_replace('_', '', $matches[1]);
$row['canonicalVersion'] = $time;
} else {
$row['canonicalVersion'] = $row['version'];
}
$row['apply_time'] = (int)$row['apply_time'];
$history[] = $row;
}
usort($history, function ($a, $b) {
if ($a['apply_time'] === $b['apply_time']) {
if (($compareResult = strcasecmp($b['canonicalVersion'], $a['canonicalVersion'])) !== 0) {
return $compareResult;
}
return strcasecmp($b['version'], $a['version']);
}
return ($a['apply_time'] > $b['apply_time']) ? -1 : 1;
});
return ArrayHelper::map($history, 'version', 'apply_time');
}
private $_appliedChanges = [];
/**
* Gathers applied changes.
* @param array $changes
* @return bool true if more data can be analysed or false if this must be last one
* @since 2.3.0
*/
protected function gatherChanges($changes): bool
{
if (!isset($changes[$this->_currentTable])) {
return true;
}
$data = array_reverse($changes[$this->_currentTable]);
/* @var $tableChange TableChange */
foreach ($data as $tableChange) {
if ($tableChange->method === 'dropTable') {
return false;
}
if ($tableChange->method === 'renameTable') {
$this->_currentTable = $tableChange->value;
return $this->gatherChanges($changes);
}
$this->_appliedChanges[] = $tableChange;
if ($tableChange->method === 'createTable') {
return false;
}
}
return true;
}
/**
* Extracts migration data structures.
* @param string $migration
* @return array
* @throws InvalidArgumentException
* @throws ErrorException
*/
protected function extract($migration): array
{
if (strpos($migration, '\\') === false) {
$file = Yii::getAlias($this->migrationPath . DIRECTORY_SEPARATOR . $migration . '.php');
if (!file_exists($file)) {
throw new ErrorException("File '{$file}' can not be found! Check migration history table.");
}
require_once $file;
}
$subject = new $migration;
$subject->db = $this->db;
$subject->up();
return $subject->changes;
}
protected $_oldTable;
/**
* Returns the table structure as applied in gathered migrations.
* @return TableStructure
* @since 2.3.0
* @throws \yii\base\InvalidParamException
*/
public function getOldTable(): TableStructure
{
if ($this->_oldTable === null) {
$this->_oldTable = new TableStructure([
'schema' => \get_class($this->db->schema),
'generalSchema' => $this->generalSchema,
'usePrefix' => $this->useTablePrefix,
'dbPrefix' => $this->db->tablePrefix,
]);
$this->_oldTable->applyChanges(array_reverse($this->_appliedChanges));
}
return $this->_oldTable;
}
/**
* Returns values as a string.
* @param mixed $value
* @return string
*/
public function displayValue($value): string
{
if ($value === null) {
return 'NULL';
}
if ($value === true) {
return 'TRUE';
}
if ($value === false) {
return 'FALSE';
}
return '"' . str_replace('"', '\"', $value) . '"';
}
/**
* Confirms adding composite primary key and removes excessive PK statements.
* @param array $newKeys
* @return bool
* @since 2.1.2
*/
protected function confirmCompositePrimaryKey($newKeys): bool
{
if (\count($this->table->primaryKey->columns) === 1 && \count($newKeys) === 1) {
/* @var $column TableColumn */
foreach ($this->plan->addColumn as $name => $column) {
if ($name === $newKeys[0] && ($column->isPrimaryKey || $column->isColumnAppendPK($this->table->schema))) {
return false;
}
}
foreach ($this->plan->alterColumn as $name => $column) {
if ($name === $newKeys[0] && ($column->isPrimaryKey || $column->isColumnAppendPK($this->table->schema))) {
return false;
}
}
return true;
}
if (\count($this->table->primaryKey->columns) > 1) {
foreach ($newKeys as $key) {
/* @var $column TableColumn */
foreach ($this->plan->addColumn as $name => $column) {
if ($name === $key) {
$column->append = $column->removePKAppend($this->table->schema);
}
}
foreach ($this->plan->alterColumn as $name => $column) {
if ($name === $key) {
$column->append = $column->removePKAppend($this->table->schema);
}
}
}
}
return true;
}
private $_modifications;
/**
* @return TablePlan
*/
public function getPlan(): TablePlan
{
if ($this->_modifications === null) {
$this->_modifications = new TablePlan();
}
return $this->_modifications;
}
/**
* Compares migration structure and database structure and gather required modifications.
* @return bool whether modification is required or not
*/
protected function compareStructures(): bool
{
if (empty($this->_appliedChanges)) {
return true;
}
$different = false;
if ($this->showOnly) {
echo "SHOWING DIFFERENCES:\n";
}
foreach ($this->table->columns as $name => $column) {
if (!isset($this->oldTable->columns[$name])) {
if ($this->showOnly) {
echo " - missing column '$name'\n";
} else {
$this->plan->addColumn[$name] = $column;
}
$different = true;
continue;
}
if (!$this->generalSchema) {
foreach (TableColumn::properties() as $property) {
if ($property === 'append' && $column->append === null && !$this->table->primaryKey->isComposite() && $column->isColumnInPK($this->table->primaryKey)) {
$column->append = $column->prepareSchemaAppend($this->table, true, $column->autoIncrement);
}
if ($this->oldTable->columns[$name]->$property !== $column->$property) {
if ($this->showOnly) {
echo " - different '$name' column property: $property (";
echo 'DB: ' . $this->displayValue($column->$property) . ' <> ';
echo 'MIG: ' . $this->displayValue($this->oldTable->columns[$name]->$property) . ")\n";
} elseif (!isset($this->plan->alterColumn[$name])) {
$this->plan->alterColumn[$name] = $column;
}
$different = true;
}
}
}
}
foreach ($this->oldTable->columns as $name => $column) {
if (!isset($this->table->columns[$name])) {
if ($this->showOnly) {
echo " - excessive column '$name'\n";
} else {
$this->plan->dropColumn[] = $name;
}
$different = true;
}
}
foreach ($this->table->foreignKeys as $name => $foreignKey) {
if (!isset($this->oldTable->foreignKeys[$name])) {
if ($this->showOnly) {
echo " - missing foreign key '$name'\n";
} else {
$this->plan->addForeignKey[$name] = $foreignKey;
}
$different = true;
continue;
}
$tableFKColumns = !empty($this->table->foreignKeys[$name]->columns) ? $this->table->foreignKeys[$name]->columns : [];
$oldTableFKColumns = !empty($this->oldTable->foreignKeys[$name]->columns) ? $this->oldTable->foreignKeys[$name]->columns : [];
if (\count(array_merge(array_diff($tableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns)),
array_diff($oldTableFKColumns, array_intersect($tableFKColumns, $oldTableFKColumns))))) {
if ($this->showOnly) {
echo " - different foreign key '$name' columns (";
echo 'DB: (' . implode(', ', $tableFKColumns) . ') <> ';
echo 'MIG: (' . implode(', ', $oldTableFKColumns) . "))\n";
} else {
$this->plan->dropForeignKey[] = $name;
$this->plan->addForeignKey[$name] = $foreignKey;
}
$different = true;
continue;
}
$tableFKRefColumns = !empty($this->table->foreignKeys[$name]->refColumns) ? $this->table->foreignKeys[$name]->refColumns : [];
$oldTableFKRefColumns = !empty($this->oldTable->foreignKeys[$name]->refColumns) ? $this->oldTable->foreignKeys[$name]->refColumns : [];
if (\count(array_merge(array_diff($tableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns)),
array_diff($oldTableFKRefColumns, array_intersect($tableFKRefColumns, $oldTableFKRefColumns))))) {
if ($this->showOnly) {
echo " - different foreign key '$name' referral columns (";
echo 'DB: (' . implode(', ', $tableFKRefColumns) . ') <> ';
echo 'MIG: (' . implode(', ', $oldTableFKRefColumns) . "))\n";
} else {
$this->plan->dropForeignKey[] = $name;
$this->plan->addForeignKey[$name] = $foreignKey;
}
$different = true;
}
}
foreach ($this->oldTable->foreignKeys as $name => $foreignKey) {
if (!isset($this->table->foreignKeys[$name])) {
if ($this->showOnly) {
echo " - excessive foreign key '$name'\n";
} else {
$this->plan->dropForeignKey[] = $name;
}
$different = true;
}
}
$tablePKColumns = !empty($this->table->primaryKey->columns) ? $this->table->primaryKey->columns : [];
$oldTablePKColumns = !empty($this->oldTable->primaryKey->columns) ? $this->oldTable->primaryKey->columns : [];
$newKeys = array_merge(array_diff($tablePKColumns, array_intersect($tablePKColumns, $oldTablePKColumns)),
array_diff($oldTablePKColumns, array_intersect($tablePKColumns, $oldTablePKColumns)));
if (\count($newKeys)) {
if ($this->showOnly) {
echo " - different primary key definition\n";
} else {
if (!empty($this->oldTable->primaryKey->columns)) {
$this->plan->dropPrimaryKey = $this->oldTable->primaryKey->name ?: TablePrimaryKey::GENERIC_PRIMARY_KEY;
}
if (!empty($this->table->primaryKey->columns) && $this->confirmCompositePrimaryKey($newKeys)) {
$this->plan->addPrimaryKey = $this->table->primaryKey;
}
}
$different = true;
}
foreach ($this->table->indexes as $name => $index) {
if (!isset($this->oldTable->indexes[$name])) {
if ($this->showOnly) {
echo " - missing index '$name'\n";
} else {
$this->plan->createIndex[$name] = $index;
}
$different = true;
continue;
}
if ($this->oldTable->indexes[$name]->unique !== $this->table->indexes[$name]->unique) {
if ($this->showOnly) {
echo " - different index '$name' definition (";
echo 'DB: unique ' . $this->displayValue($this->table->indexes[$name]->unique) . ' <> ';
echo 'MIG: unique ' . $this->displayValue($this->oldTable->indexes[$name]->unique) . ")\n";
} else {
$this->plan->dropIndex[] = $name;
$this->plan->createIndex[$name] = $index;
}
$different = true;
continue;
}
$tableIndexColumns = !empty($this->table->indexes[$name]->columns) ? $this->table->indexes[$name]->columns : [];
$oldTableIndexColumns = !empty($this->oldTable->indexes[$name]->columns) ? $this->oldTable->indexes[$name]->columns : [];
if (\count(array_merge(array_diff($tableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns)),
array_diff($oldTableIndexColumns, array_intersect($tableIndexColumns, $oldTableIndexColumns))))) {
if ($this->showOnly) {
echo " - different index '$name' columns (";
echo 'DB: (' . implode(', ', $tableIndexColumns) . ') <> ';
echo 'MIG: (' . implode(', ', $oldTableIndexColumns) . "))\n";
} else {
$this->plan->dropIndex[] = $name;
$this->plan->createIndex[$name] = $index;
}
$different = true;
}
}
foreach ($this->oldTable->indexes as $name => $index) {
if (!isset($this->table->indexes[$name])) {
if ($this->showOnly) {
echo " - excessive index '$name'\n";
} else {
$this->plan->dropIndex[] = $name;
}
$different = true;
}
}
return $different;
}
/**
* Checks if new updating migration is required.
* @return bool
* @throws ErrorException
*/
public function isUpdateRequired(): bool
{
$history = $this->fetchHistory();
if (!empty($history)) {
$this->setDummyMigrationClass();
foreach ($history as $migration => $time) {
$migration = trim($migration, '\\');
if (\in_array($migration, $this->skipMigrations, true)) {
continue;
}
if (!$this->gatherChanges($this->extract($migration))) {
break;
}
}
return $this->compareStructures();
}
return true;
}
/**
* Generates migration content or echoes exception message.
* @return string
*/
public function generateMigration(): string
{
if (empty($this->_modifications)) {
return parent::generateMigration();
}
return $this->view->renderFile(Yii::getAlias($this->templateFileUpdate), [
'className' => $this->className,
'table' => $this->table,
'plan' => $this->plan,
'namespace' => $this->normalizedNamespace
]);
}
}