Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix stable 3 3 0 10249 fix data loss on settings #739

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>