1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Generator; |
4
|
|
|
|
5
|
|
|
use Cycle\Schema\GeneratorInterface; |
6
|
|
|
use Cycle\Schema\Registry; |
7
|
|
|
use Spiral\Database\Schema\AbstractTable; |
8
|
|
|
use Spiral\Database\Schema\Comparator; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
final class PrintChanges implements GeneratorInterface |
12
|
|
|
{ |
13
|
|
|
private OutputInterface $output; |
14
|
|
|
|
15
|
|
|
private array $changes = []; |
16
|
|
|
|
17
|
|
|
public function __construct(OutputInterface $output) |
18
|
|
|
{ |
19
|
|
|
$this->output = $output; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function run(Registry $registry): Registry |
23
|
|
|
{ |
24
|
|
|
$this->output->writeln('<info>Detecting schema changes:</info>'); |
25
|
|
|
$this->changes = []; |
26
|
|
|
foreach ($registry->getIterator() as $entity) { |
27
|
|
|
if ($registry->hasTable($entity)) { |
28
|
|
|
$table = $registry->getTableSchema($entity); |
29
|
|
|
if ($table->getComparator()->hasChanges()) { |
30
|
|
|
$this->changes[] = [ |
31
|
|
|
'database' => $registry->getDatabase($entity), |
32
|
|
|
'table' => $registry->getTable($entity), |
33
|
|
|
'schema' => $table, |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
if ($this->changes === []) { |
39
|
|
|
$this->output->writeln('<fg=yellow>no database changes has been detected</fg=yellow>'); |
40
|
|
|
return $registry; |
41
|
|
|
} |
42
|
|
|
foreach ($this->changes as $change) { |
43
|
|
|
$this->output->write(sprintf('• <fg=cyan>%s.%s</fg=cyan>', $change['database'], $change['table'])); |
44
|
|
|
$this->describeChanges($change['schema']); |
45
|
|
|
} |
46
|
|
|
return $registry; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function hasChanges(): bool |
50
|
|
|
{ |
51
|
|
|
return $this->changes !== []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function describeChanges(AbstractTable $table): void |
55
|
|
|
{ |
56
|
|
|
if (!$this->output->isVerbose()) { |
57
|
|
|
$this->output->writeln(sprintf( |
58
|
|
|
': <fg=green>%s</fg=green> change(s) detected', |
59
|
|
|
$this->numChanges($table) |
60
|
|
|
)); |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$this->output->write("\n"); |
64
|
|
|
if (!$table->exists()) { |
65
|
|
|
$this->output->writeln(' - create table'); |
66
|
|
|
} |
67
|
|
|
if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) { |
68
|
|
|
$this->output->writeln(' - drop table'); |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
$cmp = $table->getComparator(); |
72
|
|
|
$this->describeColumns($cmp); |
73
|
|
|
$this->describeIndexes($cmp); |
74
|
|
|
$this->describeFKs($cmp); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function describeColumns(Comparator $cmp): void |
78
|
|
|
{ |
79
|
|
|
foreach ($cmp->addedColumns() as $column) { |
80
|
|
|
$this->output->writeln(" - add column <fg=yellow>{$column->getName()}</fg=yellow>"); |
81
|
|
|
} |
82
|
|
|
foreach ($cmp->droppedColumns() as $column) { |
83
|
|
|
$this->output->writeln(" - drop column <fg=yellow>{$column->getName()}</fg=yellow>"); |
84
|
|
|
} |
85
|
|
|
foreach ($cmp->alteredColumns() as $column) { |
86
|
|
|
$column = $column[0]; |
87
|
|
|
$this->output->writeln(" - alter column <fg=yellow>{$column->getName()}</fg=yellow>"); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function describeIndexes(Comparator $cmp): void |
92
|
|
|
{ |
93
|
|
|
foreach ($cmp->addedIndexes() as $index) { |
94
|
|
|
$index = join(', ', $index->getColumns()); |
95
|
|
|
$this->output->writeln(" - add index on <fg=yellow>[{$index}]</fg=yellow>"); |
96
|
|
|
} |
97
|
|
|
foreach ($cmp->droppedIndexes() as $index) { |
98
|
|
|
$index = join(', ', $index->getColumns()); |
99
|
|
|
$this->output->writeln(" - drop index on <fg=yellow>[{$index}]</fg=yellow>"); |
100
|
|
|
} |
101
|
|
|
foreach ($cmp->alteredIndexes() as $index) { |
102
|
|
|
$index = $index[0]; |
103
|
|
|
$index = join(', ', $index->getColumns()); |
104
|
|
|
$this->output->writeln(" - alter index on <fg=yellow>[{$index}]</fg=yellow>"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function describeFKs(Comparator $cmp): void |
109
|
|
|
{ |
110
|
|
|
foreach ($cmp->addedForeignKeys() as $fk) { |
111
|
|
|
$fkColumns = join(', ', $fk->getColumns()); |
112
|
|
|
$this->output->writeln(" - add foreign key on <fg=yellow>{$fkColumns}</fg=yellow>"); |
113
|
|
|
} |
114
|
|
|
foreach ($cmp->droppedForeignKeys() as $fk) { |
115
|
|
|
$fkColumns = join(', ', $fk->getColumns()); |
116
|
|
|
$this->output->writeln(" - drop foreign key <fg=yellow>{$fkColumns}</fg=yellow>"); |
117
|
|
|
} |
118
|
|
|
foreach ($cmp->alteredForeignKeys() as $fk) { |
119
|
|
|
$fk = $fk[0]; |
120
|
|
|
$fkColumns = join(', ', $fk->getColumns()); |
121
|
|
|
$this->output->writeln(" - alter foreign key <fg=yellow>{$fkColumns}</fg=yellow>"); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function numChanges(AbstractTable $table): int |
126
|
|
|
{ |
127
|
|
|
$cmp = $table->getComparator(); |
128
|
|
|
return count($cmp->addedColumns()) |
129
|
|
|
+ count($cmp->droppedColumns()) |
130
|
|
|
+ count($cmp->alteredColumns()) |
131
|
|
|
+ count($cmp->addedIndexes()) |
132
|
|
|
+ count($cmp->droppedIndexes()) |
133
|
|
|
+ count($cmp->alteredIndexes()) |
134
|
|
|
+ count($cmp->addedForeignKeys()) |
135
|
|
|
+ count($cmp->droppedForeignKeys()) |
136
|
|
|
+ count($cmp->alteredForeignKeys()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|