Skip to content

Commit c1b596e

Browse files
ACPT-820: Add id to Import Start request for Adminhtml
Also added updated_at column to importexport_importdata. When import is done through admin ui, we no longer delete all rows from importexport_importdata before validation - only completed and rows older than 1 day. Also removed dependency on Base64EncodedCsvData class that was removed.
1 parent 272b9b4 commit c1b596e

File tree

9 files changed

+49
-22
lines changed

9 files changed

+49
-22
lines changed

app/code/Magento/ImportExport/Block/Adminhtml/Import/Edit/Form.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ protected function _prepareForm()
211211
);
212212
$fieldsets[$behaviorCode] = $fieldset;
213213
}
214-
215214
// fieldset for file uploading
216215
$fieldset = $form->addFieldset(
217216
'upload_file_fieldset',
@@ -257,11 +256,19 @@ protected function _prepareForm()
257256
),
258257
]
259258
);
259+
$fieldset->addField(
260+
Import::FIELD_IMPORT_IDS,
261+
'hidden',
262+
[
263+
'name' => Import::FIELD_IMPORT_IDS,
264+
'label' => __('Import id'),
265+
'title' => __('Import id'),
266+
'value' => '',
267+
]
268+
);
260269
$fieldsets['upload'] = $fieldset;
261-
262270
$form->setUseContainer(true);
263271
$this->setForm($form);
264-
265272
return parent::_prepareForm();
266273
}
267274

app/code/Magento/ImportExport/Controller/Adminhtml/Import/Validate.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public function execute()
4242
//phpcs:disable Magento2.Security.Superglobal
4343
if ($data) {
4444
// common actions
45-
$resultBlock->addAction(
46-
'show',
47-
'import_validation_container'
48-
);
49-
45+
$resultBlock->addAction('show', 'import_validation_container');
5046
$import = $this->getImport()->setData($data);
5147
try {
5248
$source = $import->uploadFileAndGetSource();
5349
$this->processValidationResult($import->validateSource($source), $resultBlock);
50+
$ids = $import->getValidatedIds();
51+
if (count($ids) > 0) {
52+
$resultBlock->addAction('value', Import::FIELD_IMPORT_IDS, $ids);
53+
}
5454
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5555
$resultBlock->addError($e->getMessage());
5656
} catch (\Exception $e) {
@@ -117,7 +117,6 @@ private function processValidationResult($validationResult, $resultBlock)
117117
* Provides import model.
118118
*
119119
* @return Import
120-
* @deprecated 100.1.0
121120
*/
122121
private function getImport()
123122
{

app/code/Magento/ImportExport/Model/Import.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class Import extends AbstractModel
9898
*/
9999
public const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';
100100

101+
/**
102+
* Id of the `importexport_importdata` row after validation.
103+
*/
104+
public const FIELD_IMPORT_IDS = '_import_ids';
105+
101106
/**
102107
* Allow multiple values wrapping in double quotes for additional attributes.
103108
*/
@@ -476,6 +481,12 @@ public function getWorkingDir()
476481
public function importSource()
477482
{
478483
$ids = $this->_getEntityAdapter()->getIds();
484+
if (empty($ids)) {
485+
$idsFromPostData = $this->getData(self::FIELD_IMPORT_IDS);
486+
if (null !== $idsFromPostData && '' !== $idsFromPostData) {
487+
$ids = explode(",", $idsFromPostData);;
488+
}
489+
}
479490
$this->setData('entity', $this->getDataSourceModel()->getEntityTypeCode($ids));
480491
$this->setData('behavior', $this->getDataSourceModel()->getBehavior($ids));
481492

@@ -852,4 +863,14 @@ public function getDeletedItemsCount()
852863
{
853864
return $this->_getEntityAdapter()->getDeletedItemsCount();
854865
}
866+
867+
/**
868+
* Retrieve Ids of Validated Rows
869+
*
870+
* @return int[]
871+
*/
872+
public function getValidatedIds() : array
873+
{
874+
return $this->_getEntityAdapter()->getIds() ?? [];
875+
}
855876
}

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,7 @@ protected function _saveValidatedBunches()
421421
$startNewBunch = false;
422422

423423
$source->rewind();
424-
if (!$source instanceof Base64EncodedCsvData) {
425-
$this->_dataSourceModel->cleanBunches();
426-
}
424+
$this->_dataSourceModel->cleanProcessedBunches();
427425
$mainAttributeCode = $this->getMasterAttributeCode();
428426

429427
while ($source->valid() || count($bunchRows) || isset($entityGroup)) {

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,7 @@ protected function _saveValidatedBunches()
395395
$skuSet = [];
396396

397397
$source->rewind();
398-
if (!$source instanceof Base64EncodedCsvData) {
399-
$this->_dataSourceModel->cleanBunches();
400-
}
401-
398+
$this->_dataSourceModel->cleanProcessedBunches();
402399
while ($source->valid() || $bunchRows) {
403400
if ($startNewBunch || !$source->valid()) {
404401
$this->ids[] =

app/code/Magento/ImportExport/Model/ResourceModel/Import/Data.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ public function cleanProcessedBunches()
125125
{
126126
$this->getConnection()->delete(
127127
$this->getMainTable(),
128-
[
129-
'is_processed' => '1'
130-
]
128+
'is_processed = 1 OR TIMESTAMPADD(DAY, 1, updated_at) < NOW() '
131129
);
132130
}
133131

app/code/Magento/ImportExport/etc/db_schema.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<column xsi:type="varchar" name="behavior" nullable="false" length="10" default="append" comment="Behavior"/>
1414
<column xsi:type="longtext" name="data" nullable="true" comment="Data"/>
1515
<column xsi:type="boolean" name="is_processed" nullable="false" default="true" comment="Is Row Processed"/>
16+
<column xsi:type="timestamp" name="updated_at" on_update="true" nullable="false" default="CURRENT_TIMESTAMP"
17+
comment="timestamp of last update"/>
1618
<constraint xsi:type="primary" referenceId="PRIMARY">
1719
<column name="id"/>
1820
</constraint>

app/code/Magento/ImportExport/etc/db_schema_whitelist.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"entity": true,
66
"behavior": true,
77
"data": true,
8-
"is_processed": true
8+
"is_processed": true,
9+
"updated_at": true
910
},
1011
"constraint": {
1112
"PRIMARY": true

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest/ProductOptionsTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ public function testSaveCustomOptions(string $importFile, string $sku, int $expe
125125

126126
// Make sure that after importing existing options again, option IDs and option value IDs are not changed
127127
$customOptionValues = $this->getCustomOptionValues($sku);
128-
$this->createImportModel($pathToFile)->importData();
128+
$importModel = $this->createImportModel($pathToFile);
129+
$importModel->validateData();
130+
$importModel->importData();
129131
$this->assertEquals($customOptionValues, $this->getCustomOptionValues($sku));
130132

131133
// Cleanup imported products
@@ -222,7 +224,9 @@ public function testSaveCustomOptionsWithMultipleStoreViews()
222224

223225
// Make sure that after importing existing options again, option IDs and option value IDs are not changed
224226
$customOptionValues = $this->getCustomOptionValues($sku);
225-
$this->createImportModel($pathToFile)->importData();
227+
$importModel = $this->createImportModel($pathToFile);
228+
$importModel->validateData();
229+
$importModel->importData();
226230
$this->assertEquals(
227231
$customOptionValues,
228232
$this->getCustomOptionValues($sku),

0 commit comments

Comments
 (0)