Skip to content

Commit

Permalink
Merge pull request pkp#4379 from jonasraoni/bugfix-stable-3_3_0-10249…
Browse files Browse the repository at this point in the history
…-fix-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 8517ea2 + 7ff912c commit d226fb1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
38 changes: 29 additions & 9 deletions classes/migration/upgrade/OJSv3_3_0UpgradeMigration.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,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
6 changes: 5 additions & 1 deletion dbscripts/xml/upgrade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<!-- migrateUserAndAuthorNames should be after migrateSRLocale -->
<code function="migrateUserAndAuthorNames" condition="return $installer->tableExists('users_tmp');" />
</upgrade>

<upgrade minversion="2.4.0.0" maxversion="3.1.9.9">
<code function="migrateSubmissionCoverImages" />
</upgrade>
Expand Down Expand Up @@ -208,6 +208,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 d226fb1

Please sign in to comment.