Skip to content

Commit f344287

Browse files
committed
Merge remote-tracking branch 'epam/MAGETWO-95445' into BugFixPR
2 parents 5919093 + 2fc6596 commit f344287

File tree

1 file changed

+43
-1
lines changed
  • app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action

1 file changed

+43
-1
lines changed

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

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

8+
/**
9+
* Class for full reindex flat categories
10+
*/
811
class Full extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
912
{
1013
/**
@@ -92,6 +95,7 @@ protected function populateFlatTables(array $stores)
9295

9396
/**
9497
* Create table and add attributes as fields for specified store.
98+
*
9599
* This routine assumes that DDL operations are allowed
96100
*
97101
* @param int $store
@@ -109,6 +113,7 @@ protected function createTable($store)
109113

110114
/**
111115
* Create category flat tables and add attributes as fields.
116+
*
112117
* Tables are created only if DDL operations are allowed
113118
*
114119
* @param \Magento\Store\Model\Store[] $stores if empty, create tables for all stores of the application
@@ -167,6 +172,43 @@ protected function switchTables(array $stores = [])
167172
return $this;
168173
}
169174

175+
/**
176+
* Retrieve all actual Catalog Product Flat Table names
177+
*
178+
* @return string[]
179+
*/
180+
private function getActualStoreTablesForCategoryFlat(): array
181+
{
182+
$actualStoreTables = [];
183+
foreach ($this->storeManager->getStores() as $store) {
184+
$actualStoreTables[] = sprintf(
185+
'%s_store_%s',
186+
$this->connection->getTableName('catalog_category_flat'), $store->getId()
187+
);
188+
}
189+
190+
return $actualStoreTables;
191+
}
192+
193+
/**
194+
* Delete all category flat tables for not existing stores
195+
*
196+
* @return void
197+
*/
198+
private function deleteAbandonedStoreCategoryFlatTables(): void
199+
{
200+
$existentTables = $this->connection->getTables(
201+
$this->connection->getTableName('catalog_category_flat_store_%')
202+
);
203+
$actualStoreTables = $this->getActualStoreTablesForCategoryFlat();
204+
205+
$tablesToDelete = array_diff($existentTables, $actualStoreTables);
206+
207+
foreach ($tablesToDelete as $table) {
208+
$this->connection->dropTable($table);
209+
}
210+
}
211+
170212
/**
171213
* Transactional rebuild flat data from eav
172214
*
@@ -182,7 +224,7 @@ public function reindexAll()
182224
$stores = $this->storeManager->getStores();
183225
$this->populateFlatTables($stores);
184226
$this->switchTables($stores);
185-
227+
$this->deleteAbandonedStoreCategoryFlatTables();
186228
$this->allowTableChanges = true;
187229

188230
return $this;

0 commit comments

Comments
 (0)