Skip to content

Commit 433c9c3

Browse files
Anton EversAnton Evers
authored andcommitted
Prevent change log entry when nothing has changed
Fixes #2987 If an entry has no changes but did get a MySQL UPDATE the trigger will not fire. This prevents entities from being reindexed when they haven't changed. This way the refresh index cron job finishes faster without any loss of functionality. This updated_at column is ignored because this is the only column that will always change, even if no attribute has been edited.
1 parent d844e31 commit 433c9c3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,32 @@ protected function getLinkedViews()
183183
*/
184184
protected function buildStatement($event, $changelog)
185185
{
186+
$skipColumns = ['updated_at'];
187+
$describe = $this->connection->describeTable($this->getTableName());
188+
$columns = [];
189+
foreach ($describe as $column) {
190+
if (!in_array($column['COLUMN_NAME'], $skipColumns)) {
191+
$columns[] = sprintf('NEW.%1$s <> OLD.%1$s',
192+
$this->connection->quoteIdentifier($column['COLUMN_NAME']));
193+
}
194+
}
195+
186196
switch ($event) {
187197
case Trigger::EVENT_INSERT:
188-
case Trigger::EVENT_UPDATE:
189198
return sprintf(
190199
"INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);",
191200
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
192201
$this->connection->quoteIdentifier($changelog->getColumnName()),
193202
$this->connection->quoteIdentifier($this->getColumnName())
194203
);
204+
case Trigger::EVENT_UPDATE:
205+
return sprintf(
206+
"IF (%s) THEN INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); END IF;",
207+
implode(' OR ', $columns),
208+
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
209+
$this->connection->quoteIdentifier($changelog->getColumnName()),
210+
$this->connection->quoteIdentifier($this->getColumnName())
211+
);
195212

196213
case Trigger::EVENT_DELETE:
197214
return sprintf(

0 commit comments

Comments
 (0)