From 294aafc6ec36260066a998fe16085dfc3a8fa860 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 15 Nov 2021 07:15:21 +0000 Subject: [PATCH] Create command to generate the ids for existing customers. Create sql command to set generated ids for customers. #638 --- .../CreateCustomerGeneratedIdCommand.php | 106 ++++++++++++++++ src/Command/SetCustomerGeneratedIdCommand.php | 114 ------------------ .../load_customer_generated_ids.sql | 13 ++ 3 files changed, 119 insertions(+), 114 deletions(-) create mode 100644 src/Command/CreateCustomerGeneratedIdCommand.php delete mode 100644 src/Command/SetCustomerGeneratedIdCommand.php create mode 100644 utils/load_customer_generated_ids/load_customer_generated_ids.sql diff --git a/src/Command/CreateCustomerGeneratedIdCommand.php b/src/Command/CreateCustomerGeneratedIdCommand.php new file mode 100644 index 00000000..058711d2 --- /dev/null +++ b/src/Command/CreateCustomerGeneratedIdCommand.php @@ -0,0 +1,106 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:creategeneratedid') + ->setDescription('Create customer generated id and output to file.') + ->setHelp('Create customer generated id and output to file.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Output filename'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + $db = $em->getConnection(); + + $output_file = $input->getArgument('output_file'); + + // 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(); + + $output_info = []; + foreach ($customers as $row) + { + $cust = $row[0]; + $output->writeln('Processing customer ' . $cust->getID()); + + $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + + // set format of csv file customer id, generated id + $output_info[] = [ + $cust->getID(), + $generated_id, + ]; + + $em->detach($row[0]); + } + + // write to output file + $this->outputCustomerGeneratedIds($output_file, $output_info); + + return 0; + } + + protected function generateUniqueId($str_length) + { + $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $rand_string = ''; + $desired_length = 10; + + $rand_string = substr(str_shuffle($charset), 0, $desired_length); + + $salt = time() . $rand_string; + + return substr(md5($salt), 0, $str_length); + } + + protected function outputCustomerGeneratedIds($output_file, $entries) + { + try + { + $fh = fopen($output_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $report_file . '" could be opened.'); + } + + foreach($entries as $row) + { + fputcsv($fh, $row); + } + + fclose($fh); + } +} diff --git a/src/Command/SetCustomerGeneratedIdCommand.php b/src/Command/SetCustomerGeneratedIdCommand.php deleted file mode 100644 index b2518a14..00000000 --- a/src/Command/SetCustomerGeneratedIdCommand.php +++ /dev/null @@ -1,114 +0,0 @@ -em = $em; - - $this->loadGeneratedIds(); - - 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; - $db = $em->getConnection(); - - $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()); - - while (true) - { - $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); - - if (!isset($this->cust_generated_ids[$generated_id])) - { - $update_sql = 'UPDATE customer SET generated_id = :generated_id WHERE id = :cust_id'; - $update_stmt = $db->prepare($update_sql); - $update_stmt->execute([ - 'generated_id' => $generated_id, - 'cust_id' => $cust->getID(), - ]); - - // add generated id to hash - $this->cust_generated_ids[$generated_id] = $cust; - - break; - } - } - - $em->detach($row[0]); - } - - return 0; - } - - protected function generateUniqueId($str_length) - { - return substr(md5(time()), 0, $str_length); - } - - protected function loadGeneratedIds() - { - error_log('loading generated ids '); - $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); - $cust_iter = $cust_q->iterate(); - - $this->cust_generated_ids = []; - foreach ($cust_iter as $row) - { - $customer = $row[0]; - $generated_id = $customer->getGeneratedID(); - error_log('generated id = ' . $generated_id); - if ($generated_id != null) - { - error_log('generated id is not null ' . $generated_id); - if (!isset($this->cust_generated_ids[$generated_id])) - { - error_log('setting to hash ' . $generated_id); - $this->cust_generated_ids[$generated_id] = $customer; - } - } - - $this->em->detach($row[0]); - } - } -} diff --git a/utils/load_customer_generated_ids/load_customer_generated_ids.sql b/utils/load_customer_generated_ids/load_customer_generated_ids.sql new file mode 100644 index 00000000..dc66316c --- /dev/null +++ b/utils/load_customer_generated_ids/load_customer_generated_ids.sql @@ -0,0 +1,13 @@ +CREATE TABLE temp_table (id int(11), generated_id varchar(40)); + +LOAD DATA INFILE '/tmp/generated_ids.csv' +INTO TABLE temp_table +FIELDS TERMINATED BY ',' +ENCLOSED BY '"' +LINES TERMINATED BY '\n' +(id, generated_id); + +UPDATE customer INNER JOIN temp_table ON temp_table.id = customer.id +SET customer.generated_id = temp_table.generated_id; + +DROP TABLE temp_table;