ConvertSerializedDataToJson.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Setup\Patch\Data;
  7. use Magento\Eav\Model\Config;
  8. use Magento\Framework\App\State;
  9. use Magento\Framework\DB\AggregatedFieldDataConverter;
  10. use Magento\Framework\DB\DataConverter\SerializedToJson;
  11. use Magento\Framework\DB\FieldToConvert;
  12. use Magento\Framework\Setup\ModuleContextInterface;
  13. use Magento\Framework\Setup\ModuleDataSetupInterface;
  14. use Magento\Framework\Setup\UpgradeDataInterface;
  15. use Magento\Quote\Model\QuoteFactory;
  16. use Magento\Sales\Model\OrderFactory;
  17. use Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory as AddressCollectionFactory;
  18. use Magento\Framework\App\ResourceConnection;
  19. use Magento\Sales\Setup\SalesOrderPaymentDataConverter;
  20. use Magento\Sales\Setup\SalesSetup;
  21. use Magento\Sales\Setup\SalesSetupFactory;
  22. use Magento\Sales\Setup\SerializedDataConverter;
  23. use Magento\Framework\Setup\Patch\DataPatchInterface;
  24. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  25. /**
  26. * Class ConvertSerializedDataToJson
  27. * @package Magento\Sales\Setup\Patch
  28. */
  29. class ConvertSerializedDataToJson implements DataPatchInterface, PatchVersionInterface
  30. {
  31. /**
  32. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  33. */
  34. private $moduleDataSetup;
  35. /**
  36. * @var SalesSetupFactory
  37. */
  38. private $salesSetupFactory;
  39. /**
  40. * @var Config
  41. */
  42. private $eavConfig;
  43. /**
  44. * @var AggregatedFieldDataConverter
  45. */
  46. private $aggregatedFieldDataConverter;
  47. /**
  48. * PatchInitial constructor.
  49. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  50. */
  51. public function __construct(
  52. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  53. SalesSetupFactory $salesSetupFactory,
  54. \Magento\Eav\Model\Config $eavConfig,
  55. AggregatedFieldDataConverter $aggregatedFieldDataConverter
  56. ) {
  57. $this->moduleDataSetup = $moduleDataSetup;
  58. $this->salesSetupFactory = $salesSetupFactory;
  59. $this->eavConfig = $eavConfig;
  60. $this->aggregatedFieldDataConverter = $aggregatedFieldDataConverter;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function apply()
  66. {
  67. /** @var SalesSetup $salesSetup */
  68. $salesSetup = $this->salesSetupFactory->create();
  69. $this->convertSerializedDataToJson($salesSetup);
  70. $this->eavConfig->clear();
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public static function getDependencies()
  76. {
  77. return [
  78. UpdateEntityTypes::class
  79. ];
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public static function getVersion()
  85. {
  86. return '2.0.6';
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getAliases()
  92. {
  93. return [];
  94. }
  95. /**
  96. * Convert native serialization to JSON.
  97. *
  98. * @param SalesSetup $salesSetup
  99. */
  100. private function convertSerializedDataToJson(SalesSetup $salesSetup)
  101. {
  102. $fieldsToUpdate = [
  103. new FieldToConvert(
  104. SerializedToJson::class,
  105. $salesSetup->getTable('sales_invoice_item'),
  106. 'entity_id',
  107. 'tax_ratio'
  108. ),
  109. new FieldToConvert(
  110. SerializedToJson::class,
  111. $salesSetup->getTable('sales_creditmemo_item'),
  112. 'entity_id',
  113. 'tax_ratio'
  114. ),
  115. ];
  116. $fieldsToUpdate[] = new FieldToConvert(
  117. SerializedDataConverter::class,
  118. $salesSetup->getTable('sales_order_item'),
  119. 'item_id',
  120. 'product_options'
  121. );
  122. $fieldsToUpdate[] = new FieldToConvert(
  123. SerializedToJson::class,
  124. $salesSetup->getTable('sales_shipment'),
  125. 'entity_id',
  126. 'packages'
  127. );
  128. $fieldsToUpdate[] = new FieldToConvert(
  129. SalesOrderPaymentDataConverter::class,
  130. $salesSetup->getTable('sales_order_payment'),
  131. 'entity_id',
  132. 'additional_information'
  133. );
  134. $fieldsToUpdate[] = new FieldToConvert(
  135. SerializedToJson::class,
  136. $salesSetup->getTable('sales_payment_transaction'),
  137. 'transaction_id',
  138. 'additional_information'
  139. );
  140. $this->aggregatedFieldDataConverter->convert($fieldsToUpdate, $salesSetup->getConnection());
  141. }
  142. }