@@ -52,6 +52,14 @@ class Subscription implements SubscriptionInterface
52
52
*/
53
53
protected $ linkedViews = [];
54
54
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
+
55
63
/**
56
64
* @var Resource
57
65
*/
@@ -175,35 +183,56 @@ protected function getLinkedViews()
175
183
}
176
184
177
185
/**
178
- * Build trigger statement for INSER , UPDATE, DELETE events
186
+ * Build trigger statement for INSERT , UPDATE, DELETE events
179
187
*
180
188
* @param string $event
181
189
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
182
190
* @return string
183
191
*/
184
192
protected function buildStatement ($ event , $ changelog )
185
193
{
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
+
186
209
switch ($ event ) {
187
210
case Trigger::EVENT_INSERT :
211
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
212
+ break ;
188
213
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 ;
196
223
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 ;
204
226
default :
205
227
return '' ;
228
+
206
229
}
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
+ );
207
236
}
208
237
209
238
/**
0 commit comments