1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Generator\Migrations; |
6
|
|
|
|
7
|
|
|
use Cycle\Database\Schema\AbstractTable; |
8
|
|
|
use Cycle\Migrations\Atomizer\Atomizer; |
9
|
|
|
|
10
|
|
|
final class NameBasedOnChangesGenerator implements NameGeneratorInterface |
11
|
|
|
{ |
12
|
|
|
public function generate(Atomizer $atomizer): string |
13
|
|
|
{ |
14
|
|
|
$name = []; |
15
|
|
|
|
16
|
|
|
foreach ($atomizer->getTables() as $table) { |
17
|
|
|
if ($table->getStatus() === AbstractTable::STATUS_NEW) { |
18
|
|
|
$name[] = 'create_' . $table->getName(); |
19
|
|
|
continue; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) { |
23
|
|
|
$name[] = 'drop_' . $table->getName(); |
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ($table->getComparator()->isRenamed()) { |
28
|
|
|
$name[] = 'rename_' . $table->getInitialName(); |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$name[] = 'change_' . $table->getName(); |
33
|
|
|
|
34
|
|
|
$comparator = $table->getComparator(); |
35
|
|
|
|
36
|
|
|
foreach ($comparator->addedColumns() as $column) { |
37
|
|
|
$name[] = 'add_' . $column->getName(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($comparator->droppedColumns() as $column) { |
41
|
|
|
$name[] = 'rm_' . $column->getName(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($comparator->alteredColumns() as $column) { |
45
|
|
|
$name[] = 'alter_' . $column[0]->getName(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($comparator->addedIndexes() as $index) { |
49
|
|
|
$name[] = 'add_index_' . $index->getName(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach ($comparator->droppedIndexes() as $index) { |
53
|
|
|
$name[] = 'rm_index_' . $index->getName(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($comparator->alteredIndexes() as $index) { |
57
|
|
|
$name[] = 'alter_index_' . $index[0]->getName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($comparator->addedForeignKeys() as $fk) { |
61
|
|
|
$name[] = 'add_fk_' . $fk->getName(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($comparator->droppedForeignKeys() as $fk) { |
65
|
|
|
$name[] = 'rm_fk_' . $fk->getName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($comparator->alteredForeignKeys() as $fk) { |
69
|
|
|
$name[] = 'alter_fk_' . $fk[0]->getName(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return \implode('_', $name); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|