Total Complexity | 115 |
Total Lines | 619 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MigrationTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MigrationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | trait MigrationTrait |
||
23 | { |
||
24 | private static $tableOptions = '@tableOptions'; |
||
25 | |||
26 | /** |
||
27 | * @param $refTable |
||
28 | * @param null $refColumn |
||
|
|||
29 | * |
||
30 | * @param string $type |
||
31 | * @param null $length |
||
32 | * @return ForeignKeyColumn |
||
33 | */ |
||
34 | public function foreignKey($refTable = null, $refColumn = null, $type = Schema::TYPE_INTEGER, $length = null) |
||
35 | { |
||
36 | return (new ForeignKeyColumn($type, $length))->refTable($refTable)->refColumn($refColumn)->setMigrate($this); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param array $columns |
||
41 | * @param bool $isUnique |
||
42 | * @return IndexColumn |
||
43 | */ |
||
44 | public function index($columns = [], $isUnique = false) |
||
45 | { |
||
46 | return (new IndexColumn())->setMigrate($this)->columns($columns)->unique($isUnique); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param null $refTable |
||
51 | * @param null $refColumn |
||
52 | * |
||
53 | * @return PivotColumn |
||
54 | */ |
||
55 | public function pivot($refTable = null, $refColumn = null) |
||
56 | { |
||
57 | return (new PivotColumn())->refTable($refTable)->refColumn($refColumn)->setMigrate($this); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $name |
||
62 | * @param string $table |
||
63 | * @param array|string $columns |
||
64 | * @param bool $unique |
||
65 | */ |
||
66 | public function createIndex($name, $table, $columns, $unique = false) |
||
67 | { |
||
68 | $suffix = $unique ? 'unq' : 'idx'; |
||
69 | if (is_null($name)) { |
||
70 | $name = self::formIndexName($table, $columns, $suffix); |
||
71 | $name = $this->expandTablePrefix($name); |
||
72 | } |
||
73 | $name = self::truncateName($name, 64, '_' . $suffix); |
||
74 | parent::createIndex($name, $table, $columns, $unique); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param ColumnSchema $column |
||
79 | * @return $this|ColumnSchemaBuilder |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | public function columnSchemaToBuilder(ColumnSchema $column) |
||
83 | { |
||
84 | $size = $column->size; |
||
85 | $precision = $column->precision; |
||
86 | $default = $column->defaultValue; |
||
87 | $scale = $column->scale; |
||
88 | if ($column->isPrimaryKey && $column->autoIncrement) { |
||
89 | return $this->primaryKey(); |
||
90 | } |
||
91 | switch ($column->type) { |
||
92 | case 'string': |
||
93 | $builder = $this->string($size); |
||
94 | break; |
||
95 | case 'integer': |
||
96 | $builder = $this->integer($size); |
||
97 | break; |
||
98 | case 'datetime': |
||
99 | $builder = $this->dateTime($precision); |
||
100 | break; |
||
101 | case 'text': |
||
102 | $builder = $this->text(); |
||
103 | break; |
||
104 | case 'smallint': |
||
105 | if ($size === 1) { |
||
106 | $default = (boolean)$default; |
||
107 | $builder = $this->boolean(); |
||
108 | } else { |
||
109 | $builder = $this->smallInteger($size); |
||
110 | } |
||
111 | break; |
||
112 | case 'binary': |
||
113 | $builder = $this->binary()->defaultValue($default); |
||
114 | break; |
||
115 | case 'decimal': |
||
116 | $builder = $this->decimal($precision, $scale); |
||
117 | break; |
||
118 | case 'double': |
||
119 | $builder = $this->double($precision)->defaultValue($default); |
||
120 | break; |
||
121 | default: |
||
122 | throw new \Exception("Column ($column->name) type '$column->type' not recognized"); |
||
123 | } |
||
124 | $builder->defaultValue($default); |
||
125 | if (!$column->allowNull) { |
||
126 | $builder->notNull(); |
||
127 | } |
||
128 | $builder->comment($column->comment); |
||
129 | return $builder; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param $name |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function expandTablePrefix($name) |
||
137 | { |
||
138 | return self::setTablePrefix($name, $this->db->tablePrefix); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param $name |
||
143 | * @param $prefix |
||
144 | * @return mixed |
||
145 | * @internal param $prefix |
||
146 | */ |
||
147 | public static function setTablePrefix($name, $prefix) |
||
148 | { |
||
149 | return preg_replace('#{{%([\w\-_]+)}}#', $prefix . '$1', $name); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $table |
||
154 | * @param array $columns |
||
155 | * @param null $options |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | public function createTable($table, $columns, $options = null) |
||
159 | { |
||
160 | /** |
||
161 | * @var PivotColumn[] $pvs |
||
162 | * @var ForeignKeyColumn[] $fks |
||
163 | */ |
||
164 | echo " > create table $table ..."; |
||
165 | $time = microtime(true); |
||
166 | $pvs = []; |
||
167 | $fks = []; |
||
168 | $pks = []; |
||
169 | foreach ($columns as $column => &$type) { |
||
170 | if ($column === self::$tableOptions && is_string($type)) { |
||
171 | $options = $type; |
||
172 | unset($columns[$column]); |
||
173 | continue; |
||
174 | } |
||
175 | if ($type instanceof ColumnSchema) { |
||
176 | $column = is_numeric($column) ? $type->name : $column; |
||
177 | $type = $this->columnSchemaToBuilder($type); |
||
178 | } |
||
179 | if ((string)$type === (string)$this->primaryKey()) { |
||
180 | $pks[] = $column; |
||
181 | } |
||
182 | if ($type instanceof ForeignKeyColumn) { |
||
183 | $type->sourceTable($table)->sourceColumn($column); |
||
184 | $fks[] = $type; |
||
185 | } |
||
186 | |||
187 | if ($type instanceof PivotColumn) { |
||
188 | $type->setName($column)->sourceTable($table); |
||
189 | $pvs[] = $type; |
||
190 | unset($columns[$column]); |
||
191 | } |
||
192 | } |
||
193 | if (count($pks) > 1) { |
||
194 | foreach ($columns as $column => &$type) { |
||
195 | $type = $this->integer(); |
||
196 | } |
||
197 | } |
||
198 | $this->db->createCommand()->createTable($table, $columns, $options)->execute(); |
||
199 | foreach ($columns as $column => $type) { |
||
200 | if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) { |
||
201 | $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute(); |
||
202 | } |
||
203 | } |
||
204 | foreach ($fks as $fk) { |
||
205 | $fk->apply(); |
||
206 | } |
||
207 | if (count($pks) > 1) { |
||
208 | $this->addPrimaryKey(null, $table, $pks); |
||
209 | } |
||
210 | foreach ($pvs as $pv) { |
||
211 | $pv->apply(); |
||
212 | } |
||
213 | echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n"; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param string $name |
||
218 | * @param string $table |
||
219 | * @param array|string $columns |
||
220 | * @param string $refTable |
||
221 | * @param array|string $refColumns |
||
222 | * @param null $delete |
||
223 | * @param null $update |
||
224 | */ |
||
225 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
226 | { |
||
227 | if (is_null($name)) { |
||
228 | $name = self::formFkName($table, $columns, $refTable, $refColumns); |
||
229 | $name = $this->expandTablePrefix($name); |
||
230 | } |
||
231 | $name = self::truncateName($name, 64, '_fk'); |
||
232 | parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | */ |
||
238 | public function alterColumn($table, $column, $type) |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | public function addColumn($table, $column, $type) |
||
253 | { |
||
254 | if ($type instanceof ForeignKeyColumn) { |
||
255 | parent::addColumn($table, $column, $type); |
||
256 | $type->sourceTable($table); |
||
257 | $type->sourceColumn($column); |
||
258 | $type->apply(); |
||
259 | } else { |
||
260 | parent::addColumn($table, $column, $type); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @inheritdoc |
||
266 | */ |
||
267 | public function addPrimaryKey($name, $table, $columns) |
||
268 | { |
||
269 | if (is_null($name)) { |
||
270 | $name = self::formIndexName($table, $columns, 'pk'); |
||
271 | $name = $this->expandTablePrefix($name); |
||
272 | } |
||
273 | $name = self::truncateName($name, 64, '_pk'); |
||
274 | parent::addPrimaryKey($name, $table, $columns); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return array |
||
279 | */ |
||
280 | public function newColumns() |
||
281 | { |
||
282 | return []; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param array $array |
||
287 | */ |
||
288 | public function downNewColumns($array = []) |
||
289 | { |
||
290 | $this->_applyNewColumns($array ?: $this->newColumns(), true); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param array $array |
||
295 | */ |
||
296 | public function upNewColumns($array = []) |
||
297 | { |
||
298 | $this->_applyNewColumns($array ?: $this->newColumns(), false); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param array $columns |
||
303 | * @param bool $revert |
||
304 | */ |
||
305 | protected function _applyNewColumns($columns = [], $revert = false) |
||
306 | { |
||
307 | $columns = $revert ? array_reverse($columns) : $columns; |
||
308 | |||
309 | $result = []; |
||
310 | foreach ($columns as $key => $column) { |
||
311 | if (is_numeric($key)) { |
||
312 | $result[] = $column; |
||
313 | } else { |
||
314 | foreach ($column as $columnName => $value) { |
||
315 | $result[] = [$key, $columnName, $value]; |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | |||
320 | foreach ($result as $column) { |
||
321 | if ($column[2] instanceof PivotColumn) { |
||
322 | $column[2]->setName($column[1])->sourceTable($column[0]); |
||
323 | } |
||
324 | if ($revert) { |
||
325 | if ($column[2] instanceof PivotColumn) { |
||
326 | $column[2]->remove(); |
||
327 | continue; |
||
328 | } |
||
329 | $this->dropColumn($column[0], $column[1], $column[2]); |
||
330 | } else { |
||
331 | if ($column[2] instanceof PivotColumn) { |
||
332 | $column[2]->apply(); |
||
333 | continue; |
||
334 | } |
||
335 | $this->addColumn($column[0], $column[1], $column[2]); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @inheritdoc |
||
342 | */ |
||
343 | public function dropColumn($table, $column, $type = null) |
||
344 | { |
||
345 | if ($type instanceof ForeignKeyColumn) { |
||
346 | $type->sourceTable($table); |
||
347 | $type->sourceColumn($column); |
||
348 | $type->remove(); |
||
349 | } |
||
350 | return parent::dropColumn($table, $column); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @return array |
||
355 | */ |
||
356 | public function newTables() |
||
357 | { |
||
358 | return []; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param array $array |
||
363 | * @param null $tableOptions |
||
364 | */ |
||
365 | public function upNewTables($array = [], $tableOptions = null) |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param array $array |
||
372 | */ |
||
373 | public function upNewIndex($array = []) |
||
374 | { |
||
375 | $this->_applyNewIndex($array ?: $this->newIndex()); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @param array $array |
||
380 | */ |
||
381 | public function downNewIndex($array = []) |
||
382 | { |
||
383 | $this->_applyNewIndex($array ?: $this->newIndex(), true); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @return array |
||
388 | */ |
||
389 | public function newIndex() |
||
390 | { |
||
391 | return []; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @param array $array |
||
396 | */ |
||
397 | public function downNewTables($array = []) |
||
398 | { |
||
399 | $this->_applyNewTables($array ?: $this->newTables(), true); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @param $indexes |
||
404 | * @param bool $revert |
||
405 | */ |
||
406 | protected function _applyNewIndex($indexes, $revert = false) |
||
407 | { |
||
408 | /** |
||
409 | * @var IndexColumn $index |
||
410 | */ |
||
411 | $indexes = $revert ? array_reverse($indexes) : $indexes; |
||
412 | foreach ($indexes as $key => $data) { |
||
413 | if (!is_numeric($key)) { |
||
414 | foreach ($data as $index) { |
||
415 | if ($index instanceof IndexColumn) { |
||
416 | $index->table($key); |
||
417 | if ($revert) { |
||
418 | $index->remove(); |
||
419 | } else { |
||
420 | $index->apply(); |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | continue; |
||
425 | } |
||
426 | |||
427 | if (!$data instanceof IndexColumn) { |
||
428 | $unq = isset($data[2]) && $data[2]; |
||
429 | $columns = is_array($data[1]) ? $data[1] : explode(',', $data[1]); |
||
430 | $index = $this->index($columns, $unq)->table($data[0]); |
||
431 | } else { |
||
432 | $index = $data; |
||
433 | } |
||
434 | |||
435 | if ($revert) { |
||
436 | $index->remove(); |
||
437 | } else { |
||
438 | $index->apply(); |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @param $tables |
||
445 | * @param bool $revert |
||
446 | * @param null $tableOptions |
||
447 | */ |
||
448 | protected function _applyNewTables($tables, $revert = false, $tableOptions = null) |
||
449 | { |
||
450 | $tables = $revert ? array_reverse($tables) : $tables; |
||
451 | foreach ($tables as $table => $columns) { |
||
452 | if ($revert) { |
||
453 | foreach ($columns as $column => $type) { |
||
454 | if ($type instanceof PivotColumn) { |
||
455 | $type->setName($column)->sourceTable($table); |
||
456 | $type->remove(); |
||
457 | } |
||
458 | } |
||
459 | $this->dropTable($table); |
||
460 | } else { |
||
461 | $tableOptions = ArrayHelper::remove($columns, 'tableOptions', $tableOptions); |
||
462 | $this->createTable($table, $columns, $tableOptions); |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @param $table |
||
469 | * @param $columns |
||
470 | * @param $refTable |
||
471 | * @param $refColumns |
||
472 | * @return string |
||
473 | */ |
||
474 | public static function formFkName($table, $columns, $refTable, $refColumns) |
||
475 | { |
||
476 | if (is_array($columns)) { |
||
477 | $column = implode(',', $columns); |
||
478 | } else { |
||
479 | $column = $columns; |
||
480 | } |
||
481 | if (is_array($refColumns)) { |
||
482 | $refColumn = implode(',', $refColumns); |
||
483 | } else { |
||
484 | $refColumn = $refColumns; |
||
485 | } |
||
486 | $table = count($t = explode('.', $table)) > 1 ? $t[1] : $t[0]; |
||
487 | $refTable = count($t = explode('.', $refTable)) > 1 ? $t[1] : $t[0]; |
||
488 | return "{$table}[{$column}]_{$refTable}[{$refColumn}]_fk"; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @param $table |
||
493 | * @param $columns |
||
494 | * @param string $suffix |
||
495 | * @return string |
||
496 | */ |
||
497 | public static function formPkIndexName($table, $columns, $suffix = 'pk') |
||
498 | { |
||
499 | return self::formIndexName($table, $columns, $suffix); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * @param $table |
||
504 | * @param $columns |
||
505 | * @param string $suffix |
||
506 | * @return string |
||
507 | */ |
||
508 | public static function formIndexName($table, $columns, $suffix = 'idx') |
||
509 | { |
||
510 | $table = self::removeSchema($table); |
||
511 | $column = implode(':', array_map('trim', (array)$columns)); |
||
512 | return "{$table}:{$column}_$suffix"; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @param $table |
||
517 | * @param $rows |
||
518 | * @param int $idStart |
||
519 | * @param string $updateSeq |
||
520 | * @deprecated |
||
521 | */ |
||
522 | public function insertTo($table, $rows, $idStart = 1, $updateSeq = 'id') |
||
523 | { |
||
524 | $c = $idStart; |
||
525 | foreach ($rows as $row) { |
||
526 | if (!isset($row['id']) && !is_null($idStart)) { |
||
527 | $row += ['id' => $c++]; |
||
528 | } |
||
529 | $this->insert($table, $row); |
||
530 | } |
||
531 | if ($updateSeq) { |
||
532 | $c = (int)\Yii::$app->db->createCommand("SELECT count(*) FROM {{$table}}")->queryScalar() + 1; |
||
533 | $this->execute("ALTER SEQUENCE {$table}_{$updateSeq}_seq RESTART WITH $c;"); |
||
534 | } |
||
535 | } |
||
536 | |||
537 | public static function removeSchema($str) |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * @param $table |
||
549 | * @param $column |
||
550 | * @return false|null|string |
||
551 | * @throws \yii\db\Exception |
||
552 | */ |
||
553 | protected function getForeignKey($table, $column) |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * @TODO https://stackoverflow.com/questions/6777456/list-all-index-names-column-names-and-its-table-name-of-a-postgresql-database |
||
565 | * @unstable |
||
566 | * |
||
567 | * @param $table |
||
568 | * @param $column |
||
569 | * @return false|null|string |
||
570 | */ |
||
571 | protected function getIndexName($table, $column) |
||
572 | { |
||
573 | $condition = [':t' => $this->expandTablePrefix($table), ':c' => $column]; |
||
574 | |||
575 | if ($this->db->driverName === 'pgsql') { |
||
576 | $sql = <<<SQL |
||
577 | SELECT |
||
578 | i.relname |
||
579 | FROM |
||
580 | pg_class T, |
||
581 | pg_class i, |
||
582 | pg_index ix, |
||
583 | pg_attribute A |
||
584 | WHERE |
||
585 | T .oid = ix.indrelid |
||
586 | AND i.oid = ix.indexrelid |
||
587 | AND A .attrelid = T .oid |
||
588 | AND A .attnum = ANY (ix.indkey) |
||
589 | AND T .relname = :t |
||
590 | AND A .attname = :c |
||
591 | SQL; |
||
592 | } else { |
||
593 | $sql = 'SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME=:t AND COLUMN_NAME=:c AND CONSTRAINT_SCHEMA=DATABASE()'; |
||
594 | } |
||
595 | return $this->db->createCommand($sql, $condition)->queryScalar(); |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * @param $table |
||
600 | * @param $column |
||
601 | */ |
||
602 | public function dropIndexByColumn($table, $column) |
||
603 | { |
||
604 | if ($key = $this->getIndexName($table, $column)) { |
||
605 | $this->dropIndex($key, $table); |
||
606 | } |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * @param $table |
||
611 | * @param $column |
||
612 | * @throws \yii\db\Exception |
||
613 | */ |
||
614 | public function dropForeignKeyByColumn($table, $column) |
||
618 | } |
||
619 | } |
||
620 | |||
621 | /** |
||
622 | * Принудительно обрезаем названия ключей, если они получаются больше чем $length, т.к. базы могут вылететь с ошибкой |
||
623 | * |
||
624 | * @see https://dev.mysql.com/doc/refman/5.7/en/identifiers.html |
||
625 | * |
||
626 | * @param $name |
||
627 | * @param int $length |
||
628 | * @param null $suffix |
||
629 | * @return string |
||
630 | */ |
||
631 | public static function truncateName($name, $length = 64, $suffix = null) |
||
632 | { |
||
633 | if (strlen($name) > $length) { |
||
634 | if (StringHelper::endsWith($name, $suffix)) { |
||
635 | $name = substr($name, 0, strlen($suffix) * -1); |
||
636 | } |
||
637 | return dechex(crc32($name)) . $suffix; |
||
638 | } |
||
639 | |||
641 | } |
||
642 | } |