Skip to content

Commit 82964c2

Browse files
committed
MAGETWO-33564: [GitHub] Database Schema: Incorrect Unique Indexes #1002
1 parent 349a112 commit 82964c2

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

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

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Setup\UpgradeSchemaInterface;
1010
use Magento\Framework\Setup\ModuleContextInterface;
1111
use Magento\Framework\Setup\SchemaSetupInterface;
12+
use Magento\CatalogInventory\Model\Stock\Item as StockItem;
1213

1314
class UpgradeSchema implements UpgradeSchemaInterface
1415
{
@@ -40,30 +41,83 @@ private function upgradeProductCompositeKey(SchemaSetupInterface $setup)
4041
$oldCompositeKeyColumns = ['product_id', 'website_id'];
4142
$newCompositeKeyColumns = ['product_id', 'stock_id'];
4243

44+
$foreignKeys = $this->getForeignKeys($setup, $oldCompositeKeyColumns);
45+
// drop foreign keys
46+
$this->dropForeignKeys($setup, $foreignKeys);
47+
4348
$oldIndexName = $setup->getIdxName(
44-
\Magento\CatalogInventory\Model\Stock\Item::ENTITY,
49+
$setup->getTable(StockItem::ENTITY),
4550
$oldCompositeKeyColumns,
4651
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
4752
);
4853

4954
$newIndexName = $setup->getIdxName(
50-
\Magento\CatalogInventory\Model\Stock\Item::ENTITY,
55+
$setup->getTable(StockItem::ENTITY),
5156
$newCompositeKeyColumns,
5257
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
5358
);
5459

5560
// 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);
6062

6163
// 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;
68122
}
69123
}

0 commit comments

Comments
 (0)