@@ -21,6 +21,11 @@ class Version11410to2000 extends DatabaseStep implements \Migration\Step\StepInt
21
21
*/
22
22
protected $ duplicateIndex ;
23
23
24
+ /**
25
+ * @var array
26
+ */
27
+ protected $ resolvedDuplicates = [];
28
+
24
29
/**
25
30
* Resource of source
26
31
*
@@ -199,8 +204,8 @@ public function run()
199
204
$ this ->migrateRewrites ($ records , $ destinationRecords );
200
205
$ this ->destination ->saveRecords ($ destinationDocument ->getName (), $ destinationRecords );
201
206
}
202
- $ this ->copyEavData ('catalog_category_entity_url_key ' , 'catalog_category_entity_varchar ' );
203
- $ this ->copyEavData ('catalog_product_entity_url_key ' , 'catalog_product_entity_varchar ' );
207
+ $ this ->copyEavData ('catalog_category_entity_url_key ' , 'catalog_category_entity_varchar ' , ' category ' );
208
+ $ this ->copyEavData ('catalog_product_entity_url_key ' , 'catalog_product_entity_varchar ' , ' product ' );
204
209
$ this ->progress ->finish ();
205
210
}
206
211
@@ -245,6 +250,7 @@ protected function migrateRewrites($source, $destination)
245
250
$ destinationRecord ->setValue ('redirect_type ' , 0 );
246
251
$ destinationRecord ->setValue ('is_autogenerated ' , $ sourceRecord ->getValue ('is_system ' ));
247
252
$ destinationRecord ->setValue ('metadata ' , '' );
253
+ $ destinationRecord ->setValue ('redirect_type ' , $ sourceRecord ->getValue ('redirect_type ' ));
248
254
$ destinationRecord ->setValue ('entity_type ' , $ sourceRecord ->getValue ('entity_type ' ));
249
255
$ destinationRecord ->setValue ('request_path ' , $ sourceRecord ->getValue ('request_path ' ));
250
256
@@ -260,10 +266,11 @@ protected function migrateRewrites($source, $destination)
260
266
$ destinationRecord ->setValue ('entity_id ' , $ productId );
261
267
$ targetPath = "catalog/product/view/id/ $ productId/category/ $ categoryId " ;
262
268
} elseif (!empty ($ productId ) && empty ($ categoryId )) {
263
- $ destinationRecord ->setValue ('entity_type ' , 'category ' );
269
+ $ destinationRecord ->setValue ('entity_type ' , 'product ' );
264
270
$ destinationRecord ->setValue ('entity_id ' , $ productId );
265
271
$ targetPath = 'catalog/product/view/id/ ' . $ productId ;
266
272
} elseif (empty ($ productId ) && !empty ($ categoryId )) {
273
+ $ destinationRecord ->setValue ('entity_type ' , 'category ' );
267
274
$ destinationRecord ->setValue ('entity_id ' , $ categoryId );
268
275
$ targetPath = 'catalog/category/view/id/ ' . $ categoryId ;
269
276
} else {
@@ -284,17 +291,24 @@ protected function migrateRewrites($source, $destination)
284
291
}
285
292
}
286
293
if ($ shouldResolve ) {
287
- $ destinationRecord ->setValue (
288
- 'request_path ' ,
289
- $ sourceRecord ->getValue ('request_path ' ) . '- ' . md5 (mt_rand ())
290
- );
291
- $ message = 'Duplicate resolved. ' . sprintf (
292
- 'Request path was: %s Target path was: %s Store ID: %s New request path: %s ' ,
293
- $ sourceRecord ->getValue ('request_path ' ),
294
- $ sourceRecord ->getValue ('target_path ' ),
295
- $ sourceRecord ->getValue ('store_id ' ),
296
- $ destinationRecord ->getValue ('request_path ' )
294
+ $ key = md5 (mt_rand ());
295
+ $ requestPath = preg_replace (
296
+ '/^(.*)\.([^\.]+)$/i ' ,
297
+ '$1- ' . $ key . '.$2 ' ,
298
+ $ sourceRecord ->getValue ('request_path ' )
297
299
);
300
+ $ this ->resolvedDuplicates [$ destinationRecord ->getValue ('entity_type ' )]
301
+ [$ destinationRecord ->getValue ('entity_id ' )]
302
+ [$ sourceRecord ->getValue ('store_id ' )] = $ key ;
303
+ $ destinationRecord ->setValue ('request_path ' , $ requestPath );
304
+ $ message = 'Duplicate resolved. '
305
+ . sprintf (
306
+ 'Request path was: %s Target path was: %s Store ID: %s New request path: %s ' ,
307
+ $ sourceRecord ->getValue ('request_path ' ),
308
+ $ sourceRecord ->getValue ('target_path ' ),
309
+ $ sourceRecord ->getValue ('store_id ' ),
310
+ $ destinationRecord ->getValue ('request_path ' )
311
+ );
298
312
$ this ->logger ->addInfo ($ message );
299
313
}
300
314
}
@@ -312,7 +326,7 @@ protected function migrateRewrites($source, $destination)
312
326
* @param string $destinationName
313
327
* @return void
314
328
*/
315
- protected function copyEavData ($ sourceName , $ destinationName )
329
+ protected function copyEavData ($ sourceName , $ destinationName, $ type )
316
330
{
317
331
$ destinationDocument = $ this ->destination ->getDocument ($ destinationName );
318
332
$ pageNumber = 0 ;
@@ -323,6 +337,21 @@ protected function copyEavData($sourceName, $destinationName)
323
337
$ this ->progress ->advance ();
324
338
unset($ row ['value_id ' ]);
325
339
unset($ row ['entity_type_id ' ]);
340
+ if (!empty ($ this ->resolvedDuplicates [$ type ][$ row ['entity_id ' ]][$ row ['store_id ' ]])) {
341
+ $ row ['value ' ] = $ row ['value ' ] . '- '
342
+ . $ this ->resolvedDuplicates [$ type ][$ row ['entity_id ' ]][$ row ['store_id ' ]];
343
+ } elseif (!empty ($ this ->resolvedDuplicates [$ type ][$ row ['entity_id ' ]]) && $ row ['store_id ' ] == 0 ) {
344
+ foreach ($ this ->resolvedDuplicates [$ type ][$ row ['entity_id ' ]] as $ storeId => $ urlKey ) {
345
+ $ storeRow = $ row ;
346
+ $ storeRow ['store_id ' ] = $ storeId ;
347
+ $ storeRow ['value ' ] = $ storeRow ['value ' ] . '- ' . $ urlKey ;
348
+ $ records ->addRecord ($ this ->recordFactory ->create (['data ' => $ storeRow ]));
349
+ if (!isset ($ this ->resolvedDuplicates [$ destinationName ])) {
350
+ $ this ->resolvedDuplicates [$ destinationName ] = 0 ;
351
+ }
352
+ $ this ->resolvedDuplicates [$ destinationName ]++;;
353
+ }
354
+ }
326
355
$ records ->addRecord ($ this ->recordFactory ->create (['data ' => $ row ]));
327
356
}
328
357
$ this ->destination ->saveRecords ($ destinationName , $ records );
@@ -401,15 +430,23 @@ public function volumeCheck()
401
430
$ this ->progress ->start (1 );
402
431
$ this ->getRewritesSelect ();
403
432
$ this ->progress ->advance ();
433
+ $ categoryRecords = $ this ->source ->getRecordsCount ('catalog_category_entity_varchar ' )
434
+ + $ this ->source ->getRecordsCount ('catalog_category_entity_url_key ' )
435
+ + (isset ($ this ->resolvedDuplicates ['catalog_category_entity_varchar ' ])
436
+ ? $ this ->resolvedDuplicates ['catalog_category_entity_varchar ' ]
437
+ : 0
438
+ );
439
+ $ productRecords = $ this ->source ->getRecordsCount ('catalog_product_entity_varchar ' )
440
+ + $ this ->source ->getRecordsCount ('catalog_product_entity_url_key ' )
441
+ + (isset ($ this ->resolvedDuplicates ['catalog_product_entity_varchar ' ])
442
+ ? $ this ->resolvedDuplicates ['catalog_product_entity_varchar ' ]
443
+ : 0
444
+ );
404
445
405
446
$ result = $ this ->source ->getRecordsCount ($ this ->tableName )
406
447
== $ this ->destination ->getRecordsCount ('url_rewrite ' )
407
- && $ this ->source ->getRecordsCount ('catalog_category_entity_varchar ' )
408
- + $ this ->source ->getRecordsCount ('catalog_category_entity_url_key ' )
409
- == $ this ->destination ->getRecordsCount ('catalog_category_entity_varchar ' )
410
- && $ this ->source ->getRecordsCount ('catalog_product_entity_varchar ' )
411
- + $ this ->source ->getRecordsCount ('catalog_product_entity_url_key ' )
412
- == $ this ->destination ->getRecordsCount ('catalog_product_entity_varchar ' );
448
+ && $ categoryRecords == $ this ->destination ->getRecordsCount ('catalog_category_entity_varchar ' )
449
+ && $ productRecords == $ this ->destination ->getRecordsCount ('catalog_product_entity_varchar ' );
413
450
$ this ->progress ->finish ();
414
451
return $ result ;
415
452
}
@@ -430,8 +467,8 @@ public function getTitle()
430
467
protected function getIterationsCount ()
431
468
{
432
469
return $ this ->source ->getRecordsCount ($ this ->tableName )
433
- + $ this ->source ->getRecordsCount ('catalog_category_entity_url_key ' )
434
- + $ this ->source ->getRecordsCount ('catalog_product_entity_url_key ' );
470
+ + $ this ->source ->getRecordsCount ('catalog_category_entity_url_key ' )
471
+ + $ this ->source ->getRecordsCount ('catalog_product_entity_url_key ' );
435
472
}
436
473
437
474
/**
@@ -525,64 +562,109 @@ protected function getSuffix($suffixFor, $mainTable = 's')
525
562
* Initialize temporary table and insert UrlRewrite data
526
563
*
527
564
* @codeCoverageIgnore
565
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
528
566
* @return void
529
567
*/
530
568
protected function initTemporaryTable ()
531
569
{
532
570
/** @var \Migration\Resource\Adapter\Mysql $adapter */
533
571
$ adapter = $ this ->source ->getAdapter ();
534
572
$ select = $ adapter ->getSelect ();
535
- $ select ->getAdapter ()->query ('DROP TABLE IF EXISTS ' . $ this ->source ->addDocumentPrefix ($ this ->tableName ));
536
- $ select ->getAdapter ()->query (
537
- 'CREATE TABLE ' . $ this ->source ->addDocumentPrefix ($ this ->tableName ) . ' (
538
- id INT NOT NULL AUTO_INCREMENT,
539
- request_path VARCHAR(255) NOT NULL,
540
- target_path VARCHAR(255) NOT NULL,
541
- is_system SMALLINT NOT NULL DEFAULT 0,
542
- store_id INT NOT NULL,
543
- entity_type VARCHAR(32) NOT NULL,
544
- product_id INT NOT NULL DEFAULT 0,
545
- category_id INT NOT NULL DEFAULT 0,
546
- priority INT NOT NULL DEFAULT 0,
547
- PRIMARY KEY (id),
548
- UNIQUE (request_path, target_path, store_id, entity_type)
549
- ) '
550
- );
573
+ $ select ->getAdapter ()->dropTable ($ this ->source ->addDocumentPrefix ($ this ->tableName ));
574
+ /** @var \Magento\Framework\DB\Ddl\Table $table */
575
+ $ table = $ select ->getAdapter ()->newTable ($ this ->source ->addDocumentPrefix ($ this ->tableName ))
576
+ ->addColumn (
577
+ 'id ' ,
578
+ \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER ,
579
+ null ,
580
+ ['identity ' => true , 'unsigned ' => true , 'nullable ' => false , 'primary ' => true ]
581
+ )
582
+ ->addColumn (
583
+ 'request_path ' ,
584
+ \Magento \Framework \DB \Ddl \Table::TYPE_TEXT ,
585
+ 255
586
+ )
587
+ ->addColumn (
588
+ 'target_path ' ,
589
+ \Magento \Framework \DB \Ddl \Table::TYPE_TEXT ,
590
+ 255
591
+ )
592
+ ->addColumn (
593
+ 'is_system ' ,
594
+ \Magento \Framework \DB \Ddl \Table::TYPE_SMALLINT ,
595
+ null ,
596
+ ['nullable ' => false , 'default ' => '0 ' ]
597
+ )
598
+ ->addColumn (
599
+ 'store_id ' ,
600
+ \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER
601
+ )
602
+ ->addColumn (
603
+ 'entity_type ' ,
604
+ \Magento \Framework \DB \Ddl \Table::TYPE_TEXT ,
605
+ 32
606
+ )
607
+ ->addColumn (
608
+ 'redirect_type ' ,
609
+ \Magento \Framework \DB \Ddl \Table::TYPE_SMALLINT ,
610
+ null ,
611
+ ['unsigned ' => true , 'nullable ' => false , 'default ' => '0 ' ]
612
+ )
613
+ ->addColumn (
614
+ 'product_id ' ,
615
+ \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER
616
+ )
617
+ ->addColumn (
618
+ 'category_id ' ,
619
+ \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER
620
+ )
621
+ ->addColumn (
622
+ 'priority ' ,
623
+ \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER
624
+ )
625
+ ->addIndex (
626
+ 'url_rewrite ' ,
627
+ ['request_path ' , 'store_id ' , 'entity_type ' , 'target_path ' ],
628
+ ['type ' => \Magento \Framework \DB \Adapter \AdapterInterface::INDEX_TYPE_UNIQUE ]
629
+ ) ;
630
+ $ select ->getAdapter ()->createTable ($ table );
551
631
552
632
$ select ->from (
553
- ['r ' => $ this ->source ->addDocumentPrefix ('enterprise_url_rewrite_redirect ' )],
633
+ ['r ' => $ this ->source ->addDocumentPrefix ('enterprise_url_rewrite ' )],
554
634
[
555
635
'id ' => 'IFNULL(NULL, NULL) ' ,
556
- 'request_path ' => 'r.identifier ' ,
636
+ 'request_path ' => 'r.request_path ' ,
557
637
'target_path ' => 'r.target_path ' ,
558
- 'is_system ' => " trim('0') " ,
638
+ 'is_system ' => ' r.is_system ' ,
559
639
'store_id ' => 'r.store_id ' ,
560
640
'entity_type ' => "trim('custom') " ,
561
- 'product_id ' => "r.product_id " ,
562
- 'category_id ' => "r.category_id " ,
563
- 'priority ' => "trim('1') "
641
+ 'redirect_type ' => "trim('0') " ,
642
+ 'product_id ' => "trim('0') " ,
643
+ 'category_id ' => "trim('0') " ,
644
+ 'priority ' => "trim('2') "
564
645
]
565
646
);
566
- $ query = $ select ->insertFromSelect ($ this ->source ->addDocumentPrefix ($ this ->tableName ));
647
+ $ query = $ select ->where ('`r`.`entity_type` = 1 ' )
648
+ ->insertFromSelect ($ this ->source ->addDocumentPrefix ($ this ->tableName ));
567
649
$ select ->getAdapter ()->query ($ query );
568
650
569
651
$ select = $ adapter ->getSelect ();
570
652
$ select ->from (
571
- ['r ' => $ this ->source ->addDocumentPrefix ('enterprise_url_rewrite ' )],
653
+ ['r ' => $ this ->source ->addDocumentPrefix ('enterprise_url_rewrite_redirect ' )],
572
654
[
573
655
'id ' => 'IFNULL(NULL, NULL) ' ,
574
- 'request_path ' => 'r.request_path ' ,
656
+ 'request_path ' => 'r.identifier ' ,
575
657
'target_path ' => 'r.target_path ' ,
576
- 'is_system ' => ' r.is_system ' ,
658
+ 'is_system ' => " trim('0') " ,
577
659
'store_id ' => 'r.store_id ' ,
578
660
'entity_type ' => "trim('custom') " ,
579
- 'product_id ' => "trim('0') " ,
580
- 'category_id ' => "trim('0') " ,
581
- 'priority ' => "trim('2') "
661
+ 'redirect_type ' => "(SELECT CASE r.options WHEN 'RP' THEN 301 WHEN 'R' THEN 302 ELSE 0 END) " ,
662
+ 'product_id ' => "r.product_id " ,
663
+ 'category_id ' => "r.category_id " ,
664
+ 'priority ' => "trim('1') "
582
665
]
583
666
);
584
- $ query = $ select ->where ('`r`.`entity_type` = 1 ' )
585
- ->insertFromSelect ($ this ->source ->addDocumentPrefix ($ this ->tableName ));
667
+ $ query = $ select ->insertFromSelect ($ this ->source ->addDocumentPrefix ($ this ->tableName ));
586
668
$ select ->getAdapter ()->query ($ query );
587
669
588
670
$ select = $ adapter ->getSelect ();
@@ -595,6 +677,7 @@ protected function initTemporaryTable()
595
677
'is_system ' => 'r.is_system ' ,
596
678
'store_id ' => 'r.store_id ' ,
597
679
'entity_type ' => "trim('category') " ,
680
+ 'redirect_type ' => "trim('0') " ,
598
681
'product_id ' => "trim('0') " ,
599
682
'category_id ' => "c.entity_id " ,
600
683
'priority ' => "trim('3') "
@@ -647,6 +730,7 @@ protected function initTemporaryTable()
647
730
'is_system ' => 'r.is_system ' ,
648
731
'store_id ' => 's.store_id ' ,
649
732
'entity_type ' => "trim('product') " ,
733
+ 'redirect_type ' => "trim('0') " ,
650
734
'product_id ' => "p.entity_id " ,
651
735
'category_id ' => "c.category_id " ,
652
736
'priority ' => "trim('4') "
@@ -698,6 +782,7 @@ protected function initTemporaryTable()
698
782
'is_system ' => 'r.is_system ' ,
699
783
'store_id ' => 's.store_id ' ,
700
784
'entity_type ' => "trim('product') " ,
785
+ 'redirect_type ' => "trim('0') " ,
701
786
'product_id ' => "p.entity_id " ,
702
787
'category_id ' => "trim('0') " ,
703
788
'priority ' => "trim('4') "
@@ -740,6 +825,7 @@ protected function initTemporaryTable()
740
825
'is_system ' => 's.is_system ' ,
741
826
'store_id ' => 's.store_id ' ,
742
827
'entity_type ' => "trim('product') " ,
828
+ 'redirect_type ' => "trim('0') " ,
743
829
'product_id ' => "p.entity_id " ,
744
830
'category_id ' => "c.category_id " ,
745
831
'priority ' => "trim('4') "
@@ -780,6 +866,7 @@ protected function initTemporaryTable()
780
866
'is_system ' => 's.is_system ' ,
781
867
'store_id ' => 's.store_id ' ,
782
868
'entity_type ' => "trim('product') " ,
869
+ 'redirect_type ' => "trim('0') " ,
783
870
'product_id ' => "p.entity_id " ,
784
871
'category_id ' => "trim('0') " ,
785
872
'priority ' => "trim('4') "
0 commit comments