Skip to content

Allow ignored columns for mview to be specified at the subscription level #29692

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

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,62 @@ public function testRemove()

$this->model->remove();
}

/**
* Test ignored columns for mview specified at the subscription level
*
* @return void
*/
public function testBuildStatementIgnoredColumnSubscriptionLevel(): void
{
$tableName = 'cataloginventory_stock_item';
$ignoredColumnName = 'low_stock_date';
$notIgnoredColumnName = 'backorders';
$viewId = 'cataloginventory_stock';
$ignoredData = [
$viewId => [
$tableName => [
$ignoredColumnName => true,
$notIgnoredColumnName => false
]
]
];

$this->connectionMock->expects($this->once())
->method('isTableExists')
->willReturn(true);
$this->connectionMock->expects($this->once())
->method('describeTable')
->willReturn([
'item_id' => ['COLUMN_NAME' => 'item_id'],
'product_id' => ['COLUMN_NAME' => 'product_id'],
'stock_id' => ['COLUMN_NAME' => 'stock_id'],
'qty' => ['COLUMN_NAME' => 'qty'],
$ignoredColumnName => ['COLUMN_NAME' => $ignoredColumnName],
$notIgnoredColumnName => ['COLUMN_NAME' => $notIgnoredColumnName]
]);

$otherChangelogMock = $this->getMockForAbstractClass(ChangelogInterface::class);
$otherChangelogMock->expects($this->once())
->method('getViewId')
->willReturn($viewId);

$model = new Subscription(
$this->resourceMock,
$this->triggerFactoryMock,
$this->viewCollectionMock,
$this->viewMock,
$tableName,
'columnName',
[],
$ignoredData
);

$method = new \ReflectionMethod($model, 'buildStatement');
$method->setAccessible(true);
$statement = $method->invoke($model, Trigger::EVENT_UPDATE, $otherChangelogMock);

$this->assertStringNotContainsString($ignoredColumnName, $statement);
$this->assertStringContainsString($notIgnoredColumnName, $statement);
}
}
26 changes: 20 additions & 6 deletions lib/internal/Magento/Framework/Mview/View/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
use Magento\Framework\Mview\View\StateInterface;

/**
* Class Subscription
*
* @package Magento\Framework\Mview\View
* Class Subscription for handling partial indexation triggers
*/
class Subscription implements SubscriptionInterface
{
Expand Down Expand Up @@ -57,13 +55,19 @@ class Subscription implements SubscriptionInterface
protected $linkedViews = [];

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

/**
* List of columns that can be updated in a specific subscribed table
* for a specific view without creating a new change log entry
*/
private $ignoredUpdateColumnsBySubscription = [];

/**
* @var Resource
*/
Expand All @@ -77,6 +81,7 @@ class Subscription implements SubscriptionInterface
* @param string $tableName
* @param string $columnName
* @param array $ignoredUpdateColumns
* @param array $ignoredUpdateColumnsBySubscription
*/
public function __construct(
ResourceConnection $resource,
Expand All @@ -85,7 +90,8 @@ public function __construct(
\Magento\Framework\Mview\ViewInterface $view,
$tableName,
$columnName,
$ignoredUpdateColumns = []
$ignoredUpdateColumns = [],
$ignoredUpdateColumnsBySubscription = []
) {
$this->connection = $resource->getConnection();
$this->triggerFactory = $triggerFactory;
Expand All @@ -95,6 +101,7 @@ public function __construct(
$this->columnName = $columnName;
$this->resource = $resource;
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
$this->ignoredUpdateColumnsBySubscription = $ignoredUpdateColumnsBySubscription;
}

/**
Expand Down Expand Up @@ -209,7 +216,14 @@ protected function buildStatement($event, $changelog)
$describe = $this->connection->describeTable($tableName)
) {
$columnNames = array_column($describe, 'COLUMN_NAME');
$columnNames = array_diff($columnNames, $this->ignoredUpdateColumns);
$ignoredColumnsBySubscription = array_filter(
$this->ignoredUpdateColumnsBySubscription[$changelog->getViewId()][$this->getTableName()] ?? []
);
$ignoredColumns = array_merge(
$this->ignoredUpdateColumns,
array_keys($ignoredColumnsBySubscription)
);
$columnNames = array_diff($columnNames, $ignoredColumns);
if ($columnNames) {
$columns = [];
foreach ($columnNames as $columnName) {
Expand Down