@@ -57,21 +57,31 @@ class Subscription implements SubscriptionInterface
5757 */
5858 protected $ resource ;
5959
60+ /**
61+ * List of columns that can be updated in a subscribed table
62+ * without creating a new change log entry
63+ *
64+ * @var array
65+ */
66+ private $ ignoredUpdateColumns = [];
67+
6068 /**
6169 * @param ResourceConnection $resource
6270 * @param \Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory
6371 * @param \Magento\Framework\Mview\View\CollectionInterface $viewCollection
6472 * @param \Magento\Framework\Mview\ViewInterface $view
6573 * @param string $tableName
6674 * @param string $columnName
75+ * @param array $ignoredUpdateColumns
6776 */
6877 public function __construct (
6978 ResourceConnection $ resource ,
7079 \Magento \Framework \DB \Ddl \TriggerFactory $ triggerFactory ,
7180 \Magento \Framework \Mview \View \CollectionInterface $ viewCollection ,
7281 \Magento \Framework \Mview \ViewInterface $ view ,
7382 $ tableName ,
74- $ columnName
83+ $ columnName ,
84+ $ ignoredUpdateColumns = []
7585 ) {
7686 $ this ->connection = $ resource ->getConnection ();
7787 $ this ->triggerFactory = $ triggerFactory ;
@@ -80,6 +90,7 @@ public function __construct(
8090 $ this ->tableName = $ tableName ;
8191 $ this ->columnName = $ columnName ;
8292 $ this ->resource = $ resource ;
93+ $ this ->ignoredUpdateColumns = $ ignoredUpdateColumns ;
8394 }
8495
8596 /**
@@ -175,7 +186,7 @@ protected function getLinkedViews()
175186 }
176187
177188 /**
178- * Build trigger statement for INSER , UPDATE, DELETE events
189+ * Build trigger statement for INSERT , UPDATE, DELETE events
179190 *
180191 * @param string $event
181192 * @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
@@ -185,25 +196,48 @@ protected function buildStatement($event, $changelog)
185196 {
186197 switch ($ event ) {
187198 case Trigger::EVENT_INSERT :
199+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
200+ break ;
201+
188202 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- );
203+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
204+
205+ if ($ this ->connection ->isTableExists ($ this ->getTableName ())
206+ && $ describe = $ this ->connection ->describeTable ($ this ->getTableName ())
207+ ) {
208+ $ columnNames = array_column ($ describe , 'COLUMN_NAME ' );
209+ $ columnNames = array_diff ($ columnNames , $ this ->ignoredUpdateColumns );
210+ if ($ columnNames ) {
211+ $ columns = [];
212+ foreach ($ columnNames as $ columnName ) {
213+ $ columns [] = sprintf (
214+ 'NEW.%1$s != OLD.%1$s ' ,
215+ $ this ->connection ->quoteIdentifier ($ columnName )
216+ );
217+ }
218+ $ trigger = sprintf (
219+ "IF (%s) THEN %s END IF; " ,
220+ implode (' OR ' , $ columns ),
221+ $ trigger
222+ );
223+ }
224+ }
225+ break ;
195226
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- );
228+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s); " ;
229+ break ;
203230
204231 default :
205232 return '' ;
206233 }
234+
235+ return sprintf (
236+ $ trigger ,
237+ $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
238+ $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
239+ $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
240+ );
207241 }
208242
209243 /**
0 commit comments