-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9071 from nextcloud/bug/5200/mailbox-name-hash
fix: allow syncing of mailboxes with a trailing space
- Loading branch information
Showing
7 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Daniel Kesselberg <[email protected]> | ||
* | ||
* @author Daniel Kesselberg <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\IDBConnection; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version3500Date20231115182612 extends SimpleMigrationStep { | ||
|
||
public function __construct( | ||
private IDBConnection $connection | ||
) { | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$mailboxesTable = $schema->getTable('mail_mailboxes'); | ||
if (!$mailboxesTable->hasColumn('name_hash')) { | ||
$mailboxesTable->addColumn('name_hash', Types::STRING); | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
*/ | ||
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
// Round 1: Hash common mailbox names | ||
|
||
$this->connection->beginTransaction(); | ||
|
||
foreach (['INBOX', 'Drafts', 'Sent', 'Trash', 'Junk', 'Spam', 'Archive', 'Archives'] as $name) { | ||
$qb = $this->connection->getQueryBuilder(); | ||
$qb->update('mail_mailboxes') | ||
->set('name_hash', $qb->createNamedParameter(md5($name))) | ||
->where($qb->expr()->like('name', $qb->createNamedParameter($name), Types::STRING)) | ||
->executeStatement(); | ||
} | ||
|
||
$this->connection->commit(); | ||
|
||
// Round 2: Hash everything else | ||
|
||
$qb = $this->connection->getQueryBuilder(); | ||
$qb->select(['id', 'name']) | ||
->from('mail_mailboxes') | ||
->where($qb->expr()->emptyString('name_hash')); | ||
$mailboxes = $qb->executeQuery(); | ||
|
||
$updateQb = $this->connection->getQueryBuilder(); | ||
$updateQb->update('mail_mailboxes') | ||
->set('name_hash', $updateQb->createParameter('name_hash')) | ||
->where($updateQb->expr()->eq('id', $updateQb->createParameter('id'))); | ||
|
||
$this->connection->beginTransaction(); | ||
|
||
$queryCount = 0; | ||
while (($row = $mailboxes->fetch()) !== false) { | ||
$queryCount++; | ||
|
||
$updateQb->setParameter('id', $row['id']); | ||
$updateQb->setParameter('name_hash', md5($row['name'])); | ||
$updateQb->executeStatement(); | ||
|
||
if ($queryCount === 50000) { | ||
$this->connection->commit(); | ||
$this->connection->beginTransaction(); | ||
$queryCount = 0; | ||
} | ||
} | ||
|
||
$mailboxes->closeCursor(); | ||
|
||
$this->connection->commit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Daniel Kesselberg <[email protected]> | ||
* | ||
* @author Daniel Kesselberg <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version3500Date20231115184458 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$mailboxesTable = $schema->getTable('mail_mailboxes'); | ||
|
||
$indexOld = 'UNIQ_22DEBD839B6B5FBA5E237E06'; | ||
$indexNew = 'mail_mb_account_id_name_hash'; | ||
|
||
if ($mailboxesTable->hasIndex($indexOld)) { | ||
$mailboxesTable->dropIndex($indexOld); | ||
} | ||
|
||
if (!$mailboxesTable->hasIndex($indexNew)) { | ||
$mailboxesTable->addUniqueIndex(['account_id', 'name_hash'], $indexNew); | ||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters