From 666e6481c87c82160a9ad8d32a34b1b1c2517690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0opf?= <petr.sopf@quanti.cz> Date: Mon, 20 May 2024 10:45:48 +0200 Subject: [PATCH] [PIN-6307] Make mysqlId non-unique --- migrations_odm/Version20240520083323.php | 43 +++++++++++++++++++ src/Document/Helper/MysqlIdField.php | 4 +- .../IndividualRepository.php | 5 ++- src/Utils/SmartcardService.php | 5 ++- 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 migrations_odm/Version20240520083323.php diff --git a/migrations_odm/Version20240520083323.php b/migrations_odm/Version20240520083323.php new file mode 100644 index 0000000000..c674a432b4 --- /dev/null +++ b/migrations_odm/Version20240520083323.php @@ -0,0 +1,43 @@ +<?php + +namespace App\MigrationsODM; + +use AntiMattr\MongoDB\Migrations\AbstractMigration; +use MongoDB\Collection; +use MongoDB\Database; + +class Version20240520083323 extends AbstractMigration +{ + public function getDescription(): string + { + return "Removes unique mysqlId indexes."; + } + + public function up(Database $db): void + { + $individualCollection = $db->selectCollection('Individual'); + $beneficiaryCollection = $db->selectCollection('Beneficiary'); + + $this->dropIndexIfExists($individualCollection, 'mysqlId_1'); + $this->dropIndexIfExists($beneficiaryCollection, 'mysqlId_1'); + $this->dropIndexIfExists($beneficiaryCollection, 'members.mysqlId_1'); + } + + private function dropIndexIfExists(Collection $collection, string $indexName): void + { + $indexes = $collection->listIndexes(); + foreach ($indexes as $index) { + if ($index->getName() === $indexName) { + echo "Dropping index '$indexName' from collection...\n"; + $collection->dropIndex($indexName); + echo "Index '$indexName' dropped from collection.\n"; + return; + } + } + } + + public function down(Database $db): void + { + // Not possible + } +} diff --git a/src/Document/Helper/MysqlIdField.php b/src/Document/Helper/MysqlIdField.php index 56237faedd..b0f940d1f4 100644 --- a/src/Document/Helper/MysqlIdField.php +++ b/src/Document/Helper/MysqlIdField.php @@ -5,12 +5,12 @@ namespace Document\Helper; use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; -use Doctrine\ODM\MongoDB\Mapping\Annotations\UniqueIndex; +use Doctrine\ODM\MongoDB\Mapping\Annotations\Index; trait MysqlIdField { #[Field] - #[UniqueIndex(sparse: true)] + #[Index(name: "mysqlId_nonUnique", sparse: true)] protected int | null $mysqlId = null; #[Field] diff --git a/src/DocumentRepository/IndividualRepository.php b/src/DocumentRepository/IndividualRepository.php index 8ccda2d216..19ee31184e 100644 --- a/src/DocumentRepository/IndividualRepository.php +++ b/src/DocumentRepository/IndividualRepository.php @@ -384,7 +384,10 @@ public function getById(string $individualId): Individual */ public function getByLegacyId(int $individualId): Individual { - $individual = $this->findOneBy(['mysqlId' => $individualId]); + $individual = $this->findOneBy( + ['mysqlId' => $individualId], + ['updatedAt' => 'desc'], + ); if ($individual === null) { throw DocumentNotFoundException::documentNotFound(Individual::class, $individualId); diff --git a/src/Utils/SmartcardService.php b/src/Utils/SmartcardService.php index 4a412112d3..6e4cacc688 100644 --- a/src/Utils/SmartcardService.php +++ b/src/Utils/SmartcardService.php @@ -109,7 +109,10 @@ public function register(SmartcardRegisterInputType $registerInputType): Smartca /** @var Individual | null $individual */ $individual = is_numeric($individualId) ? - $this->individualRepository->findOneBy(['mysqlId' => (int) $individualId]) + $this->individualRepository->findOneBy( + ['mysqlId' => (int) $individualId], + ['updatedAt' => 'desc'], + ) : $this->individualRepository->find($individualId); $smartcardBeneficiary = $this->getOrCreateActiveSmartcardForBeneficiary( -- GitLab