Skip to content

Commit e526166

Browse files
ENGCOM-8063: Allow ignored columns for mview to be specified at the subscription level #29692
- Merge Pull Request #29692 from aligent-lturner/magento2-1:feature/subscription_view_config - Merged commits: 1. 2c13e0c 2. d1983b4 3. d58bed1 4. 5099ab7 5. 0417ffc 6. 8c754b2 7. 1d3c42d
2 parents e9fa180 + 1d3c42d commit e526166

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,62 @@ public function testRemove()
323323

324324
$this->model->remove();
325325
}
326+
327+
/**
328+
* Test ignored columns for mview specified at the subscription level
329+
*
330+
* @return void
331+
*/
332+
public function testBuildStatementIgnoredColumnSubscriptionLevel(): void
333+
{
334+
$tableName = 'cataloginventory_stock_item';
335+
$ignoredColumnName = 'low_stock_date';
336+
$notIgnoredColumnName = 'backorders';
337+
$viewId = 'cataloginventory_stock';
338+
$ignoredData = [
339+
$viewId => [
340+
$tableName => [
341+
$ignoredColumnName => true,
342+
$notIgnoredColumnName => false
343+
]
344+
]
345+
];
346+
347+
$this->connectionMock->expects($this->once())
348+
->method('isTableExists')
349+
->willReturn(true);
350+
$this->connectionMock->expects($this->once())
351+
->method('describeTable')
352+
->willReturn([
353+
'item_id' => ['COLUMN_NAME' => 'item_id'],
354+
'product_id' => ['COLUMN_NAME' => 'product_id'],
355+
'stock_id' => ['COLUMN_NAME' => 'stock_id'],
356+
'qty' => ['COLUMN_NAME' => 'qty'],
357+
$ignoredColumnName => ['COLUMN_NAME' => $ignoredColumnName],
358+
$notIgnoredColumnName => ['COLUMN_NAME' => $notIgnoredColumnName]
359+
]);
360+
361+
$otherChangelogMock = $this->getMockForAbstractClass(ChangelogInterface::class);
362+
$otherChangelogMock->expects($this->once())
363+
->method('getViewId')
364+
->willReturn($viewId);
365+
366+
$model = new Subscription(
367+
$this->resourceMock,
368+
$this->triggerFactoryMock,
369+
$this->viewCollectionMock,
370+
$this->viewMock,
371+
$tableName,
372+
'columnName',
373+
[],
374+
$ignoredData
375+
);
376+
377+
$method = new \ReflectionMethod($model, 'buildStatement');
378+
$method->setAccessible(true);
379+
$statement = $method->invoke($model, Trigger::EVENT_UPDATE, $otherChangelogMock);
380+
381+
$this->assertStringNotContainsString($ignoredColumnName, $statement);
382+
$this->assertStringContainsString($notIgnoredColumnName, $statement);
383+
}
326384
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
use Magento\Framework\Mview\View\StateInterface;
1212

1313
/**
14-
* Class Subscription
15-
*
16-
* @package Magento\Framework\Mview\View
14+
* Class Subscription for handling partial indexation triggers
1715
*/
1816
class Subscription implements SubscriptionInterface
1917
{
@@ -57,13 +55,19 @@ class Subscription implements SubscriptionInterface
5755
protected $linkedViews = [];
5856

5957
/**
60-
* List of columns that can be updated in a subscribed table
58+
* List of columns that can be updated in any subscribed table
6159
* without creating a new change log entry
6260
*
6361
* @var array
6462
*/
6563
private $ignoredUpdateColumns = [];
6664

65+
/**
66+
* List of columns that can be updated in a specific subscribed table
67+
* for a specific view without creating a new change log entry
68+
*/
69+
private $ignoredUpdateColumnsBySubscription = [];
70+
6771
/**
6872
* @var Resource
6973
*/
@@ -77,6 +81,7 @@ class Subscription implements SubscriptionInterface
7781
* @param string $tableName
7882
* @param string $columnName
7983
* @param array $ignoredUpdateColumns
84+
* @param array $ignoredUpdateColumnsBySubscription
8085
*/
8186
public function __construct(
8287
ResourceConnection $resource,
@@ -85,7 +90,8 @@ public function __construct(
8590
\Magento\Framework\Mview\ViewInterface $view,
8691
$tableName,
8792
$columnName,
88-
$ignoredUpdateColumns = []
93+
$ignoredUpdateColumns = [],
94+
$ignoredUpdateColumnsBySubscription = []
8995
) {
9096
$this->connection = $resource->getConnection();
9197
$this->triggerFactory = $triggerFactory;
@@ -95,6 +101,7 @@ public function __construct(
95101
$this->columnName = $columnName;
96102
$this->resource = $resource;
97103
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
104+
$this->ignoredUpdateColumnsBySubscription = $ignoredUpdateColumnsBySubscription;
98105
}
99106

100107
/**
@@ -209,7 +216,14 @@ protected function buildStatement($event, $changelog)
209216
$describe = $this->connection->describeTable($tableName)
210217
) {
211218
$columnNames = array_column($describe, 'COLUMN_NAME');
212-
$columnNames = array_diff($columnNames, $this->ignoredUpdateColumns);
219+
$ignoredColumnsBySubscription = array_filter(
220+
$this->ignoredUpdateColumnsBySubscription[$changelog->getViewId()][$this->getTableName()] ?? []
221+
);
222+
$ignoredColumns = array_merge(
223+
$this->ignoredUpdateColumns,
224+
array_keys($ignoredColumnsBySubscription)
225+
);
226+
$columnNames = array_diff($columnNames, $ignoredColumns);
213227
if ($columnNames) {
214228
$columns = [];
215229
foreach ($columnNames as $columnName) {

0 commit comments

Comments
 (0)