Skip to content

Commit 1b2fbfc

Browse files
author
Oleksii Korshenko
authored
MAGETWO-70883: Prevent change log entry when nothing has changed #4893
2 parents 4ec0778 + 5b9888b commit 1b2fbfc

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

app/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,4 +1330,11 @@
13301330
<argument name="configSource" xsi:type="object">Magento\Config\App\Config\Source\EnvironmentConfigSource</argument>
13311331
</arguments>
13321332
</type>
1333+
<type name="Magento\Framework\Mview\View\Subscription">
1334+
<arguments>
1335+
<argument name="ignoredUpdateColumns" xsi:type="array">
1336+
<item name="updated_at" xsi:type="string">updated_at</item>
1337+
</argument>
1338+
</arguments>
1339+
</type>
13331340
</config>

lib/internal/Magento/Framework/Mview/View/Subscription.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\ResourceConnection;
1212
use Magento\Framework\DB\Ddl\Trigger;
13+
use Magento\Framework\Mview\View\StateInterface;
1314

1415
class Subscription implements SubscriptionInterface
1516
{
@@ -52,6 +53,14 @@ class Subscription implements SubscriptionInterface
5253
*/
5354
protected $linkedViews = [];
5455

56+
/**
57+
* List of columns that can be updated in a subscribed table
58+
* without creating a new change log entry
59+
*
60+
* @var array
61+
*/
62+
private $ignoredUpdateColumns = [];
63+
5564
/**
5665
* @var Resource
5766
*/
@@ -64,14 +73,16 @@ class Subscription implements SubscriptionInterface
6473
* @param \Magento\Framework\Mview\ViewInterface $view
6574
* @param string $tableName
6675
* @param string $columnName
76+
* @param array $ignoredUpdateColumns
6777
*/
6878
public function __construct(
6979
ResourceConnection $resource,
7080
\Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory,
7181
\Magento\Framework\Mview\View\CollectionInterface $viewCollection,
7282
\Magento\Framework\Mview\ViewInterface $view,
7383
$tableName,
74-
$columnName
84+
$columnName,
85+
$ignoredUpdateColumns = []
7586
) {
7687
$this->connection = $resource->getConnection();
7788
$this->triggerFactory = $triggerFactory;
@@ -80,6 +91,7 @@ public function __construct(
8091
$this->tableName = $tableName;
8192
$this->columnName = $columnName;
8293
$this->resource = $resource;
94+
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
8395
}
8496

8597
/**
@@ -154,7 +166,7 @@ public function remove()
154166
protected function getLinkedViews()
155167
{
156168
if (!$this->linkedViews) {
157-
$viewList = $this->viewCollection->getViewsByStateMode(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED);
169+
$viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
158170

159171
foreach ($viewList as $view) {
160172
/** @var \Magento\Framework\Mview\ViewInterface $view */
@@ -175,35 +187,56 @@ protected function getLinkedViews()
175187
}
176188

177189
/**
178-
* Build trigger statement for INSER, UPDATE, DELETE events
190+
* Build trigger statement for INSERT, UPDATE, DELETE events
179191
*
180192
* @param string $event
181193
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
182194
* @return string
183195
*/
184196
protected function buildStatement($event, $changelog)
185197
{
198+
$columns = [];
199+
if ($this->connection->isTableExists($this->getTableName())
200+
&& $describe = $this->connection->describeTable($this->getTableName())
201+
) {
202+
foreach ($describe as $column) {
203+
if (in_array($column['COLUMN_NAME'], $this->ignoredUpdateColumns)) {
204+
continue;
205+
}
206+
$columns[] = sprintf(
207+
'NEW.%1$s != OLD.%1$s',
208+
$this->connection->quoteIdentifier($column['COLUMN_NAME'])
209+
);
210+
}
211+
}
212+
186213
switch ($event) {
187214
case Trigger::EVENT_INSERT:
215+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
216+
break;
188217
case Trigger::EVENT_UPDATE:
189-
return sprintf(
190-
"INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);",
191-
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
192-
$this->connection->quoteIdentifier($changelog->getColumnName()),
193-
$this->connection->quoteIdentifier($this->getColumnName())
194-
);
195-
218+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
219+
if ($columns) {
220+
$trigger = sprintf(
221+
"IF (%s) THEN %s END IF;",
222+
implode(' OR ', $columns),
223+
$trigger
224+
);
225+
}
226+
break;
196227
case Trigger::EVENT_DELETE:
197-
return sprintf(
198-
"INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);",
199-
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
200-
$this->connection->quoteIdentifier($changelog->getColumnName()),
201-
$this->connection->quoteIdentifier($this->getColumnName())
202-
);
203-
228+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);";
229+
break;
204230
default:
205231
return '';
232+
206233
}
234+
return sprintf(
235+
$trigger,
236+
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
237+
$this->connection->quoteIdentifier($changelog->getColumnName()),
238+
$this->connection->quoteIdentifier($this->getColumnName())
239+
);
207240
}
208241

209242
/**

0 commit comments

Comments
 (0)