5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Category \Flat \Action ;
7
7
8
+ /**
9
+ * Class for full reindex flat categories
10
+ */
8
11
class Full extends \Magento \Catalog \Model \Indexer \Category \Flat \AbstractAction
9
12
{
10
13
/**
@@ -92,6 +95,7 @@ protected function populateFlatTables(array $stores)
92
95
93
96
/**
94
97
* Create table and add attributes as fields for specified store.
98
+ *
95
99
* This routine assumes that DDL operations are allowed
96
100
*
97
101
* @param int $store
@@ -109,6 +113,7 @@ protected function createTable($store)
109
113
110
114
/**
111
115
* Create category flat tables and add attributes as fields.
116
+ *
112
117
* Tables are created only if DDL operations are allowed
113
118
*
114
119
* @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 = [])
167
172
return $ this ;
168
173
}
169
174
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
+
170
212
/**
171
213
* Transactional rebuild flat data from eav
172
214
*
@@ -182,7 +224,7 @@ public function reindexAll()
182
224
$ stores = $ this ->storeManager ->getStores ();
183
225
$ this ->populateFlatTables ($ stores );
184
226
$ this ->switchTables ($ stores );
185
-
227
+ $ this -> deleteAbandonedStoreCategoryFlatTables ();
186
228
$ this ->allowTableChanges = true ;
187
229
188
230
return $ this ;
0 commit comments