|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogInventory\Setup; |
| 8 | + |
| 9 | +use Magento\Framework\Setup\UpgradeSchemaInterface; |
| 10 | +use Magento\Framework\Setup\ModuleContextInterface; |
| 11 | +use Magento\Framework\Setup\SchemaSetupInterface; |
| 12 | + |
| 13 | +class UpgradeSchema implements UpgradeSchemaInterface |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + private $productCompositeKeyVersion = '2.2.0'; |
| 19 | + |
| 20 | + /** |
| 21 | + * {@inheritdoc} |
| 22 | + */ |
| 23 | + public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) |
| 24 | + { |
| 25 | + $setup->startSetup(); |
| 26 | + |
| 27 | + if (version_compare($context->getVersion(), $this->productCompositeKeyVersion, '<')) { |
| 28 | + $this->upgradeProductCompositeKey($setup); |
| 29 | + } |
| 30 | + |
| 31 | + $setup->endSetup(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param SchemaSetupInterface $setup |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + private function upgradeProductCompositeKey(SchemaSetupInterface $setup) |
| 39 | + { |
| 40 | + $oldCompositeKeyColumns = ['product_id', 'website_id']; |
| 41 | + $newCompositeKeyColumns = ['product_id', 'stock_id']; |
| 42 | + |
| 43 | + $oldIndexName = $setup->getIdxName( |
| 44 | + \Magento\CatalogInventory\Model\Stock\Item::ENTITY, |
| 45 | + $oldCompositeKeyColumns, |
| 46 | + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE |
| 47 | + ); |
| 48 | + |
| 49 | + $newIndexName = $setup->getIdxName( |
| 50 | + \Magento\CatalogInventory\Model\Stock\Item::ENTITY, |
| 51 | + $newCompositeKeyColumns, |
| 52 | + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE |
| 53 | + ); |
| 54 | + |
| 55 | + // 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 | + ); |
| 60 | + |
| 61 | + // 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 | + ); |
| 68 | + } |
| 69 | +} |
0 commit comments