PatchApplier.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Patch;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Module\ModuleList;
  9. use Magento\Framework\ObjectManagerInterface;
  10. use Magento\Framework\Phrase;
  11. use Magento\Framework\Setup\Exception;
  12. use Magento\Framework\Setup\ModuleDataSetupInterface;
  13. /**
  14. * Apply patches per specific module
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class PatchApplier
  18. {
  19. /**
  20. * Flag means, that we need to read schema patches
  21. */
  22. const SCHEMA_PATCH = 'schema';
  23. /**
  24. * Flag means, that we need to read data patches
  25. */
  26. const DATA_PATCH = 'data';
  27. /**
  28. * @var PatchRegistryFactory
  29. */
  30. private $patchRegistryFactory;
  31. /**
  32. * @var PatchReader
  33. */
  34. private $dataPatchReader;
  35. /**
  36. * @var PatchReader
  37. */
  38. private $schemaPatchReader;
  39. /**
  40. * @var ResourceConnection
  41. */
  42. private $resourceConnection;
  43. /**
  44. * @var PatchHistory
  45. */
  46. private $patchHistory;
  47. /**
  48. * @var PatchFactory
  49. */
  50. private $patchFactory;
  51. /**
  52. * @var \Magento\Framework\Setup\SetupInterface
  53. */
  54. private $schemaSetup;
  55. /**
  56. * @var ModuleDataSetupInterface
  57. */
  58. private $moduleDataSetup;
  59. /**
  60. * @var ObjectManagerInterface
  61. */
  62. private $objectManager;
  63. /**
  64. * @var PatchBackwardCompatability
  65. */
  66. private $patchBackwardCompatability;
  67. /**
  68. * @var ModuleList
  69. */
  70. private $moduleList;
  71. /**
  72. * PatchApplier constructor.
  73. * @param PatchReader $dataPatchReader
  74. * @param PatchReader $schemaPatchReader
  75. * @param PatchRegistryFactory $patchRegistryFactory
  76. * @param ResourceConnection $resourceConnection
  77. * @param PatchBackwardCompatability $patchBackwardCompatability
  78. * @param PatchHistory $patchHistory
  79. * @param PatchFactory $patchFactory
  80. * @param ObjectManagerInterface $objectManager
  81. * @param \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup
  82. * @param ModuleDataSetupInterface $moduleDataSetup
  83. * @param ModuleList $moduleList
  84. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  85. * @SuppressWarnings(Magento.TypeDuplication)
  86. */
  87. public function __construct(
  88. PatchReader $dataPatchReader,
  89. PatchReader $schemaPatchReader,
  90. PatchRegistryFactory $patchRegistryFactory,
  91. ResourceConnection $resourceConnection,
  92. PatchBackwardCompatability $patchBackwardCompatability,
  93. PatchHistory $patchHistory,
  94. PatchFactory $patchFactory,
  95. ObjectManagerInterface $objectManager,
  96. \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup,
  97. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  98. ModuleList $moduleList
  99. ) {
  100. $this->patchRegistryFactory = $patchRegistryFactory;
  101. $this->dataPatchReader = $dataPatchReader;
  102. $this->schemaPatchReader = $schemaPatchReader;
  103. $this->resourceConnection = $resourceConnection;
  104. $this->patchHistory = $patchHistory;
  105. $this->patchFactory = $patchFactory;
  106. $this->schemaSetup = $schemaSetup;
  107. $this->moduleDataSetup = $moduleDataSetup;
  108. $this->objectManager = $objectManager;
  109. $this->patchBackwardCompatability = $patchBackwardCompatability;
  110. $this->moduleList = $moduleList;
  111. }
  112. /**
  113. * Apply all patches for one module
  114. *
  115. * @param null | string $moduleName
  116. * @throws Exception
  117. */
  118. public function applyDataPatch($moduleName = null)
  119. {
  120. $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
  121. foreach ($registry as $dataPatch) {
  122. /**
  123. * Due to backward compatabilities reasons some patches should be skipped
  124. */
  125. if ($this->patchBackwardCompatability->isSkipableByDataSetupVersion($dataPatch, $moduleName)) {
  126. $this->patchHistory->fixPatch($dataPatch);
  127. continue;
  128. }
  129. $dataPatch = $this->objectManager->create(
  130. '\\' . $dataPatch,
  131. ['moduleDataSetup' => $this->moduleDataSetup]
  132. );
  133. if (!$dataPatch instanceof DataPatchInterface) {
  134. throw new Exception(
  135. new Phrase("Patch %1 should implement DataPatchInterface", [get_class($dataPatch)])
  136. );
  137. }
  138. if ($dataPatch instanceof NonTransactionableInterface) {
  139. $dataPatch->apply();
  140. $this->patchHistory->fixPatch(get_class($dataPatch));
  141. } else {
  142. try {
  143. $this->moduleDataSetup->getConnection()->beginTransaction();
  144. $dataPatch->apply();
  145. $this->patchHistory->fixPatch(get_class($dataPatch));
  146. $this->moduleDataSetup->getConnection()->commit();
  147. } catch (\Exception $e) {
  148. $this->moduleDataSetup->getConnection()->rollBack();
  149. throw new Exception(new Phrase($e->getMessage()));
  150. } finally {
  151. unset($dataPatch);
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Register all patches in registry in order to manipulate chains and dependencies of patches
  158. * of patches
  159. *
  160. * @param string $moduleName
  161. * @param string $patchType
  162. * @return PatchRegistry
  163. */
  164. private function prepareRegistry($moduleName, $patchType)
  165. {
  166. $reader = $patchType === self::DATA_PATCH ? $this->dataPatchReader : $this->schemaPatchReader;
  167. $registry = $this->patchRegistryFactory->create();
  168. //Prepare modules to read
  169. if ($moduleName === null) {
  170. $patchNames = [];
  171. foreach ($this->moduleList->getNames() as $moduleName) {
  172. $patchNames += $reader->read($moduleName);
  173. }
  174. } else {
  175. $patchNames = $reader->read($moduleName);
  176. }
  177. foreach ($patchNames as $patchName) {
  178. $registry->registerPatch($patchName);
  179. }
  180. return $registry;
  181. }
  182. /**
  183. * Apply all patches for one module
  184. *
  185. * Please note: that schema patches are not revertable
  186. *
  187. * @param null | string $moduleName
  188. * @throws Exception
  189. */
  190. public function applySchemaPatch($moduleName = null)
  191. {
  192. $registry = $this->prepareRegistry($moduleName, self::SCHEMA_PATCH);
  193. foreach ($registry as $schemaPatch) {
  194. try {
  195. /**
  196. * Skip patches that were applied in old style
  197. */
  198. if ($this->patchBackwardCompatability->isSkipableBySchemaSetupVersion($schemaPatch, $moduleName)) {
  199. $this->patchHistory->fixPatch($schemaPatch);
  200. continue;
  201. }
  202. /**
  203. * @var SchemaPatchInterface $schemaPatch
  204. */
  205. $schemaPatch = $this->patchFactory->create($schemaPatch, ['schemaSetup' => $this->schemaSetup]);
  206. $schemaPatch->apply();
  207. $this->patchHistory->fixPatch(get_class($schemaPatch));
  208. } catch (\Exception $e) {
  209. throw new Exception(
  210. new Phrase(
  211. 'Unable to apply patch %1 for module %2. Original exception message: %3',
  212. [
  213. get_class($schemaPatch),
  214. $moduleName,
  215. $e->getMessage()
  216. ]
  217. )
  218. );
  219. } finally {
  220. unset($schemaPatch);
  221. }
  222. }
  223. }
  224. /**
  225. * Revert data patches for specific module
  226. *
  227. * @param null | string $moduleName
  228. * @throws Exception
  229. */
  230. public function revertDataPatches($moduleName = null)
  231. {
  232. $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
  233. $adapter = $this->moduleDataSetup->getConnection();
  234. foreach ($registry->getReverseIterator() as $dataPatch) {
  235. $dataPatch = $this->objectManager->create(
  236. '\\' . $dataPatch,
  237. ['moduleDataSetup' => $this->moduleDataSetup]
  238. );
  239. if ($dataPatch instanceof PatchRevertableInterface) {
  240. try {
  241. $adapter->beginTransaction();
  242. /** @var PatchRevertableInterface|DataPatchInterface $dataPatch */
  243. $dataPatch->revert();
  244. $this->patchHistory->revertPatchFromHistory(get_class($dataPatch));
  245. $adapter->commit();
  246. } catch (\Exception $e) {
  247. $adapter->rollBack();
  248. throw new Exception(new Phrase($e->getMessage()));
  249. } finally {
  250. unset($dataPatch);
  251. }
  252. }
  253. }
  254. }
  255. }