Skip to content

Commit

Permalink
avoid repair error on orphaned votes
Browse files Browse the repository at this point in the history
Signed-off-by: dartcafe <github@dartcafe.de>
  • Loading branch information
dartcafe committed Jan 29, 2024
1 parent 74d097b commit e6d3c40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/Db/VoteMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

namespace OCA\Polls\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

/**
* @template-extends QBMapperWithUser<Vote>
Expand All @@ -39,6 +41,7 @@ class VoteMapper extends QBMapperWithUser {
public function __construct(
IDBConnection $db,
private UserMapper $userMapper,
private LoggerInterface $logger,
) {
parent::__construct($db, self::TABLE, Vote::class);
}
Expand Down Expand Up @@ -204,7 +207,17 @@ public function findOrphanedByPollandUser(int $pollId, string $userId): array {
protected function find(int $id): Vote {
$qb = $this->buildQuery();
$qb->andWhere($qb->expr()->eq(self::TABLE . '.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
try {
return $this->findEntity($qb);
} catch (DoesNotExistException $e) {
// Possible orphaned vote entry without option, try to get it directly from the table
$this->logger->info('Possibly orphaned vote found, try fallback search.', ['vote_id' => $id]);
$qb = $this->db->getQueryBuilder();
$qb->select(self::TABLE . '.*')
->from($this->getTableName(), self::TABLE)
->where($qb->expr()->eq(self::TABLE . '.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
}

protected function buildQuery(bool $findOrphaned = false): IQueryBuilder {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Db/OptionMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function setUp(): void {


$this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger);
$this->voteMapper = new VoteMapper($this->con, $this->userMapper);
$this->voteMapper = new VoteMapper($this->con, $this->userMapper, $this->logger);
$this->optionMapper = new OptionMapper($this->con, $this->session, $this->userMapper);
$this->pollMapper = new PollMapper($this->con);

Expand Down

0 comments on commit e6d3c40

Please sign in to comment.