Skip to content

Commit

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

Bugfix stable 3 3 0 10249 fix data loss on settings
  • Loading branch information
jonasraoni committed Jul 31, 2024
2 parents 4f1311f + 76fda04 commit 0624594
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/OMPv3_3_0UpgradeMigration.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,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 @@ -167,6 +167,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 0624594

Please sign in to comment.