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 31, 2024
1 parent 8d9d867 commit 194411b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,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 194411b

Please sign in to comment.