|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Setup; |
| 7 | + |
| 8 | +/** |
| 9 | + * @codeCoverageIgnore |
| 10 | + */ |
| 11 | +class ExternalFKSetup |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var SchemaSetupInterface |
| 15 | + */ |
| 16 | + protected $setup; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $entityTable; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $entityColumn; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $externalTable; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $externalColumn; |
| 37 | + |
| 38 | + /** |
| 39 | + * Install external foreign key |
| 40 | + * |
| 41 | + * @param SchemaSetupInterface $setup |
| 42 | + * @param string $entityTable |
| 43 | + * @param string $entityColumn |
| 44 | + * @param string $externalTable |
| 45 | + * @param string $externalColumn |
| 46 | + */ |
| 47 | + public function install( |
| 48 | + SchemaSetupInterface $setup, |
| 49 | + $entityTable, |
| 50 | + $entityColumn, |
| 51 | + $externalTable, |
| 52 | + $externalColumn |
| 53 | + ) { |
| 54 | + $this->setup = $setup; |
| 55 | + $this->entityTable = $entityTable; |
| 56 | + $this->entityColumn = $entityColumn; |
| 57 | + $this->externalTable = $externalTable; |
| 58 | + $this->externalColumn = $externalColumn; |
| 59 | + |
| 60 | + $this->execute(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set external foreign key |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + protected function execute() |
| 69 | + { |
| 70 | + $entityTableInfo = $this->setup->getConnection()->describeTable( |
| 71 | + $this->setup->getTable($this->entityTable) |
| 72 | + ); |
| 73 | + if (!$entityTableInfo[$this->entityColumn]['PRIMARY']) { |
| 74 | + $this->dropOldForeignKey(); |
| 75 | + $this->addForeignKeys(); |
| 76 | + } else { |
| 77 | + $this->addDefaultForeignKey(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get foreign keys for tables and columns |
| 83 | + * |
| 84 | + * @param string $refTable |
| 85 | + * @param string $refColumn |
| 86 | + * @param string $targetTable |
| 87 | + * @param string $targetColumn |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + protected function getForeignKeys( |
| 91 | + $targetTable, |
| 92 | + $targetColumn, |
| 93 | + $refTable, |
| 94 | + $refColumn |
| 95 | + ) { |
| 96 | + $foreignKeys = $this->setup->getConnection()->getForeignKeys( |
| 97 | + $this->setup->getTable($targetTable) |
| 98 | + ); |
| 99 | + $foreignKeys = array_filter( |
| 100 | + $foreignKeys, |
| 101 | + function ($key) use ($targetColumn, $refTable, $refColumn) { |
| 102 | + return $key['COLUMN_NAME'] == $targetColumn |
| 103 | + && $key['REF_TABLE_NAME'] == $refTable |
| 104 | + && $key['REF_COLUMN_NAME'] == $refColumn; |
| 105 | + } |
| 106 | + ); |
| 107 | + return $foreignKeys; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Remove foreign key if exists |
| 112 | + * |
| 113 | + * @param string $targetTable |
| 114 | + * @param string $targetColumn |
| 115 | + * @param string $refTable |
| 116 | + * @param string $refColumn |
| 117 | + * @return void |
| 118 | + */ |
| 119 | + protected function clearForeignKey( |
| 120 | + $targetTable, |
| 121 | + $targetColumn, |
| 122 | + $refTable, |
| 123 | + $refColumn |
| 124 | + ) { |
| 125 | + $foreignKeys = $this->getForeignKeys($targetTable, $targetColumn, $refTable, $refColumn); |
| 126 | + foreach ($foreignKeys as $foreignKey) { |
| 127 | + $this->setup->getConnection()->dropForeignKey( |
| 128 | + $foreignKey['TABLE_NAME'], |
| 129 | + $foreignKey['FK_NAME'] |
| 130 | + ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Add default foreign key |
| 136 | + * |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + protected function addDefaultForeignKey() |
| 140 | + { |
| 141 | + if (!count($this->getForeignKeys( |
| 142 | + $this->externalTable, |
| 143 | + $this->externalColumn, |
| 144 | + $this->entityTable, |
| 145 | + $this->entityColumn |
| 146 | + ))) { |
| 147 | + $this->setup->getConnection()->addForeignKey( |
| 148 | + $this->setup->getFkName( |
| 149 | + $this->externalTable, |
| 150 | + $this->externalColumn, |
| 151 | + $this->entityTable, |
| 152 | + $this->entityColumn |
| 153 | + ), |
| 154 | + $this->setup->getTable($this->externalTable), |
| 155 | + $this->externalColumn, |
| 156 | + $this->setup->getTable($this->entityTable), |
| 157 | + $this->entityColumn |
| 158 | + ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Add foreign keys to entity table |
| 164 | + * |
| 165 | + * @return void |
| 166 | + */ |
| 167 | + protected function addForeignKeys() |
| 168 | + { |
| 169 | + $foreignKeys = $this->setup->getConnection()->getForeignKeys( |
| 170 | + $this->setup->getTable($this->entityTable) |
| 171 | + ); |
| 172 | + $foreignKeys = array_filter( |
| 173 | + $foreignKeys, |
| 174 | + function ($key) { |
| 175 | + return $key['COLUMN_NAME'] == $this->entityColumn; |
| 176 | + } |
| 177 | + ); |
| 178 | + foreach ($foreignKeys as $foreignKeyInfo) { |
| 179 | + if (!count($this->getForeignKeys( |
| 180 | + $this->externalTable, |
| 181 | + $this->externalColumn, |
| 182 | + $this->setup->getTablePlaceholder($foreignKeyInfo['REF_TABLE_NAME']), |
| 183 | + $foreignKeyInfo['REF_COLUMN_NAME'] |
| 184 | + ))) { |
| 185 | + $this->setup->getConnection()->addForeignKey( |
| 186 | + $this->setup->getFkName( |
| 187 | + $this->externalTable, |
| 188 | + $this->externalColumn, |
| 189 | + $this->setup->getTablePlaceholder($foreignKeyInfo['REF_TABLE_NAME']), |
| 190 | + $foreignKeyInfo['REF_COLUMN_NAME'] |
| 191 | + ), |
| 192 | + $this->setup->getTable($this->externalTable), |
| 193 | + $this->externalColumn, |
| 194 | + $foreignKeyInfo['REF_TABLE_NAME'], |
| 195 | + $foreignKeyInfo['REF_COLUMN_NAME'] |
| 196 | + ); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Drop old foreign key |
| 203 | + * |
| 204 | + * @return void |
| 205 | + */ |
| 206 | + protected function dropOldForeignKey() |
| 207 | + { |
| 208 | + if (count($this->getForeignKeys( |
| 209 | + $this->externalTable, |
| 210 | + $this->externalColumn, |
| 211 | + $this->entityTable, |
| 212 | + $this->entityColumn |
| 213 | + ))) { |
| 214 | + $this->clearForeignKey( |
| 215 | + $this->externalTable, |
| 216 | + $this->externalColumn, |
| 217 | + $this->entityTable, |
| 218 | + $this->entityColumn |
| 219 | + ); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments