Skip to content

Commit

Permalink
pkp/pkp-lib#10249 Added code to look for primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Jul 28, 2024
1 parent 9d4f667 commit ffba8e1
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions classes/migration/upgrade/OJSv3_3_0UpgradeMigration.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,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 = Capsule::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 = Capsule::table($tableName);
foreach (array_unique($searchBy) as $column) {
if ($row->{$column} !== null) {
Expand Down

0 comments on commit ffba8e1

Please sign in to comment.