Skip to content

Commit

Permalink
Merge pull request #739 from jonasraoni/bugfix-stable-3_3_0-10249-fix…
Browse files Browse the repository at this point in the history
…-data-loss-on-settings

Bugfix stable 3 3 0 10249 fix data loss on settings
  • Loading branch information
jonasraoni authored Jul 31, 2024
2 parents 2cc5ae5 + 79e5033 commit dcac0ff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
38 changes: 29 additions & 9 deletions classes/migration/upgrade/OPSv3_3_0UpgradeMigration.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,37 @@ private function _toJSON($row, $tableName, $searchBy, $valueToConvert) {
if (is_array($oldValue) && $this->_isNumerical($oldValue)) $oldValue = array_values($oldValue);
$newValue = json_encode($oldValue, JSON_UNESCAPED_UNICODE); // don't convert utf-8 characters to unicode escaped code

$id = array_key_first((array)$row); // get first/primary key column
// Ensure ID fields are included on the filter to avoid updating similar rows
$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;
}
}
}

// Remove empty filters
$searchBy = array_filter($searchBy, function ($item) use ($row) {
if (empty($row->{$item})) return false;
return true;
});
if (!count($primaryKeys)) {
foreach (array_keys(get_object_vars($row)) as $column) {
if (substr($column, -3, '_id')) {
$primaryKeys[] = $column;
}
}
}

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

$queryBuilder = Capsule::table($tableName)->where($id, $row->{$id});
foreach ($searchBy as $key => $column) {
$queryBuilder = $queryBuilder->where($column, $row->{$column});
$queryBuilder = Capsule::table($tableName);
foreach (array_unique($searchBy) as $column) {
if ($row->{$column} !== null) {
$queryBuilder->where($column, $row->{$column});
} else {
$queryBuilder->whereNull($column);
}
}
$queryBuilder->update([$valueToConvert => $newValue]);
}
Expand Down
4 changes: 4 additions & 0 deletions dbscripts/xml/upgrade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<note file="docs/release-notes/README-3.3.0" />
</upgrade>

<upgrade minversion="3.3.0.16" maxversion="3.3.0.18">
<migration class="lib.pkp.classes.migration.upgrade.I10249_FixProfileImageDataLoss" />
</upgrade>

<!-- update plugin configuration - should be done as the final upgrade task -->
<code function="addPluginVersions" />
</install>

0 comments on commit dcac0ff

Please sign in to comment.