-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hamza Mahjoubi <[email protected]>
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 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
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 GitHub Actions / Psalm (stable27, 8.1)UnusedClass
|
||
|
||
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(); | ||
} | ||
} |