Skip to content

Commit 7ba3189

Browse files
committed
MAGETWO-33564: [GitHub] Database Schema: Incorrect Unique Indexes #1002
2 parents 732d445 + 82964c2 commit 7ba3189

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
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: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
use Magento\CatalogInventory\Model\Stock\Item as StockItem;
13+
14+
class UpgradeSchema implements UpgradeSchemaInterface
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $productCompositeKeyVersion = '2.2.0';
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
25+
{
26+
$setup->startSetup();
27+
28+
if (version_compare($context->getVersion(), $this->productCompositeKeyVersion, '<')) {
29+
$this->upgradeProductCompositeKey($setup);
30+
}
31+
32+
$setup->endSetup();
33+
}
34+
35+
/**
36+
* @param SchemaSetupInterface $setup
37+
* @return void
38+
*/
39+
private function upgradeProductCompositeKey(SchemaSetupInterface $setup)
40+
{
41+
$oldCompositeKeyColumns = ['product_id', 'website_id'];
42+
$newCompositeKeyColumns = ['product_id', 'stock_id'];
43+
44+
$foreignKeys = $this->getForeignKeys($setup, $oldCompositeKeyColumns);
45+
// drop foreign keys
46+
$this->dropForeignKeys($setup, $foreignKeys);
47+
48+
$oldIndexName = $setup->getIdxName(
49+
$setup->getTable(StockItem::ENTITY),
50+
$oldCompositeKeyColumns,
51+
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
52+
);
53+
54+
$newIndexName = $setup->getIdxName(
55+
$setup->getTable(StockItem::ENTITY),
56+
$newCompositeKeyColumns,
57+
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
58+
);
59+
60+
// Drop a key based on the following columns: "product_id","website_id"
61+
$setup->getConnection()->dropIndex($setup->getTable(StockItem::ENTITY), $oldIndexName);
62+
63+
// Create a key based on the following columns: "product_id","stock_id"
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;
122+
}
123+
}

0 commit comments

Comments
 (0)