@@ -57,21 +57,31 @@ class Subscription implements SubscriptionInterface
57
57
*/
58
58
protected $ resource ;
59
59
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
+
60
68
/**
61
69
* @param ResourceConnection $resource
62
70
* @param \Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory
63
71
* @param \Magento\Framework\Mview\View\CollectionInterface $viewCollection
64
72
* @param \Magento\Framework\Mview\ViewInterface $view
65
73
* @param string $tableName
66
74
* @param string $columnName
75
+ * @param array $ignoredUpdateColumns
67
76
*/
68
77
public function __construct (
69
78
ResourceConnection $ resource ,
70
79
\Magento \Framework \DB \Ddl \TriggerFactory $ triggerFactory ,
71
80
\Magento \Framework \Mview \View \CollectionInterface $ viewCollection ,
72
81
\Magento \Framework \Mview \ViewInterface $ view ,
73
82
$ tableName ,
74
- $ columnName
83
+ $ columnName ,
84
+ $ ignoredUpdateColumns = []
75
85
) {
76
86
$ this ->connection = $ resource ->getConnection ();
77
87
$ this ->triggerFactory = $ triggerFactory ;
@@ -80,6 +90,7 @@ public function __construct(
80
90
$ this ->tableName = $ tableName ;
81
91
$ this ->columnName = $ columnName ;
82
92
$ this ->resource = $ resource ;
93
+ $ this ->ignoredUpdateColumns = $ ignoredUpdateColumns ;
83
94
}
84
95
85
96
/**
@@ -175,7 +186,7 @@ protected function getLinkedViews()
175
186
}
176
187
177
188
/**
178
- * Build trigger statement for INSER , UPDATE, DELETE events
189
+ * Build trigger statement for INSERT , UPDATE, DELETE events
179
190
*
180
191
* @param string $event
181
192
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
@@ -185,25 +196,48 @@ protected function buildStatement($event, $changelog)
185
196
{
186
197
switch ($ event ) {
187
198
case Trigger::EVENT_INSERT :
199
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
200
+ break ;
201
+
188
202
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 ;
195
226
196
227
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 ;
203
230
204
231
default :
205
232
return '' ;
206
233
}
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
+ );
207
241
}
208
242
209
243
/**
0 commit comments