1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Schema; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Compares two table states. |
16
|
|
|
*/ |
17
|
|
|
final class Comparator implements ComparatorInterface |
18
|
|
|
{ |
19
|
1950 |
|
public function __construct( |
20
|
|
|
private State $initial, |
21
|
|
|
private State $current, |
22
|
|
|
) {} |
23
|
1950 |
|
|
24
|
|
|
public function hasChanges(): bool |
25
|
1836 |
|
{ |
26
|
|
|
if ($this->isRenamed()) { |
27
|
1836 |
|
return true; |
28
|
8 |
|
} |
29
|
|
|
|
30
|
|
|
if ($this->isPrimaryChanged()) { |
31
|
1836 |
|
return true; |
32
|
112 |
|
} |
33
|
|
|
|
34
|
|
|
$difference = [ |
35
|
1836 |
|
\count($this->addedColumns()), |
36
|
1836 |
|
\count($this->droppedColumns()), |
37
|
1836 |
|
\count($this->alteredColumns()), |
38
|
1836 |
|
\count($this->addedIndexes()), |
39
|
1836 |
|
\count($this->droppedIndexes()), |
40
|
1836 |
|
\count($this->alteredIndexes()), |
41
|
1836 |
|
\count($this->addedForeignKeys()), |
42
|
1836 |
|
\count($this->droppedForeignKeys()), |
43
|
1836 |
|
\count($this->alteredForeignKeys()), |
44
|
1836 |
|
]; |
45
|
|
|
|
46
|
|
|
return \array_sum($difference) !== 0; |
47
|
1836 |
|
} |
48
|
|
|
|
49
|
|
|
public function isRenamed(): bool |
50
|
1836 |
|
{ |
51
|
|
|
return $this->current->getName() !== $this->initial->getName(); |
52
|
1836 |
|
} |
53
|
|
|
|
54
|
|
|
public function isPrimaryChanged(): bool |
55
|
1836 |
|
{ |
56
|
|
|
return $this->current->getPrimaryKeys() !== $this->initial->getPrimaryKeys(); |
57
|
1836 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return AbstractColumn[] |
61
|
|
|
*/ |
62
|
|
|
public function addedColumns(): array |
63
|
1836 |
|
{ |
64
|
|
|
$difference = []; |
65
|
1836 |
|
|
66
|
|
|
$initialColumns = $this->initial->getColumns(); |
67
|
1836 |
|
foreach ($this->current->getColumns() as $name => $column) { |
68
|
1836 |
|
if (!isset($initialColumns[$name]) && !$column->isReadonlySchema()) { |
69
|
1836 |
|
$difference[] = $column; |
70
|
168 |
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $difference; |
74
|
1836 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return AbstractColumn[] |
78
|
|
|
*/ |
79
|
|
|
public function droppedColumns(): array |
80
|
1950 |
|
{ |
81
|
|
|
$difference = []; |
82
|
1950 |
|
|
83
|
|
|
$currentColumns = $this->current->getColumns(); |
84
|
1950 |
|
foreach ($this->initial->getColumns() as $name => $column) { |
85
|
1950 |
|
if (!isset($currentColumns[$name]) && !$column->isReadonlySchema()) { |
86
|
1836 |
|
$difference[] = $column; |
87
|
36 |
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $difference; |
91
|
1950 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns array where each value contain current and initial element state. |
95
|
|
|
*/ |
96
|
|
|
public function alteredColumns(): array |
97
|
1950 |
|
{ |
98
|
|
|
$difference = []; |
99
|
1950 |
|
|
100
|
|
|
$initialColumns = $this->initial->getColumns(); |
101
|
1950 |
|
foreach ($this->current->getColumns() as $name => $column) { |
102
|
1950 |
|
if (!isset($initialColumns[$name])) { |
103
|
1950 |
|
//Added into schema |
104
|
|
|
continue; |
105
|
1948 |
|
} |
106
|
|
|
|
107
|
|
|
if (!$column->compare($initialColumns[$name]) && !$column->isReadonlySchema()) { |
108
|
1836 |
|
$difference[] = [$column, $initialColumns[$name]]; |
109
|
150 |
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $difference; |
113
|
1950 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return AbstractIndex[] |
117
|
|
|
*/ |
118
|
|
|
public function addedIndexes(): array |
119
|
1836 |
|
{ |
120
|
|
|
$difference = []; |
121
|
1836 |
|
foreach ($this->current->getIndexes() as $_ => $index) { |
122
|
1836 |
|
if (!$this->initial->hasIndex($index->getColumnsWithSort())) { |
123
|
456 |
|
$difference[] = $index; |
124
|
192 |
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $difference; |
128
|
1836 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return AbstractIndex[] |
132
|
|
|
*/ |
133
|
|
|
public function droppedIndexes(): array |
134
|
1836 |
|
{ |
135
|
|
|
$difference = []; |
136
|
1836 |
|
foreach ($this->initial->getIndexes() as $_ => $index) { |
137
|
1836 |
|
if (!$this->current->hasIndex($index->getColumnsWithSort())) { |
138
|
456 |
|
$difference[] = $index; |
139
|
40 |
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $difference; |
143
|
1836 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns array where each value contain current and initial element state. |
147
|
|
|
*/ |
148
|
|
|
public function alteredIndexes(): array |
149
|
1836 |
|
{ |
150
|
|
|
$difference = []; |
151
|
1836 |
|
|
152
|
|
|
foreach ($this->current->getIndexes() as $_ => $index) { |
153
|
1836 |
|
if (!$this->initial->hasIndex($index->getColumnsWithSort())) { |
154
|
456 |
|
//Added into schema |
155
|
|
|
continue; |
156
|
192 |
|
} |
157
|
|
|
|
158
|
|
|
$initial = $this->initial->findIndex($index->getColumnsWithSort()); |
159
|
456 |
|
if (!$index->compare($initial)) { |
160
|
456 |
|
$difference[] = [$index, $initial]; |
161
|
8 |
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $difference; |
165
|
1836 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return AbstractForeignKey[] |
169
|
|
|
*/ |
170
|
|
|
public function addedForeignKeys(): array |
171
|
1836 |
|
{ |
172
|
|
|
$difference = []; |
173
|
1836 |
|
foreach ($this->current->getForeignKeys() as $_ => $foreignKey) { |
174
|
1836 |
|
if (!$this->initial->hasForeignKey($foreignKey->getColumns())) { |
175
|
176 |
|
$difference[] = $foreignKey; |
176
|
120 |
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $difference; |
180
|
1836 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return AbstractForeignKey[] |
184
|
|
|
*/ |
185
|
|
|
public function droppedForeignKeys(): array |
186
|
1836 |
|
{ |
187
|
|
|
$difference = []; |
188
|
1836 |
|
foreach ($this->initial->getForeignKeys() as $_ => $foreignKey) { |
189
|
1836 |
|
if (!$this->current->hasForeignKey($foreignKey->getColumns())) { |
190
|
224 |
|
$difference[] = $foreignKey; |
191
|
224 |
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $difference; |
195
|
1836 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns array where each value contain current and initial element state. |
199
|
|
|
*/ |
200
|
|
|
public function alteredForeignKeys(): array |
201
|
1836 |
|
{ |
202
|
|
|
$difference = []; |
203
|
1836 |
|
|
204
|
|
|
foreach ($this->current->getForeignKeys() as $_ => $foreignKey) { |
205
|
1836 |
|
if (!$this->initial->hasForeignKey($foreignKey->getColumns())) { |
206
|
176 |
|
//Added into schema |
207
|
|
|
continue; |
208
|
120 |
|
} |
209
|
|
|
|
210
|
|
|
$initial = $this->initial->findForeignKey($foreignKey->getColumns()); |
211
|
104 |
|
if (!$foreignKey->compare($initial)) { |
212
|
104 |
|
$difference[] = [$foreignKey, $initial]; |
213
|
56 |
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $difference; |
217
|
1836 |
|
} |
218
|
|
|
} |
219
|
|
|
|