carono /
yii2-migrate
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace carono\yii2migrate\helpers; |
||||
| 4 | |||||
| 5 | use yii\db\Connection; |
||||
| 6 | use yii\helpers\ArrayHelper; |
||||
| 7 | use yii\helpers\StringHelper; |
||||
| 8 | |||||
| 9 | class SchemaHelper |
||||
| 10 | { |
||||
| 11 | /** |
||||
| 12 | * @param Connection $db |
||||
| 13 | * @param $table |
||||
| 14 | * @return \yii\db\IndexConstraint[] |
||||
| 15 | */ |
||||
| 16 | 6 | public static function findNonUniqueIndexes($db, $table) |
|||
| 17 | { |
||||
| 18 | 6 | $indexes = self::findIndexes($db, $table); |
|||
| 19 | $indexes = array_filter($indexes, function ($index) { |
||||
| 20 | 6 | return !$index->isUnique; |
|||
| 21 | 6 | }); |
|||
| 22 | 6 | return ArrayHelper::index($indexes, 'name'); |
|||
| 23 | } |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @param Connection $db |
||||
| 27 | * @param $table |
||||
| 28 | * @return \yii\db\IndexConstraint[] |
||||
| 29 | */ |
||||
| 30 | 2 | public static function findUniqueIndexes($db, $table) |
|||
| 31 | { |
||||
| 32 | 2 | $indexes = self::findIndexes($db, $table); |
|||
| 33 | $indexes = array_filter($indexes, function ($index) { |
||||
| 34 | 2 | return $index->isUnique; |
|||
| 35 | 2 | }); |
|||
| 36 | 2 | return ArrayHelper::index($indexes, 'name'); |
|||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * @param Connection $db |
||||
| 41 | * @param $table |
||||
| 42 | * @return \yii\db\IndexConstraint[] |
||||
| 43 | */ |
||||
| 44 | 8 | public static function findIndexes($db, $table) |
|||
| 45 | { |
||||
| 46 | 8 | $reflectionMethod = new \ReflectionMethod(get_class($db->schema), 'loadTableIndexes'); |
|||
| 47 | 8 | $reflectionMethod->setAccessible(true); |
|||
| 48 | 8 | $indexes = $reflectionMethod->invoke($db->schema, self::expandTablePrefix($table, $db->tablePrefix)); |
|||
| 49 | 8 | return ArrayHelper::index($indexes, 'name'); |
|||
| 50 | } |
||||
| 51 | |||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * @param Connection $db |
||||
| 55 | * @param $table |
||||
| 56 | * @return \yii\db\ForeignKeyConstraint[] |
||||
| 57 | */ |
||||
| 58 | 21 | public static function findTableForeignKeys($db, $table) |
|||
| 59 | { |
||||
| 60 | 21 | $reflectionMethod = new \ReflectionMethod(get_class($db->schema), 'loadTableForeignKeys'); |
|||
| 61 | 21 | $reflectionMethod->setAccessible(true); |
|||
| 62 | 21 | return ArrayHelper::index($reflectionMethod->invoke($db->schema, self::expandTablePrefix($table, $db->tablePrefix)), 'name'); |
|||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * @param $str |
||||
| 67 | * @return mixed |
||||
| 68 | */ |
||||
| 69 | 53 | public static function removeSchema($str) |
|||
| 70 | { |
||||
| 71 | 53 | if (strpos($str, '.') !== false) { |
|||
| 72 | 1 | $arr = explode('.', $str); |
|||
| 73 | 1 | return $arr[1]; |
|||
| 74 | } |
||||
| 75 | |||||
| 76 | 52 | return $str; |
|||
| 77 | } |
||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * {{%table}} to pfx_table |
||||
| 81 | * |
||||
| 82 | * @param $name |
||||
| 83 | * @param $prefix |
||||
| 84 | * @return null|string|string[] |
||||
| 85 | */ |
||||
| 86 | 85 | public static function expandTablePrefix($name, $prefix) |
|||
| 87 | { |
||||
| 88 | 85 | return preg_replace('/{{%([\w\-_]+)}}/', addcslashes($prefix, "\\") . '$1', $name); |
|||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * pfx_table to {{%table}} |
||||
| 93 | * |
||||
| 94 | * @param $name |
||||
| 95 | * @param $prefix |
||||
| 96 | * @return null|string|string[] |
||||
| 97 | */ |
||||
| 98 | 1 | public static function collapseTablePrefix($name, $prefix) |
|||
| 99 | { |
||||
| 100 | 1 | $prefix = addcslashes($prefix, "\\/"); |
|||
| 101 | 1 | return preg_replace("/^$prefix(.+)$/m", '{{%$1}}', $name); |
|||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Принудительно обрезаем названия ключей, если они получаются больше чем $length, т.к. базы могут вылететь с ошибкой |
||||
| 106 | * |
||||
| 107 | * @see https://dev.mysql.com/doc/refman/5.7/en/identifiers.html |
||||
| 108 | * |
||||
| 109 | * @param $name |
||||
| 110 | * @param int $length |
||||
| 111 | * @param null $suffix |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 112 | * @return string |
||||
| 113 | */ |
||||
| 114 | 46 | public static function truncateIndexName($name, $length = 64, $suffix = null) |
|||
| 115 | { |
||||
| 116 | 46 | if (strlen($name) > $length) { |
|||
| 117 | 1 | if (StringHelper::endsWith($name, $suffix)) { |
|||
| 118 | 1 | $name = substr($name, 0, strlen($suffix) * -1); |
|||
|
0 ignored issues
–
show
$suffix of type null is incompatible with the type string expected by parameter $string of strlen().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 119 | } |
||||
| 120 | 1 | return dechex(crc32($name)) . $suffix; |
|||
| 121 | } |
||||
| 122 | |||||
| 123 | 46 | return $name; |
|||
| 124 | } |
||||
| 125 | } |