Skip to content
Merged
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
38 changes: 30 additions & 8 deletions lib/internal/Magento/Framework/Mview/View/ChangelogBatchWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ public function walk(
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
}

$processID = getmypid();

$idsTable = $this->idsTableBuilder->build($changelog);
$idsColumns = $this->getIdsColumns($idsTable);

try {
# Prepare list of changed entries to return
$connection->createTable($idsTable);

$columns = $this->getIdsColumns($idsTable);

$select = $this->idsSelectBuilder->build($changelog);
$select
->distinct(true)
Expand All @@ -98,11 +100,12 @@ public function walk(
$connection->insertFromSelect(
$select,
$idsTable->getName(),
$columns,
$idsColumns,
AdapterInterface::INSERT_IGNORE
)
);

# Provide list of changed entries
$select = $connection->select()
->from($idsTable->getName());

Expand All @@ -115,7 +118,7 @@ public function walk(
foreach ($queries as $query) {
$idsQuery = (clone $query)
->reset(Select::COLUMNS)
->columns($columns);
->columns($idsColumns);

$ids = $this->idsFetcher->fetch($idsQuery);

Expand All @@ -124,32 +127,51 @@ public function walk(
}

yield $ids;

if ($this->isChildProcess($processID)) {
return;
}
}
} finally {
$connection->dropTable($idsTable->getName());
# Cleanup list of changed entries
if (!$this->isChildProcess($processID)) {
$connection->dropTable($idsTable->getName());
}
}
}

/**
* Collect columns used as ID of changed entries
*
* @param \Magento\Framework\DB\Ddl\Table $table
* @param \Magento\Framework\DB\Ddl\Table $idsTable
* @return array
*/
private function getIdsColumns(Table $table): array
private function getIdsColumns(Table $idsTable): array
{
return array_values(
array_map(
static function (array $column) {
return $column['COLUMN_NAME'];
},
array_filter(
$table->getColumns(),
$idsTable->getColumns(),
static function (array $column) {
return $column['PRIMARY'] === false;
}
)
)
);
}

/**
* Check if the process was forked
*
* @param int $processID
* @return bool
*/
private function isChildProcess(
int $processID
): bool {
return $processID !== getmypid();
}
}