Skip to content

Commit 0427fd7

Browse files
author
Stanislav Idolov
committed
MAGETWO-69261: Magento\Framework\App\ResourceConnection::getTableName does not support index table name switching
1 parent aeb9638 commit 0427fd7

File tree

11 files changed

+126
-22
lines changed

11 files changed

+126
-22
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Bundle\Model\ResourceModel\Indexer;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9-
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
109
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
10+
use Magento\Framework\App\ObjectManager;
1111

1212
/**
1313
* Bundle Stock Status Indexer Resource Model
@@ -16,6 +16,35 @@
1616
*/
1717
class Stock extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock
1818
{
19+
/**
20+
* @var \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher
21+
*/
22+
private $activeTableSwitcher;
23+
24+
/**
25+
* Stock constructor.
26+
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
27+
* @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy
28+
* @param \Magento\Eav\Model\Config $eavConfig
29+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
30+
* @param null $connectionName
31+
* @param \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher|null $activeTableSwitcher
32+
*/
33+
public function __construct(
34+
\Magento\Framework\Model\ResourceModel\Db\Context $context,
35+
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
36+
\Magento\Eav\Model\Config $eavConfig,
37+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
38+
$connectionName = null,
39+
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null
40+
) {
41+
parent::__construct($context, $tableStrategy, $eavConfig, $scopeConfig, $connectionName);
42+
$this->activeTableSwitcher = $activeTableSwitcher ?: ObjectManager::getInstance()->get(
43+
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::class
44+
);
45+
}
46+
47+
1948
/**
2049
* Retrieve table name for temporary bundle option stock index
2150
*
@@ -38,7 +67,7 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
3867
$this->_cleanBundleOptionStockData();
3968
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
4069
$table = $this->getActionType() === Full::ACTION_TYPE
41-
? $this->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX
70+
? $this->activeTableSwitcher->getAdditionalTableName($this->getMainTable())
4271
: $this->getMainTable();
4372
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
4473
$connection = $this->getConnection();

app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function publishData()
144144
{
145145
$select = $this->connection->select()->from($this->getMainTmpTable());
146146
$columns = array_keys($this->connection->describeTable($this->getMainTable()));
147-
$tableName = $this->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX;
147+
$tableName = $this->activeTableSwitcher->getAdditionalTableName($this->getMainTable());
148148

149149
$this->connection->query(
150150
$this->connection->insertFromSelect(

app/code/Magento/Catalog/Model/Indexer/Product/Eav/Action/Full.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
4747
\Magento\Framework\Indexer\BatchProviderInterface $batchProvider = null,
4848
\Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\BatchSizeCalculator $batchSizeCalculator = null,
49-
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null
49+
ActiveTableSwitcher $activeTableSwitcher = null
5050
) {
5151
parent::__construct($eavDecimalFactory, $eavSourceFactory);
5252
$this->metadataPool = $metadataPool ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
@@ -59,7 +59,7 @@ public function __construct(
5959
\Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\BatchSizeCalculator::class
6060
);
6161
$this->activeTableSwitcher = $activeTableSwitcher ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
62-
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::class
62+
ActiveTableSwitcher::class
6363
);
6464
}
6565

@@ -76,7 +76,7 @@ public function execute($ids = null)
7676
try {
7777
foreach ($this->getIndexers() as $indexerName => $indexer) {
7878
$connection = $indexer->getConnection();
79-
$mainTable = $indexer->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX;
79+
$mainTable = $this->activeTableSwitcher->getAdditionalTableName($indexer->getMainTable());
8080
$connection->truncateTable($mainTable);
8181
$entityMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
8282
$batches = $this->batchProvider->getBatches(

app/code/Magento/Catalog/Model/Indexer/Product/Price/Action/Full.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Price\Action;
77

8-
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
98
use Magento\Framework\App\ObjectManager;
109

1110
/**
@@ -30,7 +29,7 @@ class Full extends \Magento\Catalog\Model\Indexer\Product\Price\AbstractAction
3029
private $batchProvider;
3130

3231
/**
33-
* @var ActiveTableSwitcher
32+
* @var \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher
3433
*/
3534
private $activeTableSwitcher;
3635

@@ -46,7 +45,7 @@ class Full extends \Magento\Catalog\Model\Indexer\Product\Price\AbstractAction
4645
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
4746
* @param \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator|null $batchSizeCalculator
4847
* @param \Magento\Framework\Indexer\BatchProviderInterface|null $batchProvider
49-
* @param ActiveTableSwitcher|null $activeTableSwitcher
48+
* @param \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher|null $activeTableSwitcher
5049
*
5150
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5251
*/
@@ -103,8 +102,9 @@ public function execute($ids = null)
103102
$this->_prepareWebsiteDateTable();
104103

105104
$entityMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
106-
$replicaTable = $this->_defaultIndexerResource->getMainTable()
107-
. ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX;
105+
$replicaTable = $this->activeTableSwitcher->getAdditionalTableName(
106+
$this->_defaultIndexerResource->getMainTable()
107+
);
108108

109109
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer $indexer */
110110
foreach ($this->getTypeIndexers() as $indexer) {
@@ -162,6 +162,6 @@ public function execute($ids = null)
162162
*/
163163
protected function getIndexTargetTable()
164164
{
165-
return $this->_defaultIndexerResource->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX;
165+
return $this->activeTableSwitcher->getAdditionalTableName($this->_defaultIndexerResource->getMainTable());
166166
}
167167
}

app/code/Magento/Catalog/Model/ResourceModel/Indexer/ActiveTableSwitcher.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Catalog\Model\ResourceModel\Indexer;
77

88
/**
9-
* Logic for switching active and replica index tables by mysql rename operation.
9+
* Logic for switching active and replica index tables.
1010
*/
1111
class ActiveTableSwitcher
1212
{
@@ -45,4 +45,13 @@ public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $conn
4545
]
4646
);
4747
}
48+
49+
/**
50+
* @param string $tableName
51+
* @return string
52+
*/
53+
public function getAdditionalTableName($tableName)
54+
{
55+
return $tableName . self::ADDITIONAL_TABLE_SUFFIX;
56+
}
4857
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ private function addReplicaTable(SchemaSetupInterface $setup, $existingTable, $r
582582
{
583583
$sql = sprintf(
584584
'CREATE TABLE IF NOT EXISTS %s LIKE %s',
585-
$setup->getTable($replicaTable),
586-
$setup->getTable($existingTable)
585+
$setup->getConnection()->quoteIdentifier($setup->getTable($replicaTable)),
586+
$setup->getConnection()->quoteIdentifier($setup->getTable($existingTable))
587587
);
588588
$setup->getConnection()->query($sql);
589589
}

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Indexer/ActiveTableSwitcherTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ public function testSwitch()
4444

4545
$this->model->switchTable($connectionMock, $tableName);
4646
}
47+
48+
public function testGetAdditionalTableName()
49+
{
50+
$tableName = 'table_name';
51+
$this->assertEquals(
52+
$tableName . \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX,
53+
$this->model->getAdditionalTableName($tableName)
54+
);
55+
}
4756
}

app/code/Magento/CatalogInventory/Model/Indexer/Stock/Action/Full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function execute($ids = null)
125125
foreach ($this->_getTypeIndexers() as $indexer) {
126126
$indexer->setActionType(self::ACTION_TYPE);
127127
$connection = $indexer->getConnection();
128-
$tableName = $indexer->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX;
128+
$tableName = $this->activeTableSwitcher->getAdditionalTableName($indexer->getMainTable());
129129

130130
$batchRowCount = isset($this->batchRowsCount[$indexer->getTypeId()])
131131
? $this->batchRowsCount[$indexer->getTypeId()]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ private function addReplicaTable(SchemaSetupInterface $setup, $existingTable, $r
159159
{
160160
$sql = sprintf(
161161
'CREATE TABLE IF NOT EXISTS %s LIKE %s',
162-
$setup->getTable($replicaTable),
163-
$setup->getTable($existingTable)
162+
$setup->getConnection()->quoteIdentifier($setup->getTable($replicaTable)),
163+
$setup->getConnection()->quoteIdentifier($setup->getTable($existingTable))
164164
);
165165
$setup->getConnection()->query($sql);
166166
}

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* @author Magento Core Team <core@magentocommerce.com>
1414
*/
1515
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
16-
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
1716
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
17+
use Magento\Framework\App\ObjectManager;
1818

1919
/**
2020
* Stock indexer for configurable product.
@@ -23,6 +23,34 @@
2323
*/
2424
class Configurable extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock
2525
{
26+
/**
27+
* @var \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher
28+
*/
29+
private $activeTableSwitcher;
30+
31+
/**
32+
* Configurable constructor.
33+
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
34+
* @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy
35+
* @param \Magento\Eav\Model\Config $eavConfig
36+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
37+
* @param null $connectionName
38+
* @param \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher|null $activeTableSwitcher
39+
*/
40+
public function __construct(
41+
\Magento\Framework\Model\ResourceModel\Db\Context $context,
42+
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
43+
\Magento\Eav\Model\Config $eavConfig,
44+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
45+
$connectionName = null,
46+
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null
47+
) {
48+
parent::__construct($context, $tableStrategy, $eavConfig, $scopeConfig, $connectionName);
49+
$this->activeTableSwitcher = $activeTableSwitcher ?: ObjectManager::getInstance()->get(
50+
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher::class
51+
);
52+
}
53+
2654
/**
2755
* Get the select object for get stock status by configurable product ids
2856
*
@@ -35,7 +63,7 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
3563
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
3664
$connection = $this->getConnection();
3765
$table = $this->getActionType() === Full::ACTION_TYPE
38-
? $this->getMainTable() . ActiveTableSwitcher::ADDITIONAL_TABLE_SUFFIX
66+
? $this->activeTableSwitcher->getAdditionalTableName($this->getMainTable())
3967
: $this->getMainTable();
4068
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
4169
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);

0 commit comments

Comments
 (0)