|
9 | 9 | use Magento\Framework\Setup\UpgradeSchemaInterface;
|
10 | 10 | use Magento\Framework\Setup\ModuleContextInterface;
|
11 | 11 | use Magento\Framework\Setup\SchemaSetupInterface;
|
| 12 | +use Magento\CatalogInventory\Model\Stock\Item as StockItem; |
12 | 13 |
|
13 | 14 | class UpgradeSchema implements UpgradeSchemaInterface
|
14 | 15 | {
|
@@ -40,30 +41,83 @@ private function upgradeProductCompositeKey(SchemaSetupInterface $setup)
|
40 | 41 | $oldCompositeKeyColumns = ['product_id', 'website_id'];
|
41 | 42 | $newCompositeKeyColumns = ['product_id', 'stock_id'];
|
42 | 43 |
|
| 44 | + $foreignKeys = $this->getForeignKeys($setup, $oldCompositeKeyColumns); |
| 45 | + // drop foreign keys |
| 46 | + $this->dropForeignKeys($setup, $foreignKeys); |
| 47 | + |
43 | 48 | $oldIndexName = $setup->getIdxName(
|
44 |
| - \Magento\CatalogInventory\Model\Stock\Item::ENTITY, |
| 49 | + $setup->getTable(StockItem::ENTITY), |
45 | 50 | $oldCompositeKeyColumns,
|
46 | 51 | \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
|
47 | 52 | );
|
48 | 53 |
|
49 | 54 | $newIndexName = $setup->getIdxName(
|
50 |
| - \Magento\CatalogInventory\Model\Stock\Item::ENTITY, |
| 55 | + $setup->getTable(StockItem::ENTITY), |
51 | 56 | $newCompositeKeyColumns,
|
52 | 57 | \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
|
53 | 58 | );
|
54 | 59 |
|
55 | 60 | // Drop a key based on the following columns: "product_id","website_id"
|
56 |
| - $setup->getConnection()->dropIndex( |
57 |
| - $setup->getTable(\Magento\CatalogInventory\Model\Stock\Item::ENTITY), |
58 |
| - $oldIndexName |
59 |
| - ); |
| 61 | + $setup->getConnection()->dropIndex($setup->getTable(StockItem::ENTITY), $oldIndexName); |
60 | 62 |
|
61 | 63 | // Create a key based on the following columns: "product_id","stock_id"
|
62 |
| - $setup->getConnection()->addIndex( |
63 |
| - \Magento\CatalogInventory\Model\Stock\Item::ENTITY, |
64 |
| - $newIndexName, |
65 |
| - $newCompositeKeyColumns, |
66 |
| - \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE |
67 |
| - ); |
| 64 | + $setup->getConnection() |
| 65 | + ->addIndex( |
| 66 | + $setup->getTable(StockItem::ENTITY), |
| 67 | + $newIndexName, |
| 68 | + $newCompositeKeyColumns, |
| 69 | + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE |
| 70 | + ); |
| 71 | + // restore deleted foreign keys |
| 72 | + $this->createForeignKeys($setup, $foreignKeys); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param SchemaSetupInterface $setup |
| 77 | + * @param array $keys |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + private function dropForeignKeys(SchemaSetupInterface $setup, array $keys) |
| 81 | + { |
| 82 | + foreach ($keys as $key) { |
| 83 | + $setup->getConnection()->dropForeignKey($key['TABLE_NAME'], $key['FK_NAME']); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param SchemaSetupInterface $setup |
| 89 | + * @param array $keys |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + private function createForeignKeys(SchemaSetupInterface $setup, array $keys) |
| 93 | + { |
| 94 | + foreach ($keys as $key) { |
| 95 | + $setup->getConnection()->addForeignKey( |
| 96 | + $key['FK_NAME'], |
| 97 | + $key['TABLE_NAME'], |
| 98 | + $key['COLUMN_NAME'], |
| 99 | + $key['REF_TABLE_NAME'], |
| 100 | + $key['REF_COLUMN_NAME'], |
| 101 | + $key['ON_DELETE'] |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param SchemaSetupInterface $setup |
| 108 | + * @param array $compositeKeys |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + private function getForeignKeys(SchemaSetupInterface $setup, array $compositeKeys) |
| 112 | + { |
| 113 | + $foreignKeys = []; |
| 114 | + $allForeignKeys = $setup->getConnection()->getForeignKeys($setup->getTable(StockItem::ENTITY)); |
| 115 | + foreach ($allForeignKeys as $key) { |
| 116 | + if (in_array($key['COLUMN_NAME'], $compositeKeys)) { |
| 117 | + $foreignKeys[] = $key; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return $foreignKeys; |
68 | 122 | }
|
69 | 123 | }
|
0 commit comments