-
Notifications
You must be signed in to change notification settings - Fork 295
fix(database): Add foreign keys to *_mailbox_id fields in mail_accounts table
#12220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\Migration; | ||
|
|
||
| use Closure; | ||
| use Doctrine\DBAL\Schema\Table; | ||
| use OCP\DB\Exception; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
| use OCP\Migration\Attributes\ModifyColumn; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| #[ModifyColumn( | ||
| table: 'mail_accounts', | ||
| description: 'Remove invalid mailbox_id from *_mailbox_id columns and add foreign keys to ensure consistency') | ||
| ] | ||
| class Version5007Date20260108124422 extends SimpleMigrationStep { | ||
| public function __construct( | ||
| private IDBConnection $db, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param string $mailboxType | ||
| */ | ||
| private function removeInconsistentMailboxEntries(string $mailboxType): void { | ||
| $selectQueryBuilder = $this->db->getQueryBuilder(); | ||
|
|
||
| $selectQueryBuilder->select('accounts.id') | ||
| ->from('mail_accounts', 'accounts') | ||
| ->leftJoin( | ||
| 'accounts', | ||
| 'mail_mailboxes', | ||
| 'mailboxes', | ||
| $selectQueryBuilder->expr()->eq( | ||
| "accounts.{$mailboxType}_mailbox_id", | ||
| 'mailboxes.id', | ||
| IQueryBuilder::PARAM_INT | ||
| ) | ||
| ) | ||
| ->where($selectQueryBuilder->expr()->isNull('mailboxes.id')) | ||
| ->andWhere($selectQueryBuilder->expr()->isNotNull("accounts.{$mailboxType}_mailbox_id")); | ||
|
|
||
| try { | ||
| $affectedRows = $selectQueryBuilder->executeQuery()->fetchAll(); | ||
|
|
||
| foreach ($affectedRows as $row) { | ||
| $updateQueryBuilder = $this->db->getQueryBuilder(); | ||
| $updateQueryBuilder->update('mail_accounts') | ||
| ->set( | ||
| "{$mailboxType}_mailbox_id", | ||
| $updateQueryBuilder->createNamedParameter(null, IQueryBuilder::PARAM_NULL) | ||
| ) | ||
| ->where( | ||
| $updateQueryBuilder->expr()->eq( | ||
| 'id', | ||
| $updateQueryBuilder->createNamedParameter($row['id'], IQueryBuilder::PARAM_INT) | ||
| ) | ||
| ); | ||
| $updateQueryBuilder->executeStatement(); | ||
| } | ||
| } catch (Exception $e) { | ||
| $this->logger->error("Emptying inconsistent {$mailboxType}_mailbox_id field failed", [ | ||
| 'exception' => $e | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param Table $accountsTable | ||
| * @param Table $mailboxesTable | ||
| * @param string $mailboxType | ||
| * @return void | ||
| */ | ||
| private function addMailboxKey(Table $accountsTable, Table $mailboxesTable, string $mailboxType): void { | ||
| $accountsTable->addForeignKeyConstraint( | ||
| $mailboxesTable, | ||
| ["{$mailboxType}_mailbox_id"], | ||
| ['id'], | ||
| [ | ||
| 'onDelete' => 'SET NULL', | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param IOutput $output | ||
| * @param Closure(): ISchemaWrapper $schemaClosure | ||
| * @param array $options | ||
| */ | ||
| #[Override] | ||
| public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
| $this->removeInconsistentMailboxEntries('drafts'); | ||
| $this->removeInconsistentMailboxEntries('sent'); | ||
| $this->removeInconsistentMailboxEntries('trash'); | ||
| $this->removeInconsistentMailboxEntries('junk'); | ||
| $this->removeInconsistentMailboxEntries('archive'); | ||
| $this->removeInconsistentMailboxEntries('snooze'); | ||
| } | ||
|
|
||
| /** | ||
| * @param IOutput $output | ||
| * @param Closure(): ISchemaWrapper $schemaClosure | ||
| * @param array $options | ||
| * @return null|ISchemaWrapper | ||
| */ | ||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if ($schema->hasTable('mail_accounts')) { | ||
DerDreschner marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: What's the best-practice with the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The checks shouldn't be necessary ideally, but in case of a migration error it should be possible to re-run that migration. Imagine a migration creates two tables: t1 and t2. Creating t2 failed (something temporary that the admin could resolve, or a patch is applied). The upgrade runs the same migration again, because the migration is not yet written to oc_migrations. This is why we add the table/column exists checks :) |
||
| $accountsTable = $schema->getTable('mail_accounts'); | ||
| $mailboxesTable = $schema->getTable('mail_mailboxes'); | ||
|
|
||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'drafts'); | ||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'sent'); | ||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'trash'); | ||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'junk'); | ||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'archive'); | ||
| $this->addMailboxKey($accountsTable, $mailboxesTable, 'snooze'); | ||
| } | ||
|
|
||
| return $schema; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.