Skip to content

Commit

Permalink
fix: add repair job to deleted duplicated cached messages
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
  • Loading branch information
st3iny authored and backportbot[bot] committed Jul 1, 2024
1 parent 617fbb8 commit 7075e64
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud
<step>OCA\Mail\Migration\MakeItineraryExtractorExecutable</step>
<step>OCA\Mail\Migration\ProvisionAccounts</step>
<step>OCA\Mail\Migration\RepairMailTheads</step>
<step>OCA\Mail\Migration\DeleteDuplicateUids</step>
</post-migration>
</repair-steps>
<commands>
Expand Down
51 changes: 51 additions & 0 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1626,4 +1626,55 @@ public function findMessagesToUnSnooze(int $mailboxId, int $time): array {

return $this->findEntities($select);
}

/**
* Delete all duplicated cached messages.
* Some messages (with the same mailbox_id and uid) where inserted twice and this method cleans
* up the duplicated rows.
*/
public function deleteDuplicateUids(): void {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('t1.id', 't1.mailbox_id', 't1.uid')
->from($this->getTableName(), 't1')
->innerJoin('t1', $this->getTableName(), 't2', $qb->expr()->andX(
$qb->expr()->eq('t1.mailbox_id', 't2.mailbox_id', IQueryBuilder::PARAM_INT),
$qb->expr()->eq('t1.uid', 't2.uid', IQueryBuilder::PARAM_INT),
$qb->expr()->neq('t1.id', 't2.id', IQueryBuilder::PARAM_INT),
))
->groupBy('mailbox_id', 'uid')
->executeQuery();
$rows = $result->fetchAll();
$result->closeCursor();

if (empty($rows)) {
return;
}

$deleteQb = $this->db->getQueryBuilder();
$deleteQb->delete($this->getTableName())
->where(
$deleteQb->expr()->neq(
'id',
$deleteQb->createParameter('id'),
IQueryBuilder::PARAM_INT,
),
$deleteQb->expr()->eq(
'mailbox_id',
$deleteQb->createParameter('mailbox_id'),
IQueryBuilder::PARAM_INT,
),
$deleteQb->expr()->eq(
'uid',
$deleteQb->createParameter('uid'),
IQueryBuilder::PARAM_INT,
),
);

foreach ($rows as $row) {
$deleteQb->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT);
$deleteQb->setParameter('mailbox_id', $row['mailbox_id'], IQueryBuilder::PARAM_INT);
$deleteQb->setParameter('uid', $row['uid'], IQueryBuilder::PARAM_INT);
$deleteQb->executeStatement();
}
}
}
29 changes: 29 additions & 0 deletions lib/Migration/DeleteDuplicateUids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Migration;

use OCA\Mail\Db\MessageMapper;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class DeleteDuplicateUids implements IRepairStep {
public function __construct(
private MessageMapper $messageMapper,
) {
}

public function getName(): string {
return 'Delete duplicated cached messages';
}

public function run(IOutput $output): void {
$this->messageMapper->deleteDuplicateUids();
}
}

0 comments on commit 7075e64

Please sign in to comment.