Skip to content

Commit 216e8d7

Browse files
committed
MAGETWO-70883: Prevent change log entry when nothing has changed #4893
- Merge Pull Request #4893 from ajpevers/magento2:patch-2 - Merged commits: 1. 433c9c3 2. 0028ce6 3. 01e20d2 4. d41bc5b
2 parents d844e31 + d41bc5b commit 216e8d7

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ class Subscription implements SubscriptionInterface
5252
*/
5353
protected $linkedViews = [];
5454

55+
/**
56+
* List of columns that can be updated in a subscribed table
57+
* without creating a new change log entry
58+
*
59+
* @var array
60+
*/
61+
protected $ignoredUpdateColumns = ['updated_at'];
62+
5563
/**
5664
* @var Resource
5765
*/
@@ -175,35 +183,56 @@ protected function getLinkedViews()
175183
}
176184

177185
/**
178-
* Build trigger statement for INSER, UPDATE, DELETE events
186+
* Build trigger statement for INSERT, UPDATE, DELETE events
179187
*
180188
* @param string $event
181189
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
182190
* @return string
183191
*/
184192
protected function buildStatement($event, $changelog)
185193
{
194+
$columns = [];
195+
if ($this->connection->isTableExists($this->getTableName())
196+
&& $describe = $this->connection->describeTable($this->getTableName())
197+
) {
198+
foreach ($describe as $column) {
199+
if (in_array($column['COLUMN_NAME'], $this->ignoredUpdateColumns)) {
200+
continue;
201+
}
202+
$columns[] = sprintf(
203+
'NEW.%1$s != OLD.%1$s',
204+
$this->connection->quoteIdentifier($column['COLUMN_NAME'])
205+
);
206+
}
207+
}
208+
186209
switch ($event) {
187210
case Trigger::EVENT_INSERT:
211+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
212+
break;
188213
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-
214+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
215+
if ($columns) {
216+
$trigger = sprintf(
217+
"IF (%s) THEN %s END IF;",
218+
implode(' OR ', $columns),
219+
$trigger
220+
);
221+
}
222+
break;
196223
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-
224+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);";
225+
break;
204226
default:
205227
return '';
228+
206229
}
230+
return sprintf(
231+
$trigger,
232+
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
233+
$this->connection->quoteIdentifier($changelog->getColumnName()),
234+
$this->connection->quoteIdentifier($this->getColumnName())
235+
);
207236
}
208237

209238
/**

0 commit comments

Comments
 (0)