diff --git a/classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php b/classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php index c30dc63034..097acc9b5a 100644 --- a/classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php +++ b/classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php @@ -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) {