Skip to content

Commit 3b5840b

Browse files
committed
MAGETWO-66195: SQL Error during price reindexation process for custom profile
1 parent 845ddbc commit 3b5840b

File tree

3 files changed

+92
-8
lines changed
  • app/code/Magento

3 files changed

+92
-8
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
class Source extends AbstractEav
1717
{
18+
const TRANSIT_PREFIX = 'transit_';
19+
1820
/**
1921
* Catalog resource helper
2022
*
@@ -361,4 +363,45 @@ private function saveDataFromSelect(\Magento\Framework\DB\Select $select, array
361363

362364
$this->_saveIndexData($data);
363365
}
366+
367+
/**
368+
* @inheritdoc
369+
*/
370+
protected function _prepareRelationIndex($parentIds = null)
371+
{
372+
$connection = $this->getConnection();
373+
$idxTable = $this->getIdxTable();
374+
375+
if (!$this->tableStrategy->getUseIdxTable()) {
376+
$additionalIdxTable = $connection->getTableName(self::TRANSIT_PREFIX . $this->getIdxTable());
377+
$connection->createTemporaryTableLike($additionalIdxTable, $idxTable);
378+
379+
$query = $connection->insertFromSelect(
380+
$this->_prepareRelationIndexSelect($parentIds),
381+
$additionalIdxTable,
382+
[]
383+
);
384+
$connection->query($query);
385+
386+
$select = $connection->select()->from($additionalIdxTable);
387+
$query = $connection->insertFromSelect(
388+
$select,
389+
$idxTable,
390+
[],
391+
\Magento\Framework\DB\Adapter\AdapterInterface::INSERT_IGNORE
392+
);
393+
$connection->query($query);
394+
395+
$connection->dropTemporaryTable($additionalIdxTable);
396+
} else {
397+
$query = $connection->insertFromSelect(
398+
$this->_prepareRelationIndexSelect($parentIds),
399+
$idxTable,
400+
[],
401+
\Magento\Framework\DB\Adapter\AdapterInterface::INSERT_IGNORE
402+
);
403+
$connection->query($query);
404+
}
405+
return $this;
406+
}
364407
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,4 +954,10 @@
954954
<argument name="connectionName" xsi:type="string">indexer</argument>
955955
</arguments>
956956
</type>
957+
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\Source">
958+
<arguments>
959+
<argument name="tableStrategy" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\TemporaryTableStrategy</argument>
960+
<argument name="connectionName" xsi:type="string">indexer</argument>
961+
</arguments>
962+
</type>
957963
</config>

app/code/Magento/GroupedProduct/Model/ResourceModel/Product/Indexer/Price/Grouped.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
class Grouped extends DefaultPrice implements GroupedInterface
1414
{
15+
const TRANSIT_PREFIX = 'transit_';
16+
1517
/**
16-
* {@inheritdoc}
17-
* @param null|array $entityIds
18+
* @inheritdoc
1819
*/
1920
protected function reindex($entityIds = null)
2021
{
@@ -26,13 +27,51 @@ protected function reindex($entityIds = null)
2627
* Use calculated price for relation products
2728
*
2829
* @param int|array $entityIds the parent entity ids limitation
29-
* @return \Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped
30+
* @return $this
3031
*/
3132
protected function _prepareGroupedProductPriceData($entityIds = null)
3233
{
3334
if (!$this->hasEntity() && empty($entityIds)) {
3435
return $this;
3536
}
37+
38+
$connection = $this->getConnection();
39+
$table = $this->getIdxTable();
40+
41+
if (!$this->tableStrategy->getUseIdxTable()) {
42+
$additionalIdxTable = $connection->getTableName(self::TRANSIT_PREFIX . $this->getIdxTable());
43+
$connection->createTemporaryTableLike($additionalIdxTable, $table);
44+
$query = $connection->insertFromSelect(
45+
$this->_prepareGroupedProductPriceDataSelect($entityIds),
46+
$additionalIdxTable,
47+
[]
48+
);
49+
$connection->query($query);
50+
51+
$select = $connection->select()->from($additionalIdxTable);
52+
$query = $connection->insertFromSelect(
53+
$select,
54+
$table,
55+
[],
56+
\Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE
57+
);
58+
$connection->query($query);
59+
$connection->dropTemporaryTable($additionalIdxTable);
60+
} else {
61+
$query = $this->_prepareGroupedProductPriceDataSelect($entityIds)->insertFromSelect($table);
62+
$connection->query($query);
63+
}
64+
return $this;
65+
}
66+
67+
/**
68+
* Prepare data index select for Grouped products prices
69+
*
70+
* @param int|array $entityIds the parent entity ids limitation
71+
* @return \Magento\Framework\DB\Select
72+
*/
73+
protected function _prepareGroupedProductPriceDataSelect($entityIds = null)
74+
{
3675
$connection = $this->getConnection();
3776
$table = $this->getIdxTable();
3877
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
@@ -99,10 +138,6 @@ protected function _prepareGroupedProductPriceData($entityIds = null)
99138
'store_field' => new \Zend_Db_Expr('cs.store_id')
100139
]
101140
);
102-
103-
$query = $select->insertFromSelect($table);
104-
$connection->query($query);
105-
106-
return $this;
141+
return $select;
107142
}
108143
}

0 commit comments

Comments
 (0)