35 private $patchRegistryFactory;
40 private $dataPatchReader;
45 private $schemaPatchReader;
50 private $resourceConnection;
55 private $patchHistory;
60 private $patchFactory;
70 private $moduleDataSetup;
75 private $objectManager;
80 private $patchBackwardCompatability;
116 $this->patchRegistryFactory = $patchRegistryFactory;
117 $this->dataPatchReader = $dataPatchReader;
118 $this->schemaPatchReader = $schemaPatchReader;
120 $this->patchHistory = $patchHistory;
121 $this->patchFactory = $patchFactory;
122 $this->schemaSetup = $schemaSetup;
123 $this->moduleDataSetup = $moduleDataSetup;
125 $this->patchBackwardCompatability = $patchBackwardCompatability;
126 $this->moduleList = $moduleList;
137 $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
142 if ($this->patchBackwardCompatability->isSkipableByDataSetupVersion($dataPatch, $moduleName)) {
143 $this->patchHistory->fixPatch($dataPatch);
147 $dataPatch = $this->objectManager->create(
149 [
'moduleDataSetup' => $this->moduleDataSetup]
153 new Phrase(
"Patch %1 should implement DataPatchInterface", [get_class($dataPatch)])
158 $this->patchHistory->fixPatch(get_class($dataPatch));
161 $this->moduleDataSetup->getConnection()->beginTransaction();
163 $this->patchHistory->fixPatch(get_class($dataPatch));
164 $this->moduleDataSetup->getConnection()->commit();
166 $this->moduleDataSetup->getConnection()->rollBack();
183 private function prepareRegistry($moduleName, $patchType)
185 $reader = $patchType === self::DATA_PATCH ? $this->dataPatchReader : $this->schemaPatchReader;
186 $registry = $this->patchRegistryFactory->create();
189 if ($moduleName ===
null) {
191 foreach ($this->moduleList->getNames() as $moduleName) {
192 $patchNames += $reader->read($moduleName);
195 $patchNames = $reader->read($moduleName);
198 foreach ($patchNames as $patchName) {
213 public function applySchemaPatch($moduleName =
null)
215 $registry = $this->prepareRegistry($moduleName, self::SCHEMA_PATCH);
221 if ($this->patchBackwardCompatability->isSkipableBySchemaSetupVersion($schemaPatch, $moduleName)) {
222 $this->patchHistory->fixPatch($schemaPatch);
228 $schemaPatch = $this->patchFactory->create($schemaPatch, [
'schemaSetup' => $this->schemaSetup]);
229 $schemaPatch->apply();
230 $this->patchHistory->fixPatch(get_class($schemaPatch));
231 }
catch (\Exception $e) {
234 'Unable to apply patch %1 for module %2. Original exception message: %3',
236 get_class($schemaPatch),
254 public function revertDataPatches($moduleName =
null)
256 $registry = $this->prepareRegistry($moduleName, self::DATA_PATCH);
257 $adapter = $this->moduleDataSetup->getConnection();
259 foreach (
$registry->getReverseIterator() as $dataPatch) {
260 $dataPatch = $this->objectManager->create(
262 [
'moduleDataSetup' => $this->moduleDataSetup]
264 if ($dataPatch instanceof PatchRevertableInterface) {
268 $dataPatch->revert();
269 $this->patchHistory->revertPatchFromHistory(get_class($dataPatch));
271 }
catch (\Exception $e) {
273 throw new Exception(
new Phrase($e->getMessage()));
applyDataPatch($moduleName=null)
__construct(PatchReader $dataPatchReader, PatchReader $schemaPatchReader, PatchRegistryFactory $patchRegistryFactory, ResourceConnection $resourceConnection, PatchBackwardCompatability $patchBackwardCompatability, PatchHistory $patchHistory, PatchFactory $patchFactory, ObjectManagerInterface $objectManager, \Magento\Framework\Setup\SchemaSetupInterface $schemaSetup, \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup, ModuleList $moduleList)