From 67429113ddbd821dbf0374b0c3b2be7080c98672 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 11 Nov 2021 10:35:18 +0000 Subject: [PATCH] Add command to set generated id for existing customers. #638 --- config/services.yaml | 5 + src/Command/SetCustomerGeneratedIdCommand.php | 110 ++++++++++++++++++ src/Service/UniqueIdGenerator.php | 2 + 3 files changed, 117 insertions(+) create mode 100644 src/Command/SetCustomerGeneratedIdCommand.php diff --git a/config/services.yaml b/config/services.yaml index 0498fcdd..65576192 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -301,3 +301,8 @@ services: App\Service\HubFilteringGeoChecker: arguments: $geofence_flag: "%env(HUB_GEOFENCE_ENABLE)%" + + # customer id generator + App\Service\UniqueIdGenerator: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/SetCustomerGeneratedIdCommand.php b/src/Command/SetCustomerGeneratedIdCommand.php new file mode 100644 index 00000000..f031ca08 --- /dev/null +++ b/src/Command/SetCustomerGeneratedIdCommand.php @@ -0,0 +1,110 @@ +em = $em; + $this->id_gen = $id_gen; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:setgeneratedid') + ->setDescription('Set customer generated id.') + ->setHelp('Set customer generated id.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + + $batch_size = 20; + $i = 1; + + $em->getConnection()->getConfiguration()->setSQLLogger(null); + + // get all customers with no generated ids. First time to run this would mean all customers + $cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null'); + + $customers = $cust_q->iterate(); + foreach ($customers as $row) + { + $cust = $row[0]; + $output->writeln('Processing customer ' . $cust->getID()); + + // retry until we get a unique generated id + while (true) + { + try + { + $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + + // set generated id + $cust->setGeneratedId($generated_id); + + // reopen in case we get an exception + if (!$em->isOpen()) + { + $em = $em->create( + $em->getConnection(), + $em->getConfiguration() + ); + } + + //++$i; + //if (($i % $batch_size) === 0) + //{ + $em->flush(); + $em->clear(); + //} + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + $em->detach($row[0]); + } + + //$em->flush(); + + return 0; + } + + protected function generateUniqueId($str_length) + { + return substr(md5(time()), 0, $str_length); + } + +} diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 3cd11f26..90af3369 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -30,6 +30,8 @@ class UniqueIdGenerator if ($cust == null) return $generated_id; + + sleep(1); } }