Skip to content

Commit c96d788

Browse files
committed
MAGETWO-33564: [GitHub] Database Schema: Incorrect Unique Indexes #1002
1 parent 18c8f6c commit c96d788

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

app/code/Magento/CatalogInventory/Setup/InstallSchema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
254254
->addIndex(
255255
$installer->getIdxName(
256256
'cataloginventory_stock_item',
257-
['product_id', 'website_id'],
257+
['product_id', 'stock_id'],
258258
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
259259
),
260-
['product_id', 'website_id'],
260+
['product_id', 'stock_id'],
261261
['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
262262
)
263263
->addIndex(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

app/code/Magento/CatalogInventory/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_CatalogInventory" setup_version="2.0.2">
9+
<module name="Magento_CatalogInventory" setup_version="2.2.0">
1010
<sequence>
1111
<module name="Magento_Catalog"/>
1212
</sequence>

0 commit comments

Comments
 (0)