EnableSegmentation.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Catalog\Setup\Patch\Data;
  8. use Magento\Framework\Setup\ModuleDataSetupInterface;
  9. use Magento\Framework\Setup\Patch\DataPatchInterface;
  10. /**
  11. * Class EnableSegmentation.
  12. *
  13. * @package Magento\Catalog\Setup\Patch
  14. */
  15. class EnableSegmentation implements DataPatchInterface
  16. {
  17. /**
  18. * @var ModuleDataSetupInterface
  19. */
  20. private $moduleDataSetup;
  21. /**
  22. * EnableSegmentation constructor.
  23. *
  24. * @param ModuleDataSetupInterface $moduleDataSetup
  25. */
  26. public function __construct(
  27. ModuleDataSetupInterface $moduleDataSetup
  28. ) {
  29. $this->moduleDataSetup = $moduleDataSetup;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function apply()
  35. {
  36. $this->moduleDataSetup->startSetup();
  37. $setup = $this->moduleDataSetup;
  38. $catalogCategoryProductIndexColumns = array_keys(
  39. $setup->getConnection()->describeTable($setup->getTable('catalog_category_product_index'))
  40. );
  41. $storeSelect = $setup->getConnection()->select()->from($setup->getTable('store'))->where('store_id > 0');
  42. foreach ($setup->getConnection()->fetchAll($storeSelect) as $store) {
  43. $catalogCategoryProductIndexSelect = $setup->getConnection()->select()
  44. ->from(
  45. $setup->getTable('catalog_category_product_index')
  46. )->where(
  47. 'store_id = ?',
  48. $store['store_id']
  49. );
  50. $indexTable = $setup->getTable('catalog_category_product_index') .
  51. '_' .
  52. \Magento\Store\Model\Store::ENTITY .
  53. $store['store_id'];
  54. $setup->getConnection()->query(
  55. $setup->getConnection()->insertFromSelect(
  56. $catalogCategoryProductIndexSelect,
  57. $indexTable,
  58. $catalogCategoryProductIndexColumns,
  59. \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE
  60. )
  61. );
  62. }
  63. $setup->getConnection()->delete($setup->getTable('catalog_category_product_index'));
  64. $setup->getConnection()->delete($setup->getTable('catalog_category_product_index_replica'));
  65. $setup->getConnection()->delete($setup->getTable('catalog_category_product_index_tmp'));
  66. $this->moduleDataSetup->endSetup();
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public static function getDependencies()
  72. {
  73. return [];
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getAliases()
  79. {
  80. return [];
  81. }
  82. }