Skip to content

Commit 1f05fd8

Browse files
committed
MAGETWO-95445: Category Flat Data indexer doesnt show status as reindex required after deleting store/storeview
- Delete abandoned tables in full reindex
1 parent 8e2b6f9 commit 1f05fd8

File tree

2 files changed

+62
-1
lines changed
  • app/code/Magento/Catalog

2 files changed

+62
-1
lines changed

app/code/Magento/Catalog/Helper/Product/Flat/Indexer.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,17 @@ public function getFlatTableName($storeId)
466466
return sprintf('%s_%s', $this->getTable('catalog_product_flat'), $storeId);
467467
}
468468

469+
/**
470+
* Retrieve Catalog Product Flat Table name
471+
*
472+
* @param int $storeId
473+
* @return string
474+
*/
475+
private function getCategoryFlatTableName(int $storeId): string
476+
{
477+
return sprintf('%s_store_%s', $this->getTable('catalog_category_flat'), $storeId);
478+
}
479+
469480
/**
470481
* Retrieve loaded attribute by code
471482
*
@@ -515,4 +526,25 @@ public function deleteAbandonedStoreFlatTables()
515526
$connection->dropTable($table);
516527
}
517528
}
529+
530+
/**
531+
* Delete all category flat tables for not existing stores
532+
*
533+
* @return void
534+
*/
535+
public function deleteAbandonedStoreCategoryFlatTables()
536+
{
537+
$connection = $this->_resource->getConnection();
538+
$existentTables = $connection->getTables($connection->getTableName('catalog_category_flat_store_%'));
539+
$actualStoreTables = [];
540+
foreach ($this->_storeManager->getStores() as $store) {
541+
$actualStoreTables[] = $this->getCategoryFlatTableName($store->getId());
542+
}
543+
544+
$tablesToDelete = array_diff($existentTables, $actualStoreTables);
545+
546+
foreach ($tablesToDelete as $table) {
547+
$connection->dropTable($table);
548+
}
549+
}
518550
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Category\Flat\Action;
77

8+
use Magento\Framework\App\ResourceConnection;
9+
10+
/**
11+
* Class for full reindex flat categories
12+
*/
813
class Full extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
914
{
1015
/**
@@ -19,6 +24,28 @@ class Full extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
1924
*/
2025
protected $allowTableChanges = true;
2126

27+
/**
28+
* @var \Magento\Catalog\Helper\Product\Flat\Indexer
29+
*/
30+
private $indexer;
31+
32+
/**
33+
* @param ResourceConnection $resource
34+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
35+
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
36+
* @param \Magento\Catalog\Helper\Product\Flat\Indexer $indexer
37+
*/
38+
public function __construct(
39+
ResourceConnection $resource,
40+
\Magento\Store\Model\StoreManagerInterface $storeManager,
41+
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
42+
\Magento\Catalog\Helper\Product\Flat\Indexer $indexer = null
43+
) {
44+
$this->indexer = $indexer ?: \Magento\Framework\App\ObjectManager::getInstance()
45+
->get(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
46+
parent::__construct($resource, $storeManager, $resourceHelper);
47+
}
48+
2249
/**
2350
* Add suffix to table name to show it is old
2451
*
@@ -92,6 +119,7 @@ protected function populateFlatTables(array $stores)
92119

93120
/**
94121
* Create table and add attributes as fields for specified store.
122+
*
95123
* This routine assumes that DDL operations are allowed
96124
*
97125
* @param int $store
@@ -109,6 +137,7 @@ protected function createTable($store)
109137

110138
/**
111139
* Create category flat tables and add attributes as fields.
140+
*
112141
* Tables are created only if DDL operations are allowed
113142
*
114143
* @param \Magento\Store\Model\Store[] $stores if empty, create tables for all stores of the application
@@ -182,7 +211,7 @@ public function reindexAll()
182211
$stores = $this->storeManager->getStores();
183212
$this->populateFlatTables($stores);
184213
$this->switchTables($stores);
185-
214+
$this->indexer->deleteAbandonedStoreCategoryFlatTables();
186215
$this->allowTableChanges = true;
187216

188217
return $this;

0 commit comments

Comments
 (0)