Skip to content

Commit

Permalink
Fix marketplace test issue (#353)
Browse files Browse the repository at this point in the history
* add skip_entity_id_binding Magento hack to workaround test on marketplace

* clone collection to avoid duplicates and walk delete items from original

* remove onDelete=cascade

* increase version

* doc

* update doc
  • Loading branch information
yuloma authored Nov 4, 2024
1 parent 4a94cc3 commit 0aeaa7b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
42 changes: 36 additions & 6 deletions Cron/Processor.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
<?php
/**
* By removing the UNIQUE restriction from our table, it happens that during imports or product generation with fixtures,
* duplicate elements are inserted into the table. These operations are handled within transactions and it seems that our code
* is currently not able to determine if the element is already added when it is within a transaction.
*
* As a result, when executing the Processor for the Update on Save, we receive more elements than we actually need to process,
* because many of them are duplicates.
*
* So what we do here is clone the original collection that has all the ungrouped elements.
* Then, a group is applied to the copy and thus we ensure that we process unique elements.
* At the end, the elements of the original collection are deleted from the table.
*
* This logic is applied in the same way in the others two functions updateItems and deleteItems.
*/

declare(strict_types=1);

namespace Doofinder\Feed\Cron;

use Doofinder\Feed\Api\Data\ChangedItemInterface;
use Doofinder\Feed\Helper\Item as ItemHelper;
use Doofinder\Feed\Helper\StoreConfig;
use Doofinder\Feed\Model\ChangedItem\DocumentsProvider;
Expand Down Expand Up @@ -120,8 +135,13 @@ private function manageItems($store, $itemType, $indice)
private function createItems($store, $itemType, $indice)
{
$collection = $this->changedItemCollectionFactory->create()->filterCreated((int)$store->getId(), $itemType);
if ($collection->getSize()) {
$created = $this->documentsProvider->getBatched($collection, (int)$store->getId());

// Avoid process more than once the same product due to Magento2 product import process.
$groupedCollection = clone $collection;
$groupedCollection->getSelect()->group(ChangedItemInterface::ITEM_ID);

if ($groupedCollection->getSize()) {
$created = $this->documentsProvider->getBatched($groupedCollection, (int)$store->getId());
foreach ($this->batch->getItems($created, $this->batchSize) as $batchDocuments) {
$items = $this->mapItems($batchDocuments);
if (count($items)) {
Expand Down Expand Up @@ -153,8 +173,13 @@ private function createItems($store, $itemType, $indice)
private function updateItems($store, $itemType, $indice)
{
$collection = $this->changedItemCollectionFactory->create()->filterUpdated((int)$store->getId(), $itemType);
if ($collection->getSize()) {
$updated = $this->documentsProvider->getBatched($collection, (int)$store->getId());

// Avoid process more than once the same product due to Magento2 product import process.
$groupedCollection = clone $collection;
$groupedCollection->getSelect()->group(ChangedItemInterface::ITEM_ID);

if ($groupedCollection->getSize()) {
$updated = $this->documentsProvider->getBatched($groupedCollection, (int)$store->getId());
foreach ($this->batch->getItems($updated, $this->batchSize) as $batchDocuments) {
$items = $this->mapItems($batchDocuments);
if (count($items)) {
Expand Down Expand Up @@ -186,8 +211,13 @@ private function updateItems($store, $itemType, $indice)
private function deleteItems($store, $itemType, $indice)
{
$collection = $this->changedItemCollectionFactory->create()->filterDeleted((int)$store->getId(), $itemType);
if ($collection->getSize()) {
$deleted = $this->documentsProvider->getBatched($collection);

// Avoid process more than once the same product due to Magento2 product import process.
$groupedCollection = clone $collection;
$groupedCollection->getSelect()->group(ChangedItemInterface::ITEM_ID);

if ($groupedCollection->getSize()) {
$deleted = $this->documentsProvider->getBatched($groupedCollection);
foreach ($this->batch->getItems($deleted, $this->batchSize) as $batchDeleted) {
$items = $this->mapItems($batchDeleted);
if (count($items)) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doofinder/doofinder-magento2",
"version": "0.14.11",
"version": "0.14.12",
"description": "Doofinder module for Magento 2",
"type": "magento2-module",
"require": {
Expand Down
11 changes: 5 additions & 6 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
<column xsi:type="int" name="store_id" nullable="false" comment="ID of store the change was issued on"/>
<column xsi:type="smallint" name="item_type" nullable="false" comment="Type of item"/>
<column xsi:type="varchar" name="operation_type" nullable="false" length="255" comment="Operation Type"/>
<column xsi:type="int" name="skip_entity_id_binding" unsigned="true" nullable="true" identity="false"
comment="Ignore Entity ID Binding"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
<constraint xsi:type="unique" referenceId="DOOFINDER_FEED_CHANGED_ITEM_ITEM_ID_STORE_ID_OPERATION_TYPE_ITEM_TYPE">
<column name="item_id"/>
<column name="store_id"/>
<column name="operation_type"/>
<column name="item_type"/>
</constraint>
<constraint xsi:type="foreign" referenceId="DOOFINDER_FEED_CHANGED_ITEM_SKIP_ENTITY_ID_BINDING_FK"
table="doofinder_feed_changed_item" column="skip_entity_id_binding"
referenceTable="catalog_product_entity" referenceColumn="entity_id"/>
</table>
</schema>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Doofinder_Feed" setup_version="0.14.11">
<module name="Doofinder_Feed" setup_version="0.14.12">
<sequence>
<module name="Magento_Integration" />
</sequence>
Expand Down

0 comments on commit 0aeaa7b

Please sign in to comment.