|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Migrations\Atomizer; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\Schema\AbstractColumn; |
|
|
|
|
|
|
8
|
|
|
use Cycle\Database\Schema\AbstractForeignKey; |
|
9
|
|
|
use Cycle\Database\Schema\AbstractIndex; |
|
10
|
|
|
use Cycle\Database\Schema\AbstractTable; |
|
11
|
|
|
use Cycle\Database\Schema\Comparator; |
|
12
|
|
|
use Spiral\Reactor\Partial\Source; |
|
13
|
|
|
use Spiral\Reactor\Serializer; |
|
14
|
|
|
use Spiral\Reactor\Traits\SerializerTrait; |
|
15
|
|
|
|
|
16
|
|
|
final class Renderer implements RendererInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use SerializerTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Comparator alteration states. |
|
22
|
|
|
*/ |
|
23
|
|
|
public const NEW_STATE = 0; |
|
24
|
|
|
public const ORIGINAL_STATE = 1; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
136 |
|
public function createTable(Source $source, AbstractTable $table): void |
|
30
|
|
|
{ |
|
31
|
136 |
|
$this->render( |
|
32
|
136 |
|
$source, |
|
33
|
136 |
|
'$this->table(%s)', |
|
34
|
|
|
$table |
|
35
|
|
|
); |
|
36
|
136 |
|
$comparator = $table->getComparator(); |
|
37
|
|
|
|
|
38
|
136 |
|
$this->declareColumns($source, $comparator); |
|
39
|
136 |
|
$this->declareIndexes($source, $comparator); |
|
40
|
136 |
|
$this->declareForeignKeys($source, $comparator, $table->getPrefix()); |
|
41
|
|
|
|
|
42
|
136 |
|
if (count($table->getPrimaryKeys())) { |
|
43
|
136 |
|
$this->render( |
|
44
|
|
|
$source, |
|
45
|
136 |
|
' ->setPrimaryKeys(%s)', |
|
46
|
136 |
|
$table->getPrimaryKeys() |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//Finalization |
|
51
|
136 |
|
$source->addLine(' ->create();'); |
|
52
|
136 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
104 |
|
public function updateTable(Source $source, AbstractTable $table): void |
|
58
|
|
|
{ |
|
59
|
104 |
|
$this->render( |
|
60
|
104 |
|
$source, |
|
61
|
104 |
|
'$this->table(%s)', |
|
62
|
|
|
$table |
|
63
|
|
|
); |
|
64
|
104 |
|
$comparator = $table->getComparator(); |
|
65
|
|
|
|
|
66
|
104 |
|
if ($comparator->isPrimaryChanged()) { |
|
67
|
8 |
|
$this->render( |
|
68
|
|
|
$source, |
|
69
|
8 |
|
' ->setPrimaryKeys(%s)', |
|
70
|
8 |
|
$table->getPrimaryKeys() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
104 |
|
$this->declareColumns($source, $comparator); |
|
75
|
104 |
|
$this->declareIndexes($source, $comparator); |
|
76
|
104 |
|
$this->declareForeignKeys($source, $comparator, $table->getPrefix()); |
|
77
|
|
|
|
|
78
|
|
|
//Finalization |
|
79
|
104 |
|
$source->addLine(' ->update();'); |
|
80
|
104 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
104 |
|
public function revertTable(Source $source, AbstractTable $table): void |
|
86
|
|
|
{ |
|
87
|
|
|
//Get table blueprint |
|
88
|
104 |
|
$this->render( |
|
89
|
104 |
|
$source, |
|
90
|
104 |
|
'$this->table(%s)', |
|
91
|
|
|
$table |
|
92
|
|
|
); |
|
93
|
104 |
|
$comparator = $table->getComparator(); |
|
94
|
|
|
|
|
95
|
104 |
|
$this->revertForeignKeys($source, $comparator, $table->getPrefix()); |
|
96
|
104 |
|
$this->revertIndexes($source, $comparator); |
|
97
|
104 |
|
$this->revertColumns($source, $comparator); |
|
98
|
|
|
|
|
99
|
|
|
//Finalization |
|
100
|
104 |
|
$source->addLine(' ->update();'); |
|
101
|
104 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
136 |
|
public function dropTable(Source $source, AbstractTable $table): void |
|
107
|
|
|
{ |
|
108
|
136 |
|
$this->render( |
|
109
|
136 |
|
$source, |
|
110
|
136 |
|
'$this->table(%s)->drop();', |
|
111
|
|
|
$table |
|
112
|
|
|
); |
|
113
|
136 |
|
} |
|
114
|
|
|
|
|
115
|
136 |
|
private function declareColumns(Source $source, Comparator $comparator): void |
|
116
|
|
|
{ |
|
117
|
136 |
|
foreach ($comparator->addedColumns() as $column) { |
|
118
|
136 |
|
$this->render( |
|
119
|
136 |
|
$source, |
|
120
|
136 |
|
' ->addColumn(%s, %s, %s)', |
|
121
|
136 |
|
$column->getName(), |
|
122
|
136 |
|
$column->getDeclaredType() ?? $column->getAbstractType(), |
|
123
|
|
|
$column |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
136 |
|
foreach ($comparator->alteredColumns() as $pair) { |
|
128
|
48 |
|
$this->alterColumn( |
|
129
|
48 |
|
$source, |
|
130
|
48 |
|
$pair[self::NEW_STATE], |
|
131
|
48 |
|
$pair[self::ORIGINAL_STATE] |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
136 |
|
foreach ($comparator->droppedColumns() as $column) { |
|
136
|
16 |
|
$this->render( |
|
137
|
|
|
$source, |
|
138
|
16 |
|
' ->dropColumn(%s)', |
|
139
|
16 |
|
$column->getName() |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
136 |
|
} |
|
143
|
|
|
|
|
144
|
136 |
|
private function declareIndexes(Source $source, Comparator $comparator): void |
|
145
|
|
|
{ |
|
146
|
136 |
|
foreach ($comparator->addedIndexes() as $index) { |
|
147
|
104 |
|
$this->render( |
|
148
|
104 |
|
$source, |
|
149
|
104 |
|
' ->addIndex(%s, %s)', |
|
150
|
104 |
|
$index->getColumns(), |
|
151
|
|
|
$index |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
136 |
|
foreach ($comparator->alteredIndexes() as $pair) { |
|
156
|
|
|
/** @var AbstractIndex $index */ |
|
157
|
16 |
|
$index = $pair[self::NEW_STATE]; |
|
158
|
16 |
|
$this->render( |
|
159
|
16 |
|
$source, |
|
160
|
16 |
|
' ->alterIndex(%s, %s)', |
|
161
|
16 |
|
$index->getColumns(), |
|
162
|
|
|
$index |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
136 |
|
foreach ($comparator->droppedIndexes() as $index) { |
|
167
|
8 |
|
$this->render( |
|
168
|
|
|
$source, |
|
169
|
8 |
|
' ->dropIndex(%s)', |
|
170
|
8 |
|
$index->getColumns() |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
136 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $prefix Database isolation prefix |
|
177
|
|
|
*/ |
|
178
|
136 |
|
private function declareForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void |
|
179
|
|
|
{ |
|
180
|
136 |
|
foreach ($comparator->addedForeignKeys() as $key) { |
|
181
|
40 |
|
$this->render( |
|
182
|
40 |
|
$source, |
|
183
|
40 |
|
' ->addForeignKey(%s, %s, %s, %s)', |
|
184
|
40 |
|
$key->getColumns(), |
|
185
|
40 |
|
substr($key->getForeignTable(), strlen($prefix)), |
|
186
|
40 |
|
$key->getForeignKeys(), |
|
187
|
|
|
$key |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
136 |
|
foreach ($comparator->alteredForeignKeys() as $pair) { |
|
192
|
|
|
/** @var AbstractForeignKey $key */ |
|
193
|
8 |
|
$key = $pair[self::NEW_STATE]; |
|
194
|
8 |
|
$this->render( |
|
195
|
8 |
|
$source, |
|
196
|
8 |
|
' ->alterForeignKey(%s, %s, %s, %s)', |
|
197
|
8 |
|
$key->getColumns(), |
|
198
|
8 |
|
substr($key->getForeignTable(), strlen($prefix)), |
|
199
|
8 |
|
$key->getForeignKeys(), |
|
200
|
|
|
$key |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
136 |
|
foreach ($comparator->droppedForeignKeys() as $key) { |
|
205
|
8 |
|
$this->render( |
|
206
|
|
|
$source, |
|
207
|
8 |
|
' ->dropForeignKey(%s)', |
|
208
|
8 |
|
$key->getColumns() |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
136 |
|
} |
|
212
|
|
|
|
|
213
|
104 |
|
private function revertColumns(Source $source, Comparator $comparator): void |
|
214
|
|
|
{ |
|
215
|
104 |
|
foreach ($comparator->droppedColumns() as $column) { |
|
216
|
16 |
|
$this->render( |
|
217
|
16 |
|
$source, |
|
218
|
16 |
|
' ->addColumn(%s, %s, %s)', |
|
219
|
16 |
|
$column->getName(), |
|
220
|
16 |
|
$column->getDeclaredType() ?? $column->getAbstractType(), |
|
221
|
|
|
$column |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
104 |
|
foreach ($comparator->alteredColumns() as $pair) { |
|
226
|
48 |
|
$this->alterColumn( |
|
227
|
48 |
|
$source, |
|
228
|
48 |
|
$pair[self::ORIGINAL_STATE], |
|
229
|
48 |
|
$pair[self::NEW_STATE] |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
104 |
|
foreach ($comparator->addedColumns() as $column) { |
|
234
|
16 |
|
$this->render( |
|
235
|
|
|
$source, |
|
236
|
16 |
|
' ->dropColumn(%s)', |
|
237
|
16 |
|
$column->getName() |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
104 |
|
} |
|
241
|
|
|
|
|
242
|
104 |
|
private function revertIndexes(Source $source, Comparator $comparator): void |
|
243
|
|
|
{ |
|
244
|
104 |
|
foreach ($comparator->droppedIndexes() as $index) { |
|
245
|
8 |
|
$this->render( |
|
246
|
8 |
|
$source, |
|
247
|
8 |
|
' ->addIndex(%s, %s)', |
|
248
|
8 |
|
$index->getColumns(), |
|
249
|
|
|
$index |
|
250
|
|
|
); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
104 |
|
foreach ($comparator->alteredIndexes() as $pair) { |
|
254
|
|
|
/** @var AbstractIndex $index */ |
|
255
|
16 |
|
$index = $pair[self::ORIGINAL_STATE]; |
|
256
|
16 |
|
$this->render( |
|
257
|
16 |
|
$source, |
|
258
|
16 |
|
' ->alterIndex(%s, %s)', |
|
259
|
16 |
|
$index->getColumns(), |
|
260
|
|
|
$index |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
104 |
|
foreach ($comparator->addedIndexes() as $index) { |
|
265
|
16 |
|
$this->render( |
|
266
|
|
|
$source, |
|
267
|
16 |
|
' ->dropIndex(%s)', |
|
268
|
16 |
|
$index->getColumns() |
|
269
|
|
|
); |
|
270
|
|
|
} |
|
271
|
104 |
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param string $prefix Database isolation prefix. |
|
275
|
|
|
*/ |
|
276
|
104 |
|
private function revertForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void |
|
277
|
|
|
{ |
|
278
|
104 |
|
foreach ($comparator->droppedForeignKeys() as $key) { |
|
279
|
8 |
|
$this->render( |
|
280
|
8 |
|
$source, |
|
281
|
8 |
|
' ->addForeignKey(%s, %s, %s, %s)', |
|
282
|
8 |
|
$key->getColumns(), |
|
283
|
8 |
|
substr($key->getForeignTable(), strlen($prefix)), |
|
284
|
8 |
|
$key->getForeignKeys(), |
|
285
|
|
|
$key |
|
286
|
|
|
); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
104 |
|
foreach ($comparator->alteredForeignKeys() as $pair) { |
|
290
|
|
|
/** @var AbstractForeignKey $key */ |
|
291
|
8 |
|
$key = $pair[self::ORIGINAL_STATE]; |
|
292
|
8 |
|
$this->render( |
|
293
|
8 |
|
$source, |
|
294
|
8 |
|
' ->alterForeignKey(%s, %s, %s, %s)', |
|
295
|
8 |
|
$key->getColumns(), |
|
296
|
8 |
|
substr($key->getForeignTable(), strlen($prefix)), |
|
297
|
8 |
|
$key->getForeignKeys(), |
|
298
|
|
|
$key |
|
299
|
|
|
); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
104 |
|
foreach ($comparator->addedForeignKeys() as $key) { |
|
303
|
8 |
|
$this->render($source, ' ->dropForeignKey(%s)', $key->getColumns()); |
|
304
|
|
|
} |
|
305
|
104 |
|
} |
|
306
|
|
|
|
|
307
|
48 |
|
protected function alterColumn( |
|
308
|
|
|
Source $source, |
|
309
|
|
|
AbstractColumn $column, |
|
310
|
|
|
AbstractColumn $original |
|
311
|
|
|
): void { |
|
312
|
48 |
|
if ($column->getName() !== $original->getName()) { |
|
313
|
16 |
|
$name = $original->getName(); |
|
314
|
|
|
} else { |
|
315
|
32 |
|
$name = $column->getName(); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
48 |
|
$this->render( |
|
319
|
48 |
|
$source, |
|
320
|
48 |
|
' ->alterColumn(%s, %s, %s)', |
|
321
|
|
|
$name, |
|
322
|
48 |
|
$column->getDeclaredType() ?? $column->getAbstractType(), |
|
323
|
|
|
$column |
|
324
|
|
|
); |
|
325
|
|
|
|
|
326
|
48 |
|
if ($column->getName() !== $original->getName()) { |
|
327
|
16 |
|
$this->render( |
|
328
|
|
|
$source, |
|
329
|
16 |
|
' ->renameColumn(%s, %s)', |
|
330
|
|
|
$name, |
|
331
|
16 |
|
$column->getName() |
|
332
|
|
|
); |
|
333
|
|
|
} |
|
334
|
48 |
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Render values and options into source. |
|
338
|
|
|
* |
|
339
|
|
|
* @param array ...$values |
|
340
|
|
|
*/ |
|
341
|
136 |
|
protected function render(Source $source, string $format, ...$values): void |
|
342
|
|
|
{ |
|
343
|
136 |
|
$serializer = $this->getSerializer(); |
|
344
|
|
|
|
|
345
|
136 |
|
$rendered = []; |
|
346
|
136 |
|
foreach ($values as $value) { |
|
347
|
136 |
|
if ($value instanceof AbstractTable) { |
|
348
|
136 |
|
$rendered[] = $serializer->serialize( |
|
349
|
136 |
|
substr($value->getName(), strlen($value->getPrefix())) |
|
350
|
|
|
); |
|
351
|
136 |
|
continue; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
136 |
|
if ($value instanceof AbstractColumn) { |
|
355
|
136 |
|
$rendered[] = $this->columnOptions($serializer, $value); |
|
356
|
136 |
|
continue; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
136 |
|
if ($value instanceof AbstractIndex) { |
|
360
|
104 |
|
$rendered[] = $this->indexOptions($serializer, $value); |
|
361
|
104 |
|
continue; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
136 |
|
if ($value instanceof AbstractForeignKey) { |
|
365
|
40 |
|
$rendered[] = $this->foreignKeyOptions($serializer, $value); |
|
366
|
40 |
|
continue; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
// numeric array |
|
370
|
136 |
|
if (is_array($value) && count($value) > 0 && is_numeric(array_keys($value)[0])) { |
|
371
|
136 |
|
$rendered[] = '["' . implode('", "', $value) . '"]'; |
|
372
|
136 |
|
continue; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
136 |
|
$rendered[] = $serializer->serialize($value); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
136 |
|
$lines = sprintf($format, ...$rendered); |
|
379
|
136 |
|
foreach (explode("\n", $lines) as $line) { |
|
380
|
136 |
|
$source->addLine($line); |
|
381
|
|
|
} |
|
382
|
136 |
|
} |
|
383
|
|
|
|
|
384
|
136 |
|
private function columnOptions(Serializer $serializer, AbstractColumn $column): string |
|
385
|
|
|
{ |
|
386
|
136 |
|
$options = [ |
|
387
|
136 |
|
'nullable' => $column->isNullable(), |
|
388
|
136 |
|
'default' => $column->getDefaultValue(), |
|
389
|
|
|
]; |
|
390
|
|
|
|
|
391
|
136 |
|
if ($column->getAbstractType() === 'enum') { |
|
392
|
8 |
|
$options['values'] = $column->getEnumValues(); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
136 |
|
if ($column->getAbstractType() === 'string') { |
|
396
|
6 |
|
$options['size'] = $column->getSize(); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
136 |
|
if ($column->getAbstractType() === 'decimal') { |
|
400
|
8 |
|
$options['scale'] = $column->getScale(); |
|
401
|
8 |
|
$options['precision'] = $column->getPrecision(); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
136 |
|
$default = $options['default']; |
|
405
|
|
|
if ($column::DATETIME_NOW === ($default instanceof \Stringable ? (string)$default : $default)) { |
|
406
|
|
|
$options['default'] = AbstractColumn::DATETIME_NOW; |
|
407
|
104 |
|
} |
|
408
|
|
|
|
|
409
|
104 |
|
return $this->mountIndents($serializer->serialize($options)); |
|
410
|
104 |
|
} |
|
411
|
|
|
|
|
412
|
104 |
|
private function indexOptions(Serializer $serializer, AbstractIndex $index): string |
|
413
|
104 |
|
{ |
|
414
|
|
|
return $this->mountIndents( |
|
415
|
|
|
$serializer->serialize( |
|
416
|
|
|
[ |
|
417
|
|
|
'name' => $index->getName(), |
|
418
|
|
|
'unique' => $index->isUnique(), |
|
419
|
40 |
|
] |
|
420
|
|
|
) |
|
421
|
|
|
); |
|
422
|
|
|
} |
|
423
|
40 |
|
|
|
424
|
40 |
|
private function foreignKeyOptions( |
|
425
|
|
|
Serializer $serializer, |
|
426
|
40 |
|
AbstractForeignKey $reference |
|
427
|
40 |
|
): string { |
|
428
|
40 |
|
return $this->mountIndents( |
|
429
|
|
|
$serializer->serialize( |
|
430
|
|
|
[ |
|
431
|
|
|
'name' => $reference->getName(), |
|
432
|
|
|
'delete' => $reference->getDeleteRule(), |
|
433
|
|
|
'update' => $reference->getUpdateRule(), |
|
434
|
|
|
] |
|
435
|
|
|
) |
|
436
|
|
|
); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
136 |
|
/** |
|
440
|
|
|
* Mount indents for column and index options. |
|
441
|
136 |
|
* |
|
442
|
136 |
|
* @param $serialized |
|
443
|
136 |
|
*/ |
|
444
|
136 |
|
private function mountIndents(string $serialized): string |
|
445
|
|
|
{ |
|
446
|
|
|
$lines = explode("\n", $serialized); |
|
447
|
136 |
|
foreach ($lines as &$line) { |
|
448
|
|
|
$line = ' ' . $line; |
|
449
|
|
|
unset($line); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
return ltrim(implode("\n", $lines)); |
|
453
|
|
|
} |
|
454
|
|
|
} |
|
455
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths