UpdateBundleRelatedSchema.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Setup\Patch\Schema;
  7. use Magento\Framework\Setup\SchemaSetupInterface;
  8. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  9. use Magento\Framework\Setup\Patch\SchemaPatchInterface;
  10. /**
  11. * Class UpdateBundleRelatedSchema
  12. *
  13. * @package Magento\Bundle\Setup\Patch
  14. */
  15. class UpdateBundleRelatedSchema implements SchemaPatchInterface, PatchVersionInterface
  16. {
  17. /**
  18. * @var SchemaSetupInterface
  19. */
  20. private $schemaSetup;
  21. /**
  22. * UpdateBundleRelatedSchema constructor.
  23. * @param SchemaSetupInterface $schemaSetup
  24. */
  25. public function __construct(
  26. SchemaSetupInterface $schemaSetup
  27. ) {
  28. $this->schemaSetup = $schemaSetup;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function apply()
  34. {
  35. $this->schemaSetup->startSetup();
  36. // Updating data of the 'catalog_product_bundle_option_value' table.
  37. $tableName = $this->schemaSetup->getTable('catalog_product_bundle_option_value');
  38. $select = $this->schemaSetup->getConnection()->select()
  39. ->from(
  40. ['values' => $tableName],
  41. ['value_id']
  42. )->joinLeft(
  43. [
  44. 'options' => $this->schemaSetup->getTable(
  45. 'catalog_product_bundle_option'
  46. )
  47. ],
  48. 'values.option_id = options.option_id',
  49. ['parent_product_id' => 'parent_id']
  50. );
  51. $this->schemaSetup->getConnection()->query(
  52. $this->schemaSetup->getConnection()->insertFromSelect(
  53. $select,
  54. $tableName,
  55. ['value_id', 'parent_product_id'],
  56. \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE
  57. )
  58. );
  59. // Updating data of the 'catalog_product_bundle_selection_price' table.
  60. $tableName = $this->schemaSetup->getTable(
  61. 'catalog_product_bundle_selection_price'
  62. );
  63. $tmpTableName = $this->schemaSetup->getTable(
  64. 'catalog_product_bundle_selection_price_tmp'
  65. );
  66. $existingForeignKeys = $this->schemaSetup->getConnection()->getForeignKeys($tableName);
  67. foreach ($existingForeignKeys as $key) {
  68. $this->schemaSetup->getConnection()->dropForeignKey($key['TABLE_NAME'], $key['FK_NAME']);
  69. }
  70. $this->schemaSetup->getConnection()->createTable(
  71. $this->schemaSetup->getConnection()->createTableByDdl($tableName, $tmpTableName)
  72. );
  73. foreach ($existingForeignKeys as $key) {
  74. $this->schemaSetup->getConnection()->addForeignKey(
  75. $key['FK_NAME'],
  76. $key['TABLE_NAME'],
  77. $key['COLUMN_NAME'],
  78. $key['REF_TABLE_NAME'],
  79. $key['REF_COLUMN_NAME'],
  80. $key['ON_DELETE']
  81. );
  82. }
  83. $this->schemaSetup->getConnection()->query(
  84. $this->schemaSetup->getConnection()->insertFromSelect(
  85. $this->schemaSetup->getConnection()->select()->from($tableName),
  86. $tmpTableName
  87. )
  88. );
  89. $this->schemaSetup->getConnection()->truncateTable($tableName);
  90. $columnsToSelect = [];
  91. foreach ($this->schemaSetup->getConnection()->describeTable($tmpTableName) as $column) {
  92. $alias = $column['COLUMN_NAME'] == 'parent_product_id' ? 'selections.' : 'prices.';
  93. $columnsToSelect[] = $alias . $column['COLUMN_NAME'];
  94. }
  95. $select = $this->schemaSetup->getConnection()->select()
  96. ->from(
  97. ['prices' => $tmpTableName],
  98. []
  99. )->joinLeft(
  100. [
  101. 'selections' => $this->schemaSetup->getTable(
  102. 'catalog_product_bundle_selection'
  103. )
  104. ],
  105. 'prices.selection_id = selections.selection_id',
  106. []
  107. )->columns($columnsToSelect);
  108. $this->schemaSetup->getConnection()->query(
  109. $this->schemaSetup->getConnection()->insertFromSelect($select, $tableName)
  110. );
  111. $this->schemaSetup->getConnection()->dropTable($tmpTableName);
  112. $this->schemaSetup->endSetup();
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public static function getDependencies()
  118. {
  119. return [];
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public static function getVersion()
  125. {
  126. return '2.0.4';
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getAliases()
  132. {
  133. return [];
  134. }
  135. }