From ed2d0da7af54d6d7525d68c6712ded458b5e38ef Mon Sep 17 00:00:00 2001 From: Patrick Bender Date: Sun, 2 Jul 2023 14:39:38 +0200 Subject: [PATCH 1/3] fix #4479 by ensuring subquery in findNewIds never returns NULL Signed-off-by: Patrick Bender --- lib/Db/MessageMapper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index 809e76a7ce..bdfe9dfd04 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -1209,8 +1209,9 @@ public function findNewIds(Mailbox $mailbox, array $ids): array { $select = $this->db->getQueryBuilder(); $subSelect = $this->db->getQueryBuilder(); + // MIN returns NULL if there are no rows selected, therefore we use COALESCE to ensure 0 is returned in this case $subSelect - ->select($subSelect->func()->min('sent_at')) + ->select($subSelect->createFunction('COALESCE(MIN(' . $subSelect->getColumnName('sent_at') . '), 0)')) ->from($this->getTableName()) ->where( $subSelect->expr()->eq('mailbox_id', $select->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT)), From e8b34b0a0ab4e3f596fa1821593dd4bdc4c28d6a Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 13 Sep 2023 13:08:36 +0200 Subject: [PATCH 2/3] fixup! fix #4479 by ensuring subquery in findNewIds never returns NULL Signed-off-by: Christoph Wurst --- tests/Integration/Db/MessageMapperTest.php | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Integration/Db/MessageMapperTest.php b/tests/Integration/Db/MessageMapperTest.php index e8078de1ff..1bc668419f 100644 --- a/tests/Integration/Db/MessageMapperTest.php +++ b/tests/Integration/Db/MessageMapperTest.php @@ -28,6 +28,7 @@ use ChristophWurst\Nextcloud\Testing\DatabaseTransaction; use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Account; +use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MessageMapper; use OCA\Mail\Db\TagMapper; use OCA\Mail\Support\PerformanceLogger; @@ -140,4 +141,28 @@ public function testResetPreviewDataFlag(): void { $result->closeCursor(); self::assertEquals(0, $cnt); } + + /** + * Verify we still find the one message of the mailbox if the passed IDs do not exist + */ + public function testFindNewIdsCoalesence(): void { + $uid = time(); + $mailbox = new Mailbox(); + $mailbox->setId(1); + $qb = $this->db->getQueryBuilder(); + $insert = $qb->insert($this->mapper->getTableName()) + ->values([ + 'uid' => $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT), + 'message_id' => $qb->createNamedParameter(''), + 'mailbox_id' => $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT), + 'subject' => $qb->createNamedParameter('TEST'), + 'sent_at' => $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT), + ]); + $insert->executeStatement(); + $id = $insert->getLastInsertId(); + + $found = $this->mapper->findNewIds($mailbox, [$id + 1]); + + self::assertCount(1, $found); + } } From 045e0bbc50d6c180e73a7e6782f2f8ade2a627b3 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 13 Sep 2023 13:10:01 +0200 Subject: [PATCH 3/3] fixup! fix #4479 by ensuring subquery in findNewIds never returns NULL Signed-off-by: Christoph Wurst --- tests/Integration/Db/MessageMapperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Db/MessageMapperTest.php b/tests/Integration/Db/MessageMapperTest.php index 1bc668419f..4d7acce751 100644 --- a/tests/Integration/Db/MessageMapperTest.php +++ b/tests/Integration/Db/MessageMapperTest.php @@ -145,7 +145,7 @@ public function testResetPreviewDataFlag(): void { /** * Verify we still find the one message of the mailbox if the passed IDs do not exist */ - public function testFindNewIdsCoalesence(): void { + public function testFindNewIdsCoalescence(): void { $uid = time(); $mailbox = new Mailbox(); $mailbox->setId(1);