Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MigrateDataBetweenShards.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
16 
22 {
26  const SKIP_MIGRATION_DATA_FLAG = 'skip-migration';
27 
31  private $resourceConnection;
32 
36  private $selectGenerator;
37 
44  public function __construct(
45  ResourceConnection $resourceConnection,
46  SelectGenerator $selectGenerator
47  ) {
48  $this->resourceConnection = $resourceConnection;
49  $this->selectGenerator = $selectGenerator;
50  }
51 
57  public function isApplicable(string $statement) : bool
58  {
59  return $statement !== self::SKIP_MIGRATION_DATA_FLAG;
60  }
61 
65  public function getCallback(ElementHistory $elementHistory) : callable
66  {
68  $newTable = $elementHistory->getNew();
70  $oldTable = $elementHistory->getOld();
71  $that = $this;
72 
73  return function () use ($newTable, $oldTable, $that) {
74  $firstConnection = $that->resourceConnection->getConnection($oldTable->getResource());
75  $secondConnection = $that->resourceConnection->getConnection($newTable->getResource());
76  $select = $firstConnection->select()->from($oldTable->getName());
77 
78  foreach ($this->selectGenerator->generator($select, $oldTable->getResource()) as $data) {
79  if (count($data)) {
80  $columns = array_keys($data[0]);
81  $secondConnection->insertArray($newTable->getName(), $columns, $data);
82  }
83  }
84  };
85  }
86 }
$columns
Definition: default.phtml:15
__construct(ResourceConnection $resourceConnection, SelectGenerator $selectGenerator)