/**
  * @param int $valueId
  * @return int
  */
 protected function getEntityId($valueId)
 {
     /** @var Mysql $adapter */
     $adapter = $this->source->getAdapter();
     $query = $adapter->getSelect()->from(['mg' => $this->source->addDocumentPrefix($this->mediaGalleryDocument)], [$this->field])->where("mg.{$this->valueIdField} = ?", $valueId);
     return (int) $query->getAdapter()->fetchOne($query);
 }
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     do {
         /** @var StepList $steps */
         $steps = $this->stepListFactory->create(['mode' => 'delta']);
         /**
          * @var string $stepName
          * @var StageInterface[] $step
          */
         foreach ($steps->getSteps() as $stepName => $step) {
             if (empty($step['delta'])) {
                 continue;
             }
             $this->runDelta($step, $stepName);
             if (!empty($step['volume'])) {
                 $this->runVolume($step, $stepName);
             }
         }
         $deltaLogs = $this->groupsReader->getGroups();
         foreach ($deltaLogs as $deltaDocuments) {
             foreach (array_keys($deltaDocuments) as $documentName) {
                 /** @var Mysql $adapter */
                 $adapter = $this->source->getAdapter();
                 $adapter->deleteProcessedRecords($this->source->addDocumentPrefix($this->source->getDeltaLogName($documentName)));
             }
         }
         $this->logger->info('Migration completed successfully');
         if ($this->autoRestart) {
             $this->logger->info("Automatic restart in {$this->autoRestart} sec. Use CTRL-C to abort");
             sleep($this->autoRestart);
         }
     } while ($this->autoRestart);
     return true;
 }
 /**
  * @return array
  */
 public function getStoreIds()
 {
     /** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
     $adapter = $this->source->getAdapter();
     $query = $adapter->getSelect()->from($this->source->addDocumentPrefix($this->storeTable), ['store_id']);
     return $query->getAdapter()->fetchCol($query);
 }
 /**
  * Get attribute set IDs for entity type 'catalog_product'
  * @return array
  */
 protected function getProductAttributeSets()
 {
     if (empty($this->productAttributeSets)) {
         /** @var Mysql $adapter */
         $adapter = $this->source->getAdapter();
         $query = $adapter->getSelect()->from(['as' => $this->source->addDocumentPrefix('eav_attribute_set')], ['attribute_set_id'])->join(['et' => $this->source->addDocumentPrefix('eav_entity_type')], 'et.entity_type_id = as.entity_type_id', [])->where('et.entity_type_code = ?', 'catalog_product');
         $this->productAttributeSets = array_flip($query->getAdapter()->fetchCol($query));
     }
     return $this->productAttributeSets;
 }
 /**
  * @param string $eavAttribute
  * @return \Magento\Framework\DB\Select
  */
 protected function getEavAttributeSelect($eavAttribute)
 {
     /** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
     $adapter = $this->source->getAdapter();
     $select = $adapter->getSelect();
     $tables = [];
     foreach (array_keys($this->getDocumentList()) as $sourceDocument) {
         $tables[] = $this->source->addDocumentPrefix($sourceDocument);
     }
     $select->from($tables)->where($eavAttribute . ' is not null');
     return $select;
 }
 /**
  * @param string $documentName
  * @param string $idKey
  * @return void
  */
 protected function processChangedRecords($documentName, $idKey)
 {
     $items = $this->source->getChangedRecords($documentName, $idKey);
     if (empty($items)) {
         return;
     }
     if (!$this->eolOnce) {
         $this->eolOnce = true;
         echo PHP_EOL;
     }
     $destinationName = $this->mapReader->getDocumentMap($documentName, MapInterface::TYPE_SOURCE);
     $sourceDocument = $this->source->getDocument($documentName);
     $destDocument = $this->destination->getDocument($destinationName);
     $recordTransformer = $this->getRecordTransformer($sourceDocument, $destDocument);
     do {
         $destinationRecords = $destDocument->getRecords();
         $ids = [];
         foreach ($items as $data) {
             echo '.';
             $ids[] = $data[$idKey];
             $this->transformData($data, $sourceDocument, $destDocument, $recordTransformer, $destinationRecords);
         }
         $this->destination->updateChangedRecords($destinationName, $destinationRecords);
         $documentNameDelta = $this->source->getDeltaLogName($documentName);
         $documentNameDelta = $this->source->addDocumentPrefix($documentNameDelta);
         $this->markRecordsProcessed($documentNameDelta, $idKey, $ids);
     } while (!empty($items = $this->source->getChangedRecords($documentName, $idKey)));
 }
 /**
  * @param Record $recordToHandle
  * @param Record $oppositeRecord
  * @return mixed
  */
 public function handle(Record $recordToHandle, Record $oppositeRecord)
 {
     $this->validate($recordToHandle);
     $sourcePatterns = [];
     $destinationPatters = [];
     foreach ($this->source->getDocumentList() as $document) {
         $destDocumentName = $this->getDestDocumentName($document);
         if ($destDocumentName === false) {
             continue;
         }
         $sourcePatterns[] = sprintf('`%s`', $this->source->addDocumentPrefix($document));
         $destinationPatters[] = sprintf('`%s`', $this->destination->addDocumentPrefix($destDocumentName));
     }
     $newValue = str_replace($sourcePatterns, $destinationPatters, $recordToHandle->getValue($this->field));
     $recordToHandle->setValue($this->field, $newValue);
 }
 /**
  * @param int $storeId
  * @return string
  */
 protected function getIncrementPrefix($storeId)
 {
     /** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
     $adapter = $this->source->getAdapter();
     $select = $adapter->getSelect()->from(['cs' => $this->source->addDocumentPrefix($this->storeTable)], ['store_id' => 'csg.default_store_id'])->join(['csg' => $this->source->addDocumentPrefix($this->storeGroupTable)], 'csg.group_id = cs.group_id', [])->where('cs.store_id = ?', $storeId);
     return $select->getAdapter()->fetchOne($select);
 }
 /**
  * @return \Magento\Framework\DB\Select
  */
 public function getLogDataSelect()
 {
     $fields = ['visitor_id' => 'lv.visitor_id', 'customer_id' => 'lc.customer_id', 'session_id' => 'lv.session_id', 'last_visit_at' => 'lv.last_visit_at'];
     /** @var \Magento\Framework\DB\Select $select */
     $select = $this->sourceAdapter->getSelect();
     $select->from(['lv' => $this->source->addDocumentPrefix('log_visitor')], $fields)->joinLeft(['lc' => $this->source->addDocumentPrefix('log_customer')], 'lv.visitor_id = lc.visitor_id', [])->group('lv.visitor_id')->order('lv.visitor_id');
     return $select;
 }
 /**
  * @param bool $countVolume
  * @return int
  */
 protected function countCmsPageRewrites($countVolume = false)
 {
     if (!$countVolume) {
         return 1;
     }
     /** @var \Magento\Framework\Db\Select $select */
     $select = $this->source->getAdapter()->getSelect();
     $select->from(['cp' => $this->source->addDocumentPrefix($this->cmsPageTableName)], [new \Zend_Db_Expr('COUNT(*)')])->joinLeft(['cps' => $this->source->addDocumentPrefix($this->cmsPageStoreTableName)], 'cps.page_id = cp.page_id', []);
     return $select->getAdapter()->fetchOne($select);
 }
 /**
  * @param string $sourceGridDocumentName
  * @return \Magento\Framework\DB\Select
  */
 protected function getEntityIdsSelect($sourceGridDocumentName)
 {
     /** @var \Migration\ResourceModel\Adapter\Mysql $adapter */
     $adapter = $this->source->getAdapter();
     /** @var \Magento\Framework\DB\Select $select */
     $select = $adapter->getSelect();
     $schema = $this->config->getSource()['database']['name'];
     $select->from($this->source->addDocumentPrefix($sourceGridDocumentName), 'entity_id', $schema);
     return $select;
 }
 /**
  * Fulfill temporary table with redirects
  *
  * @param \Migration\ResourceModel\Adapter\Mysql $adapter
  * @return void
  */
 protected function collectRedirects(\Migration\ResourceModel\Adapter\Mysql $adapter)
 {
     $select = $adapter->getSelect();
     $select->from(['r' => $this->source->addDocumentPrefix('enterprise_url_rewrite')], ['id' => 'IFNULL(NULL, NULL)', 'request_path' => 'r.request_path', 'target_path' => 'r.target_path', 'is_system' => 'r.is_system', 'store_id' => "s.store_id", 'entity_type' => "trim('custom')", 'redirect_type' => "(SELECT CASE eurr.options WHEN 'RP' THEN 301 WHEN 'R' THEN 302 ELSE 0 END)", 'product_id' => "trim('0')", 'category_id' => "trim('0')", 'cms_page_id' => "trim('0')", 'priority' => "trim('2')"]);
     $select->join(['eurrr' => $this->source->addDocumentPrefix('enterprise_url_rewrite_redirect_rewrite')], 'eurrr.url_rewrite_id = r.url_rewrite_id', []);
     $select->join(['eurr' => $this->source->addDocumentPrefix('enterprise_url_rewrite_redirect')], 'eurrr.redirect_id = eurr.redirect_id', []);
     $select->join(['s' => $this->source->addDocumentPrefix('core_store')], 's.store_id > 0', []);
     $query = $select->insertFromSelect($this->source->addDocumentPrefix($this->tableName));
     $select->getAdapter()->query($query);
 }
 /**
  * @return array
  */
 protected function getAttributeIds()
 {
     if (empty($this->attributeIds)) {
         /** @var Mysql $adapter */
         $adapter = $this->source->getAdapter();
         $query = $adapter->getSelect()->from($this->source->addDocumentPrefix('eav_attribute'), ['attribute_id'])->where('attribute_code = ?', $this->attributeCode);
         $this->attributeIds = array_flip($query->getAdapter()->fetchCol($query));
     }
     return $this->attributeIds;
 }
 /**
  * @param string $attributeType
  * @return void
  */
 protected function initEavAttributes($attributeType)
 {
     if (isset($this->eavAttributes[$attributeType])) {
         return;
     }
     /** @var Mysql $adapter */
     $adapter = $this->source->getAdapter();
     $query = $adapter->getSelect()->from(['et' => $this->source->addDocumentPrefix('eav_entity_type')], [])->join(['ea' => $this->source->addDocumentPrefix('eav_attribute')], 'et.entity_type_id = ea.entity_type_id', ['attribute_id', 'backend_type', 'attribute_code', 'entity_type_id'])->where('et.entity_type_code = ?', $attributeType);
     $attributes = $query->getAdapter()->fetchAll($query);
     foreach ($attributes as $attribute) {
         $this->eavAttributes[$attributeType][$attribute['attribute_code']] = $attribute;
         $this->eavAttributes[$attributeType][$attribute['attribute_id']] = $attribute;
     }
 }
 /**
  * @param Document $sourceDocument
  * @param Document $destinationDocument
  * @return bool
  */
 protected function isCopiedDirectly(Document $sourceDocument, Document $destinationDocument)
 {
     if (!$this->copyDirectly) {
         return;
     }
     $result = true;
     $schema = $this->config->getSource()['database']['name'];
     /** @var \Magento\Framework\DB\Select $select */
     $select = $this->source->getAdapter()->getSelect();
     $select->from($this->source->addDocumentPrefix($sourceDocument->getName()), '*', $schema);
     try {
         $this->destination->getAdapter()->insertFromSelect($select, $this->destination->addDocumentPrefix($destinationDocument->getName()), array_keys($sourceDocument->getStructure()->getFields()));
     } catch (\Exception $e) {
         $this->copyDirectly = false;
         $this->logger->error('Document ' . $sourceDocument->getName() . ' can not be copied directly because of error: ' . $e->getMessage());
         $result = false;
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function perform()
 {
     $updateData = $this->helper->getUpdateData();
     $selectData = $this->helper->getSelectData();
     $sourceDocuments = $this->readerGroups->getGroup('source_documents');
     foreach ($sourceDocuments as $sourceDocName => $idKey) {
         if ($this->source->getRecordsCount($this->source->getDeltaLogName($sourceDocName)) == 0) {
             continue;
         }
         $items = $this->source->getChangedRecords($sourceDocName, $idKey, 0, true);
         if (empty($items)) {
             continue;
         }
         $this->logger->debug(sprintf('%s has changes', $sourceDocName));
         if (!$this->eolOnce) {
             $this->eolOnce = true;
             echo PHP_EOL;
         }
         $gridIdKey = $updateData[$sourceDocName]['idKey'];
         $page = 1;
         do {
             $ids = [];
             foreach ($items as $data) {
                 echo '.';
                 $ids[] = $data[$gridIdKey];
             }
             foreach ($updateData[$sourceDocName]['methods'] as $method) {
                 echo '.';
                 $destinationDocumentName = $selectData[$method]['destination'];
                 $select = call_user_func_array([$this->data, $method], [$selectData[$method]['columns'], $ids]);
                 $this->destination->getAdapter()->insertFromSelect($select, $this->destination->addDocumentPrefix($destinationDocumentName), [], \Magento\Framework\Db\Adapter\AdapterInterface::INSERT_ON_DUPLICATE);
             }
             $documentNameDelta = $this->source->getDeltaLogName($sourceDocName);
             $documentNameDelta = $this->source->addDocumentPrefix($documentNameDelta);
             $this->markRecordsProcessed($documentNameDelta, $idKey, $ids);
         } while (!empty($items = $this->source->getChangedRecords($sourceDocName, $idKey, $page++)));
     }
     return true;
 }
 /**
  * Run step
  *
  * @return bool
  */
 public function perform()
 {
     /** @var \Migration\ResourceModel\Adapter\Mysql $sourceAdapter */
     $sourceAdapter = $this->source->getAdapter();
     /** @var \Migration\ResourceModel\Adapter\Mysql $destinationAdapter */
     $destinationAdapter = $this->destination->getAdapter();
     $sourceDocuments = array_keys($this->groups->getGroup('source_documents'));
     $this->progress->start($this->getIterationsCount(), LogManager::LOG_LEVEL_INFO);
     foreach ($sourceDocuments as $sourceDocumentName) {
         $destinationDocumentName = $this->map->getDocumentMap($sourceDocumentName, MapInterface::TYPE_SOURCE);
         $sourceTable = $sourceAdapter->getTableDdlCopy($this->source->addDocumentPrefix($sourceDocumentName), $this->destination->addDocumentPrefix($destinationDocumentName));
         $destinationTable = $destinationAdapter->getTableDdlCopy($this->destination->addDocumentPrefix($destinationDocumentName), $this->destination->addDocumentPrefix($destinationDocumentName));
         foreach ($sourceTable->getColumns() as $columnData) {
             $destinationTable->setColumn($columnData);
         }
         $destinationAdapter->createTableByDdl($destinationTable);
         $destinationDocument = $this->destination->getDocument($destinationDocumentName);
         $this->logger->debug('migrating', ['table' => $sourceDocumentName]);
         $pageNumber = 0;
         $this->progress->start($this->source->getRecordsCount($sourceDocumentName), LogManager::LOG_LEVEL_DEBUG);
         while (!empty($sourceRecords = $this->source->getRecords($sourceDocumentName, $pageNumber))) {
             $pageNumber++;
             $recordsToSave = $destinationDocument->getRecords();
             foreach ($sourceRecords as $recordData) {
                 $this->progress->advance(LogManager::LOG_LEVEL_INFO);
                 $this->progress->advance(LogManager::LOG_LEVEL_DEBUG);
                 /** @var Record $destinationRecord */
                 $destinationRecord = $this->recordFactory->create(['document' => $destinationDocument]);
                 $destinationRecord->setData($recordData);
                 $recordsToSave->addRecord($destinationRecord);
             }
             $this->destination->saveRecords($destinationDocument->getName(), $recordsToSave);
         }
         $this->progress->finish(LogManager::LOG_LEVEL_DEBUG);
     }
     $this->progress->finish(LogManager::LOG_LEVEL_INFO);
     return true;
 }
 /**
  * @return string
  */
 protected function getPriceAttributeId()
 {
     $select = $this->sourceAdapter->getSelect();
     $select->from($this->source->addDocumentPrefix('eav_attribute'))->where('attribute_code = ?', 'price');
     return $select->getAdapter()->fetchOne($select);
 }