Skip to content

Commit

Permalink
#10249 Added code to look for primary keys
Browse files Browse the repository at this point in the history
(cherry picked from commit 0ceca36)
  • Loading branch information
jonasraoni committed Jul 31, 2024
1 parent b776f7f commit 31a5744
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions classes/migration/upgrade/PKPv3_3_0UpgradeMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,29 @@ private function _toJSON($row, $tableName, $searchBy, $valueToConvert)
$newValue = json_encode($oldValue, JSON_UNESCAPED_UNICODE); // don't convert utf-8 characters to unicode escaped code

// Ensure ID fields are included on the filter to avoid updating similar rows
foreach (array_keys($row) as $column) {
if (substr($column, -3, '_id')) {
$searchBy[] = $column;
$tableDetails = DB::connection()->getDoctrineSchemaManager()->listTableDetails($tableName);
$primaryKeys = [];
try {
$primaryKeys = $tableDetails->getPrimaryKeyColumns();
} catch (Exception $e) {
foreach ($tableDetails->getIndexes() as $index) {
if ($index->isPrimary() || $index->isUnique()) {
$primaryKeys = $index->getColumns();
break;
}
}
}

if (!count($primaryKeys)) {
foreach (array_keys($row) as $column) {
if (substr($column, -3, '_id')) {
$primaryKeys[] = $column;
}
}
}

$searchBy = array_merge($searchBy, $primaryKeys);

$queryBuilder = DB::table($tableName);
foreach (array_unique($searchBy) as $column) {
if ($row->{$column} !== null) {
Expand Down

0 comments on commit 31a5744

Please sign in to comment.