Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent change log entry when nothing has changed #4893

Merged
merged 4 commits into from
Jul 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions lib/internal/Magento/Framework/Mview/View/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class Subscription implements SubscriptionInterface
*/
protected $linkedViews = [];

/**
* List of columns that can be updated in a subscribed table
* without creating a new change log entry
*
* @var array
*/
protected $ignoredUpdateColumns = ['updated_at'];

/**
* @var Resource
*/
Expand Down Expand Up @@ -175,35 +183,56 @@ protected function getLinkedViews()
}

/**
* Build trigger statement for INSER, UPDATE, DELETE events
* Build trigger statement for INSERT, UPDATE, DELETE events
*
* @param string $event
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
* @return string
*/
protected function buildStatement($event, $changelog)
{
$columns = [];
if ($this->connection->isTableExists($this->getTableName())
&& $describe = $this->connection->describeTable($this->getTableName())
) {
foreach ($describe as $column) {
if (in_array($column['COLUMN_NAME'], $this->ignoredUpdateColumns)) {
continue;
}
$columns[] = sprintf(
'NEW.%1$s != OLD.%1$s',
$this->connection->quoteIdentifier($column['COLUMN_NAME'])
);
}
}

switch ($event) {
case Trigger::EVENT_INSERT:
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
break;
case Trigger::EVENT_UPDATE:
return sprintf(
"INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);",
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);

$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
if ($columns) {
$trigger = sprintf(
"IF (%s) THEN %s END IF;",
implode(' OR ', $columns),
$trigger
);
}
break;
case Trigger::EVENT_DELETE:
return sprintf(
"INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);",
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);

$trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);";
break;
default:
return '';

}
return sprintf(
$trigger,
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);
}

/**
Expand Down