Skip to content

Commit

Permalink
fix: broken email shares
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Aug 23, 2024
1 parent 356f1df commit 01339bb
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/Migration/Version0721Date20240823135515.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Migration;

use OCA\Polls\Db\Share;
use OCA\Polls\Db\Vote;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/*
*@psalm-suppress UnusedClass
*/

/**
* Installation class for the polls app.
* Initial db creation
* Changed class naming: Version[jjmmpp]Date[YYYYMMDDHHMMSS]
* Version: jj = major version, mm = minor, pp = patch
*/
class Version0721Date20240823135515 extends SimpleMigrationStep {

Check failure on line 29 in lib/Migration/Version0721Date20240823135515.php

View workflow job for this annotation

GitHub Actions / Psalm (stable27, 8.1)

UnusedClass

lib/Migration/Version0721Date20240823135515.php:29:7: UnusedClass: Class OCA\Polls\Migration\Version0721Date20240823135515 is never used (see https://psalm.dev/075)

private ISchemaWrapper $schema;
public function __construct(
private IDBConnection $connection
) {
}


public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
$qb = $this->connection->getQueryBuilder();
$userIds = $qb->selectDistinct('user_id')
->from(Vote::TABLE)
->executeQuery()
->fetchAll();

$userIds = array_map(fn ($row) => $row['user_id'], $userIds);

$qb = $this->connection->getQueryBuilder();
$qb->select('*')
->from(Share::TABLE)
->where($qb->expr()->eq('type', $qb->createNamedParameter('external', IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->notIn('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)))
->andWhere($qb->expr()->in('email_address', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));

$affectedShares = $qb->executeQuery()->fetchAll();

foreach ($affectedShares as $share) {
$this->renameUserIdAndType($share['user_id'], $share['email_address']);
}
}


public function renameUserIdAndType(string $userId, string $replacementName): void {
$query = $this->connection->getQueryBuilder();
$query->update(Share::TABLE)
->set('user_id', $query->createNamedParameter($replacementName))
->set('type', $query->createNamedParameter('email'))
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter('external')))
->executeStatement();
}
}

0 comments on commit 01339bb

Please sign in to comment.