From e6d3c40c4d8ddc9c99973482beb15c1db2714073 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 29 Jan 2024 13:33:41 +0100 Subject: [PATCH] avoid repair error on orphaned votes Signed-off-by: dartcafe --- lib/Db/VoteMapper.php | 15 ++++++++++++++- tests/Unit/Db/OptionMapperTest.php | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index cfda74c0b..c75728af8 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -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 @@ -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); } @@ -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 { diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index e78d6e099..30c2e3a59 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -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);