Skip to content

Commit 60b95b3

Browse files
MC-33405: review_entity_summary table has no unique key entity_pk_value-entity_type-store_id and may contain duplicate rows
1 parent f442fa9 commit 60b95b3

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

app/code/Magento/Review/Model/ResourceModel/Review.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ protected function aggregateReviewSummary($object, $ratingSummaryObject)
361361
);
362362
$select = $connection->select()->from($this->_aggregateTable)
363363
->where('entity_pk_value = :pk_value')
364-
->where('entity_type = :entity_type')
365-
->where('store_id = :store_id');
364+
->where('store_id = :store_id')
365+
->where('entity_type = :entity_type');
366366
$bind = [
367367
':pk_value' => $object->getEntityPkValue(),
368-
':entity_type' => $object->getEntityId(),
369368
':store_id' => $ratingSummaryObject->getStoreId(),
369+
':entity_type' => $object->getEntityId(),
370370
];
371371
$oldData = $connection->fetchRow($select, $bind);
372372
$data = new \Magento\Framework\DataObject();
@@ -424,6 +424,7 @@ protected function _loadVotedRatingIds($reviewId)
424424

425425
/**
426426
* Aggregate this review's ratings.
427+
*
427428
* Useful, when changing the review.
428429
*
429430
* @param int[] $ratingIds
@@ -471,6 +472,7 @@ public function getEntityIdByCode($entityCode)
471472

472473
/**
473474
* Delete reviews by product id.
475+
*
474476
* Better to call this method in transaction, because operation performed on two separated tables
475477
*
476478
* @param int $productId
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Review\Setup\Patch\Schema;
9+
10+
use Magento\Framework\DB\Adapter\AdapterInterface;
11+
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
12+
use Magento\Framework\Setup\SchemaSetupInterface;
13+
14+
/**
15+
* Schema patch to add unique constraint (entity_pk_value, store_id, entity_type) to review_entity_summary.
16+
*/
17+
class AddUniqueConstraintToReviewEntitySummary implements SchemaPatchInterface
18+
{
19+
/**
20+
* Table name to modify
21+
*/
22+
private const TABLE = 'review_entity_summary';
23+
/**
24+
* Columns to be unique
25+
*/
26+
private const COLUMNS = [
27+
'entity_pk_value',
28+
'store_id',
29+
'entity_type',
30+
];
31+
32+
/**
33+
* @var SchemaSetupInterface
34+
*/
35+
private $setup;
36+
37+
/**
38+
* @param SchemaSetupInterface $setup
39+
*/
40+
public function __construct(
41+
SchemaSetupInterface $setup
42+
) {
43+
$this->setup = $setup;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function apply()
50+
{
51+
$this->setup->startSetup();
52+
$this->addUniqueKey();
53+
$this->setup->endSetup();
54+
return $this;
55+
}
56+
57+
/**
58+
* Add unique constraint (entity_pk_value, store_id, entity_type) to review_entity_summary.
59+
*
60+
* Remove duplicate entries if any and retries until the unique key is successfully added.
61+
*/
62+
private function addUniqueKey(): void
63+
{
64+
$this->setup->getConnection()->addIndex(
65+
$this->setup->getTable(self::TABLE),
66+
$this->setup->getIdxName(
67+
self::TABLE,
68+
self::COLUMNS,
69+
AdapterInterface::INDEX_TYPE_UNIQUE
70+
),
71+
self::COLUMNS,
72+
AdapterInterface::INDEX_TYPE_UNIQUE
73+
);
74+
}
75+
76+
/**
77+
* @inheritDoc
78+
*/
79+
public static function getDependencies()
80+
{
81+
return [];
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*/
87+
public function getAliases()
88+
{
89+
return [];
90+
}
91+
}

0 commit comments

Comments
 (0)