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