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->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) { |
64
|
|
|
$this->output->writeln(' - drop table'); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$this->output->isVerbose()) { |
69
|
|
|
$this->output->writeln( |
70
|
|
|
\sprintf( |
71
|
|
|
': <fg=green>%s</fg=green> change(s) detected', |
72
|
|
|
$this->numChanges($table), |
73
|
|
|
), |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->output->write("\n"); |
80
|
|
|
|
81
|
|
|
if (!$table->exists()) { |
82
|
|
|
$this->output->writeln(' - create table'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$cmp = $table->getComparator(); |
86
|
|
|
|
87
|
|
|
$this->describeColumns($cmp); |
88
|
|
|
$this->describeIndexes($cmp); |
89
|
|
|
$this->describeFKs($cmp); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function describeColumns(ComparatorInterface $cmp): void |
93
|
|
|
{ |
94
|
|
|
foreach ($cmp->addedColumns() as $column) { |
95
|
|
|
$this->output->writeln(" - add column <fg=yellow>[{$column->getName()}]</fg=yellow>"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($cmp->droppedColumns() as $column) { |
99
|
|
|
$this->output->writeln(" - drop column <fg=yellow>[{$column->getName()}]</fg=yellow>"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($cmp->alteredColumns() as $column) { |
103
|
|
|
$column = $column[0]; |
104
|
|
|
$this->output->writeln(" - alter column <fg=yellow>[{$column->getName()}]</fg=yellow>"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function describeIndexes(ComparatorInterface $cmp): void |
109
|
|
|
{ |
110
|
|
|
foreach ($cmp->addedIndexes() as $index) { |
111
|
|
|
$index = \implode(', ', $index->getColumns()); |
112
|
|
|
$this->output->writeln(" - add index on <fg=yellow>[{$index}]</fg=yellow>"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($cmp->droppedIndexes() as $index) { |
116
|
|
|
$index = \implode(', ', $index->getColumns()); |
117
|
|
|
$this->output->writeln(" - drop index on <fg=yellow>[{$index}]</fg=yellow>"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($cmp->alteredIndexes() as $index) { |
121
|
|
|
$index = $index[0]; |
122
|
|
|
$index = \implode(', ', $index->getColumns()); |
123
|
|
|
$this->output->writeln(" - alter index on <fg=yellow>[{$index}]</fg=yellow>"); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function describeFKs(ComparatorInterface $cmp): void |
128
|
|
|
{ |
129
|
|
|
foreach ($cmp->addedForeignKeys() as $fk) { |
130
|
|
|
$fkColumns = \implode(', ', $fk->getColumns()); |
131
|
|
|
$this->output->writeln(" - add foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>"); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($cmp->droppedForeignKeys() as $fk) { |
135
|
|
|
$fkColumns = \implode(', ', $fk->getColumns()); |
136
|
|
|
$this->output->writeln(" - drop foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>"); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($cmp->alteredForeignKeys() as $fk) { |
140
|
|
|
$fk = $fk[0]; |
141
|
|
|
$fkColumns = \implode(', ', $fk->getColumns()); |
142
|
|
|
$this->output->writeln(" - alter foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>"); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function numChanges(AbstractTable $table): int |
147
|
|
|
{ |
148
|
|
|
$cmp = $table->getComparator(); |
149
|
|
|
|
150
|
|
|
return \count($cmp->addedColumns()) |
151
|
|
|
+ \count($cmp->droppedColumns()) |
152
|
|
|
+ \count($cmp->alteredColumns()) |
153
|
|
|
+ \count($cmp->addedIndexes()) |
154
|
|
|
+ \count($cmp->droppedIndexes()) |
155
|
|
|
+ \count($cmp->alteredIndexes()) |
156
|
|
|
+ \count($cmp->addedForeignKeys()) |
157
|
|
|
+ \count($cmp->droppedForeignKeys()) |
158
|
|
|
+ \count($cmp->alteredForeignKeys()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function hasTableChanges(AbstractTable $table): bool |
162
|
|
|
{ |
163
|
|
|
return $table->getComparator()->hasChanges() |
164
|
|
|
|| !$table->exists() |
165
|
|
|
|| $table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|