Skip to content

Commit ac3f176

Browse files
authored
MDEE-460: Exporter doesn't restore the deleted option (#274)
* MDEE-460: Exporter doesn't restore the deleted option
1 parent 126d3c3 commit ac3f176

File tree

11 files changed

+182
-193
lines changed

11 files changed

+182
-193
lines changed

DataExporter/Model/Indexer/FeedIndexProcessorCreateUpdateDelete.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\DataExporter\Model\Indexer;
99

1010
use Magento\DataExporter\Export\Processor as ExportProcessor;
11+
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
1112
use Magento\Framework\App\ResourceConnection;
1213

1314
/**
@@ -19,19 +20,23 @@ class FeedIndexProcessorCreateUpdateDelete extends FeedIndexProcessorCreateUpdat
1920
* @var MarkRemovedEntitiesInterface
2021
*/
2122
private $markRemovedEntities;
23+
private CommerceDataExportLoggerInterface $logger;
2224

2325
/**
2426
* @param ResourceConnection $resourceConnection
2527
* @param ExportProcessor $exportProcessor
2628
* @param MarkRemovedEntitiesInterface $markRemovedEntities
29+
* @param CommerceDataExportLoggerInterface $logger
2730
*/
2831
public function __construct(
2932
ResourceConnection $resourceConnection,
3033
ExportProcessor $exportProcessor,
31-
MarkRemovedEntitiesInterface $markRemovedEntities
34+
MarkRemovedEntitiesInterface $markRemovedEntities,
35+
CommerceDataExportLoggerInterface $logger
3236
) {
3337
parent::__construct($resourceConnection, $exportProcessor);
3438
$this->markRemovedEntities = $markRemovedEntities;
39+
$this->logger = $logger;
3540
}
3641

3742
/**
@@ -45,6 +50,14 @@ public function __construct(
4550
public function partialReindex(FeedIndexMetadata $metadata, DataSerializerInterface $serializer, EntityIdsProviderInterface $idsProvider, array $ids = []): void
4651
{
4752
parent::partialReindex($metadata, $serializer, $idsProvider, $ids);
48-
$this->markRemovedEntities->execute($ids, $metadata);
53+
54+
try {
55+
$this->markRemovedEntities->execute($ids, $metadata);
56+
} catch (\Throwable $e) {
57+
$this->logger->error(
58+
sprintf("Cannot delete feed items. product ids: %s", implode(', ', $ids)),
59+
['exception' => $e]
60+
);
61+
}
4962
}
5063
}

DataExporter/Model/Indexer/MarkRemovedEntities.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@
1515
*/
1616
class MarkRemovedEntities implements MarkRemovedEntitiesInterface
1717
{
18-
/**
19-
* @var ResourceConnection
20-
*/
21-
private $resourceConnection;
22-
23-
/**
24-
* @var MarkRemovedEntitiesQuery
25-
*/
26-
private $markRemovedEntitiesQuery;
18+
private ResourceConnection $resourceConnection;
19+
private MarkRemovedEntitiesQuery $markRemovedEntitiesQuery;
2720

2821
/**
2922
* @param ResourceConnection $resourceConnection
@@ -43,13 +36,14 @@ public function __construct(
4336
public function execute(array $ids, FeedIndexMetadata $metadata): void
4437
{
4538
$select = $this->markRemovedEntitiesQuery->getQuery($ids, $metadata);
46-
$connection = $this->resourceConnection->getConnection();
4739

48-
$update = $connection->updateFromSelect(
49-
$select,
50-
['f' => $this->resourceConnection->getTableName($metadata->getFeedTableName())]
51-
);
40+
// convert select-object to sql-string with staging future
41+
$sqlSelect = $select->assemble();
42+
43+
// make "update from select" statement
44+
$sqlUpdate = preg_replace('/SELECT .*? FROM/', 'UPDATE', $sqlSelect);
45+
$sqlUpdate = str_replace("WHERE", 'SET `f`.`is_deleted` = 1 WHERE', $sqlUpdate);
5246

53-
$connection->query($update);
47+
$this->resourceConnection->getConnection()->query($sqlUpdate);
5448
}
5549
}

DataExporter/Model/Query/MarkRemovedEntitiesQuery.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
*/
1717
class MarkRemovedEntitiesQuery
1818
{
19-
/**
20-
* @var ResourceConnection
21-
*/
22-
private $resourceConnection;
19+
private ResourceConnection $resourceConnection;
2320

2421
/**
2522
* @param ResourceConnection $resourceConnection
@@ -40,15 +37,16 @@ public function __construct(ResourceConnection $resourceConnection)
4037
public function getQuery(array $ids, FeedIndexMetadata $metadata): Select
4138
{
4239
$connection = $this->resourceConnection->getConnection();
43-
$select = $connection->select()
40+
return $connection->select()
41+
->from(
42+
['f' => $this->resourceConnection->getTableName($metadata->getFeedTableName())]
43+
)
4444
->joinLeft(
4545
['s' => $this->resourceConnection->getTableName($metadata->getSourceTableName())],
4646
\sprintf('f.%s = s.%s', $metadata->getFeedTableField(), $metadata->getSourceTableField()),
47-
['is_deleted' => new \Zend_Db_Expr('1')]
47+
[]
4848
)
4949
->where(\sprintf('f.%s IN (?)', $metadata->getFeedTableField()), $ids)
5050
->where(\sprintf('s.%s IS NULL', $metadata->getSourceTableField()));
51-
52-
return $select;
5351
}
5452
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductPriceDataExporter\Model\Indexer;
9+
10+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
11+
use Magento\DataExporter\Model\Indexer\MarkRemovedEntitiesInterface;
12+
use Magento\DataExporter\Model\Query\MarkRemovedEntitiesQuery;
13+
use Magento\Framework\App\ResourceConnection;
14+
15+
/**
16+
* Action responsible for marking entities as removed
17+
*/
18+
class MarkRemovedEntities implements MarkRemovedEntitiesInterface
19+
{
20+
private ResourceConnection $resourceConnection;
21+
private MarkRemovedEntitiesQuery $markRemovedEntitiesQuery;
22+
23+
/**
24+
* @param ResourceConnection $resourceConnection
25+
* @param MarkRemovedEntitiesQuery $markRemovedEntitiesQuery
26+
*/
27+
public function __construct(
28+
ResourceConnection $resourceConnection,
29+
MarkRemovedEntitiesQuery $markRemovedEntitiesQuery
30+
) {
31+
$this->resourceConnection = $resourceConnection;
32+
$this->markRemovedEntitiesQuery = $markRemovedEntitiesQuery;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function execute(array $ids, FeedIndexMetadata $metadata): void
39+
{
40+
$select = $this->markRemovedEntitiesQuery->getQuery($ids, $metadata);
41+
$connection = $this->resourceConnection->getConnection();
42+
43+
$connection->query(
44+
$connection->deleteFromSelect($select, 'feed')
45+
);
46+
}
47+
}

ProductPriceDataExporter/Model/Query/MarkRemovedEntitiesQuery.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\App\ResourceConnection;
1414
use Magento\Framework\DB\Select;
1515
use Magento\Framework\EntityManager\MetadataPool;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\DataExporter\Model\Query\MarkRemovedEntitiesQuery as DefaultMarkRemovedEntitiesQuery;
1618

1719
/**
1820
* Select price feed items that should be removed when product:
@@ -23,7 +25,7 @@
2325
* Remove price for specific customer group handled in provider:
2426
* @see \Magento\ProductPriceDataExporter\Model\Provider\DeleteFeedItems
2527
*/
26-
class MarkRemovedEntitiesQuery
28+
class MarkRemovedEntitiesQuery extends DefaultMarkRemovedEntitiesQuery
2729
{
2830
private const STATUS_ATTRIBUTE_CODE = "status";
2931

@@ -57,6 +59,7 @@ public function __construct(
5759
$this->resourceConnection = $resourceConnection;
5860
$this->eavConfig = $eavConfig;
5961
$this->metadataPool = $metadataPool;
62+
parent::__construct($resourceConnection);
6063
}
6164

6265
/**
@@ -66,7 +69,7 @@ public function __construct(
6669
* @param FeedIndexMetadata $metadata
6770
*
6871
* @return Select
69-
* @throws \Magento\Framework\Exception\LocalizedException
72+
* @throws LocalizedException
7073
*/
7174
public function getQuery(array $ids, FeedIndexMetadata $metadata): Select
7275
{
@@ -75,13 +78,13 @@ public function getQuery(array $ids, FeedIndexMetadata $metadata): Select
7578
$catalogProductTable = $this->resourceConnection->getTableName($metadata->getSourceTableName());
7679
$productEntityJoinField = $connection->getAutoIncrementField($catalogProductTable);
7780
return $connection->select()
78-
->from(['feed' => $this->resourceConnection->getTableName($metadata->getFeedTableName())])
81+
->from(
82+
['feed' => $this->resourceConnection->getTableName($metadata->getFeedTableName())]
83+
)
7984
->joinLeft(
8085
['p' => $catalogProductTable],
8186
\sprintf('feed.%s = p.%s', $metadata->getFeedTableField(), $metadata->getSourceTableField()),
82-
[
83-
'is_deleted' => new \Zend_Db_Expr('1')
84-
]
87+
[]
8588
)
8689
->joinLeft(
8790
['w' => $this->resourceConnection->getTableName('store_website')],

ProductPriceDataExporter/Plugin/DeleteFeedItems.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

ProductPriceDataExporter/etc/di.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@
3131
</argument>
3232
</arguments>
3333
</virtualType>
34+
<type name="Magento\ProductPriceDataExporter\Model\Indexer\MarkRemovedEntities">
35+
<arguments>
36+
<argument name="markRemovedEntitiesQuery" xsi:type="object">Magento\ProductPriceDataExporter\Model\Query\MarkRemovedEntitiesQuery</argument>
37+
</arguments>
38+
</type>
39+
<virtualType name="Magento\ProductPriceDataExporter\Model\Indexer\FeedIndexProcessorCreateUpdateDelete" type="Magento\DataExporter\Model\Indexer\FeedIndexProcessorCreateUpdateDelete">
40+
<arguments>
41+
<argument name="markRemovedEntities" xsi:type="object">Magento\ProductPriceDataExporter\Model\Indexer\MarkRemovedEntities</argument>
42+
</arguments>
43+
</virtualType>
3444
<virtualType name="Magento\ProductPriceDataExporter\Model\Indexer\ProductPriceFeedIndexer" type="Magento\DataExporter\Model\Indexer\FeedIndexer">
3545
<arguments>
3646
<argument name="feedIndexMetadata" xsi:type="object">Magento\ProductPriceDataExporter\Model\Indexer\ProductPriceFeedIndexMetadata</argument>
3747
<argument name="serializer" xsi:type="object">Magento\ProductPriceDataExporter\Model\Indexer\ProductPriceDataSerializer</argument>
48+
<argument name="processor" xsi:type="object">Magento\ProductPriceDataExporter\Model\Indexer\FeedIndexProcessorCreateUpdateDelete</argument>
3849
</arguments>
3950
</virtualType>
4051
<virtualType name="Magento\ProductPriceDataExporter\Model\ProductPriceFeed" type="Magento\DataExporter\Model\Feed">
@@ -49,8 +60,4 @@
4960
</argument>
5061
</arguments>
5162
</type>
52-
53-
<type name="Magento\DataExporter\Model\Indexer\MarkRemovedEntities">
54-
<plugin name="exporter-delete-feed-items-when-product-deleted" type="Magento\ProductPriceDataExporter\Plugin\DeleteFeedItems"/>
55-
</type>
5663
</config>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ProductReviewDataExporter\Model\Query;
9+
10+
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
11+
use Magento\DataExporter\Model\Query\MarkRemovedEntitiesQuery as MainQuery;
12+
use Magento\DataExporter\Model\Query\MarkRemovedEntitiesQuery as DefaultMarkRemovedEntitiesQuery;
13+
use Magento\Framework\App\ResourceConnection;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Review\Model\Review;
16+
use Zend_Db;
17+
18+
/**
19+
* Mark removed entities select query provider
20+
*/
21+
class MarkRemovedEntitiesQuery extends DefaultMarkRemovedEntitiesQuery
22+
{
23+
/**
24+
* Entities feed names
25+
*/
26+
private const FEED_NAME_REVIEWS = 'reviews';
27+
private const FEED_NAME_RATING_METADATA = 'ratingMetadata';
28+
29+
private MainQuery $mainQuery;
30+
31+
/**
32+
* @param ResourceConnection $resourceConnection
33+
* @param MainQuery $mainQuery
34+
*/
35+
public function __construct(ResourceConnection $resourceConnection, MainQuery $mainQuery)
36+
{
37+
$this->mainQuery = $mainQuery;
38+
parent::__construct($resourceConnection);
39+
}
40+
41+
/**
42+
* Get select query for marking removed entities
43+
*
44+
* @param array $ids
45+
* @param FeedIndexMetadata $metadata
46+
*
47+
* @return Select
48+
*/
49+
public function getQuery(array $ids, FeedIndexMetadata $metadata): Select
50+
{
51+
$select = $this->mainQuery->getQuery($ids, $metadata);
52+
53+
if ($metadata->getFeedName() === self::FEED_NAME_REVIEWS) {
54+
$select->where('s.status_id != ?', Review::STATUS_APPROVED, Zend_Db::INT_TYPE);
55+
} elseif ($metadata->getFeedName() === self::FEED_NAME_RATING_METADATA) {
56+
$select->where('s.is_active != ?', 1, Zend_Db::INT_TYPE);
57+
}
58+
59+
return $select;
60+
}
61+
}

0 commit comments

Comments
 (0)