Skip to content

Commit 4fec755

Browse files
authored
Merge pull request #4087 from nextcloud/backport/4086/main
[main] Avoid broken updates on invalid vote records
2 parents 4495613 + 996238f commit 4fec755

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
<post-migration>
3939
<step>OCA\Polls\Migration\RepairSteps\DropOrphanedTables</step>
4040
<step>OCA\Polls\Migration\RepairSteps\DropOrphanedColumns</step>
41-
<step>OCA\Polls\Migration\RepairSteps\UpdateHashes</step>
4241
<step>OCA\Polls\Migration\RepairSteps\DeleteInvalidRecords</step>
42+
<step>OCA\Polls\Migration\RepairSteps\UpdateHashes</step>
4343
<step>OCA\Polls\Migration\RepairSteps\CreateIndices</step>
4444
</post-migration>
4545
<install>

lib/Db/TableManager.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Doctrine\DBAL\Schema\Schema;
1313
use Doctrine\DBAL\Types\Type;
14+
use Exception;
1415
use OCA\Polls\AppConstants;
1516
use OCA\Polls\Migration\TableSchema;
1617
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -379,11 +380,16 @@ public function migrateOptionsToHash(): array {
379380
$count = 0;
380381
if ($table->hasColumn('poll_option_hash')) {
381382
foreach ($this->optionMapper->getAll() as $option) {
382-
$option->syncOption();
383-
// $option->setPollOptionHash(hash('md5', $option->getPollId() . $option->getPollOptionText() . $option->getTimestamp()));
384-
385-
$this->optionMapper->update($option);
386-
$count++;
383+
try {
384+
$option->syncOption();
385+
// $option->setPollOptionHash(hash('md5', $option->getPollId() . $option->getPollOptionText() . $option->getTimestamp()));
386+
387+
$this->optionMapper->update($option);
388+
$count++;
389+
} catch (Exception $e) {
390+
$messages[] = 'Skip hash update - Error updating option hash for optionId ' . $option->getId();
391+
$this->logger->error('Error updating option hash for optionId {id}', ['id' => $option->getId(), 'message' => $e->getMessage()]);
392+
}
387393
}
388394

389395
$this->logger->info('Updated {number} hashes in {db}', ['number' => $count,'db' => $this->dbPrefix . OptionMapper::TABLE]);
@@ -401,9 +407,14 @@ public function migrateOptionsToHash(): array {
401407
$count = 0;
402408
if ($table->hasColumn('vote_option_hash')) {
403409
foreach ($this->voteMapper->getAll() as $vote) {
404-
$vote->setVoteOptionHash(hash('md5', $vote->getPollId() . $vote->getUserId() . $vote->getVoteOptionText()));
405-
$this->voteMapper->update($vote);
406-
$count++;
410+
try {
411+
$vote->setVoteOptionHash(hash('md5', $vote->getPollId() . $vote->getUserId() . $vote->getVoteOptionText()));
412+
$this->voteMapper->update($vote);
413+
$count++;
414+
} catch (Exception $e) {
415+
$messages[] = 'Skip hash update - Error updating option hash for voteId ' . $vote->getId();
416+
$this->logger->error('Error updating option hash for voteId {id}', ['id' => $vote->getId(), 'message' => $e->getMessage()]);
417+
}
407418
}
408419

409420
$this->logger->info('Updated {number} hashes in {db}', ['number' => $count, 'db' => $this->dbPrefix . VoteMapper::TABLE]);

0 commit comments

Comments
 (0)