@@ -153,19 +153,15 @@ public function applyDataPatch($moduleName = null)
153
153
new Phrase ("Patch %1 should implement DataPatchInterface " , [get_class ($ dataPatch )])
154
154
);
155
155
}
156
+ if ($ this ->isApplied ($ dataPatch )) {
157
+ continue ;
158
+ }
156
159
if ($ dataPatch instanceof NonTransactionableInterface) {
157
- $ dataPatch ->apply ();
158
- $ this ->patchHistory ->fixPatch (get_class ($ dataPatch ));
160
+ $ this ->applyPatch ($ dataPatch );
159
161
} else {
160
162
try {
161
163
$ this ->moduleDataSetup ->getConnection ()->beginTransaction ();
162
- $ dataPatch ->apply ();
163
- $ this ->patchHistory ->fixPatch (get_class ($ dataPatch ));
164
- foreach ($ dataPatch ->getAliases () as $ patchAlias ) {
165
- if (!$ this ->patchHistory ->isApplied ($ patchAlias )) {
166
- $ this ->patchHistory ->fixPatch ($ patchAlias );
167
- }
168
- }
164
+ $ this ->applyPatch ($ dataPatch );
169
165
$ this ->moduleDataSetup ->getConnection ()->commit ();
170
166
} catch (\Exception $ e ) {
171
167
$ this ->moduleDataSetup ->getConnection ()->rollBack ();
@@ -187,35 +183,6 @@ public function applyDataPatch($moduleName = null)
187
183
}
188
184
}
189
185
190
- /**
191
- * Register all patches in registry in order to manipulate chains and dependencies of patches of patches
192
- *
193
- * @param string $moduleName
194
- * @param string $patchType
195
- * @return PatchRegistry
196
- */
197
- private function prepareRegistry ($ moduleName , $ patchType )
198
- {
199
- $ reader = $ patchType === self ::DATA_PATCH ? $ this ->dataPatchReader : $ this ->schemaPatchReader ;
200
- $ registry = $ this ->patchRegistryFactory ->create ();
201
-
202
- //Prepare modules to read
203
- if ($ moduleName === null ) {
204
- $ patchNames = [];
205
- foreach ($ this ->moduleList ->getNames () as $ moduleName ) {
206
- $ patchNames += $ reader ->read ($ moduleName );
207
- }
208
- } else {
209
- $ patchNames = $ reader ->read ($ moduleName );
210
- }
211
-
212
- foreach ($ patchNames as $ patchName ) {
213
- $ registry ->registerPatch ($ patchName );
214
- }
215
-
216
- return $ registry ;
217
- }
218
-
219
186
/**
220
187
* Apply all patches for one module
221
188
*
@@ -240,12 +207,8 @@ public function applySchemaPatch($moduleName = null)
240
207
* @var SchemaPatchInterface $schemaPatch
241
208
*/
242
209
$ schemaPatch = $ this ->patchFactory ->create ($ schemaPatch , ['schemaSetup ' => $ this ->schemaSetup ]);
243
- $ schemaPatch ->apply ();
244
- $ this ->patchHistory ->fixPatch (get_class ($ schemaPatch ));
245
- foreach ($ schemaPatch ->getAliases () as $ patchAlias ) {
246
- if (!$ this ->patchHistory ->isApplied ($ patchAlias )) {
247
- $ this ->patchHistory ->fixPatch ($ patchAlias );
248
- }
210
+ if (!$ this ->isApplied ($ schemaPatch )) {
211
+ $ this ->applyPatch ($ schemaPatch );
249
212
}
250
213
} catch (\Exception $ e ) {
251
214
$ schemaPatchClass = is_object ($ schemaPatch ) ? get_class ($ schemaPatch ) : $ schemaPatch ;
@@ -281,7 +244,7 @@ public function revertDataPatches($moduleName = null)
281
244
'\\' . $ dataPatch ,
282
245
['moduleDataSetup ' => $ this ->moduleDataSetup ]
283
246
);
284
- if ($ dataPatch instanceof PatchRevertableInterface) {
247
+ if ($ dataPatch instanceof PatchRevertableInterface && $ this -> isApplied ( $ dataPatch ) ) {
285
248
try {
286
249
$ adapter ->beginTransaction ();
287
250
/** @var PatchRevertableInterface|DataPatchInterface $dataPatch */
@@ -297,4 +260,63 @@ public function revertDataPatches($moduleName = null)
297
260
}
298
261
}
299
262
}
263
+
264
+ /**
265
+ * Register all patches in registry in order to manipulate chains and dependencies of patches of patches
266
+ *
267
+ * @param string $moduleName
268
+ * @param string $patchType
269
+ * @return PatchRegistry
270
+ */
271
+ private function prepareRegistry ($ moduleName , $ patchType )
272
+ {
273
+ $ reader = $ patchType === self ::DATA_PATCH ? $ this ->dataPatchReader : $ this ->schemaPatchReader ;
274
+ $ registry = $ this ->patchRegistryFactory ->create ();
275
+
276
+ //Prepare modules to read
277
+ if ($ moduleName === null ) {
278
+ $ patchNames = [];
279
+ foreach ($ this ->moduleList ->getNames () as $ moduleName ) {
280
+ $ patchNames += $ reader ->read ($ moduleName );
281
+ }
282
+ } else {
283
+ $ patchNames = $ reader ->read ($ moduleName );
284
+ }
285
+
286
+ foreach ($ patchNames as $ patchName ) {
287
+ $ registry ->registerPatch ($ patchName );
288
+ }
289
+
290
+ return $ registry ;
291
+ }
292
+
293
+ /**
294
+ * Apply the given patch. The patch is and its aliases are added to the history.
295
+ */
296
+ private function applyPatch ((PatchInterface $ patch ): void
297
+ {
298
+ $ patch ->apply ();
299
+ $ this ->patchHistory ->fixPatch (get_class ($ schemaPatch ));
300
+ foreach ($ schemaPatch ->getAliases () as $ patchAlias ) {
301
+ if (!$ this ->patchHistory ->isApplied ($ patchAlias )) {
302
+ $ this ->patchHistory ->fixPatch ($ patchAlias );
303
+ }
304
+ }
305
+ }
306
+
307
+ /**
308
+ * Check wether the given patch or any of its aliases are already applied or not.
309
+ */
310
+ private function isApplied (PatchInterface $ patch ): bool
311
+ {
312
+ if (!$ this ->patchHistory ->isApplied (get_class ($ patch ))) {
313
+ foreach ($ patch ->getAliases () as $ alias ) {
314
+ if ($ this ->patchHistory ->isApplied ($ alias )) {
315
+ return true ;
316
+ }
317
+ }
318
+ }
319
+
320
+ return false ;
321
+ }
300
322
}
0 commit comments