Skip to content

Commit

Permalink
fixup! fix: duplicate uid repair job failing on postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
st3iny committed Jul 8, 2024
1 parent 451a4ec commit 6669076
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,8 @@ public function findMessagesToUnSnooze(int $mailboxId, int $time): array {
* 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.
*
* @throws \OCP\DB\Exception
*/
public function deleteDuplicateUids(): void {
$qb = $this->db->getQueryBuilder();
Expand All @@ -1672,19 +1674,12 @@ public function deleteDuplicateUids(): void {
$qb->expr()->eq('t1.uid', 't2.uid', IQueryBuilder::PARAM_INT),
$qb->expr()->neq('t1.id', 't2.id', IQueryBuilder::PARAM_INT),
))
->groupBy('t1.mailbox_id', 't1.uid', 't1.id')
->executeQuery();
$rows = $result->fetchAll();
$result->closeCursor();

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

$deleteQb = $this->db->getQueryBuilder();
$deleteQb->delete($this->getTableName())
->where(
$deleteQb->expr()->eq(
$deleteQb->expr()->neq(
'id',
$deleteQb->createParameter('id'),
IQueryBuilder::PARAM_INT,
Expand All @@ -1701,30 +1696,20 @@ public function deleteDuplicateUids(): void {
),
);

$deleted = [];
foreach ($rows as $row) {
//$deleted[$row['mailbox_id'] . $row['uid']] = true;
if (!isset($deleted[$row['mailbox_id']])) {
$deleted[$row['mailbox_id']] = [];
$handledMailboxIdUidPairs = [];
while ($row = $result->fetch()) {
$pair = $row['mailbox_id'] . ':' . $row['uid'];
if (isset($handledMailboxIdUidPairs[$pair])) {
continue;
}
$deleted[$row['mailbox_id']][$row['uid']][] = $row['id'];
}

foreach ($deleted as $mailboxId => $uids) {
foreach ($uids as $uid => $ids) {
$first = true;
foreach ($ids as $id) {
if ($first) {
$first = false;
continue;
}
$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();

$deleteQb->setParameter('id', $id, IQueryBuilder::PARAM_INT);
$deleteQb->setParameter('mailbox_id', $mailboxId, IQueryBuilder::PARAM_INT);
$deleteQb->setParameter('uid', $uid, IQueryBuilder::PARAM_INT);
$deleteQb->executeStatement();
}
}
$handledMailboxIdUidPairs[$pair] = true;
}
$result->closeCursor();
}
}

0 comments on commit 6669076

Please sign in to comment.