From 85483fb744d963d485de3bd079fc7911e22054c1 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 3 Oct 2023 22:08:46 +0800 Subject: [PATCH] Added batch update for rider table. #762 --- src/Command/GetJobOrderArchiveDataCommand.php | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Command/GetJobOrderArchiveDataCommand.php b/src/Command/GetJobOrderArchiveDataCommand.php index 1ff9ac6d..a6540080 100644 --- a/src/Command/GetJobOrderArchiveDataCommand.php +++ b/src/Command/GetJobOrderArchiveDataCommand.php @@ -174,16 +174,23 @@ class GetJobOrderArchiveDataCommand extends Command } } - // TODO: can we maybe find a way to not load the archive files successively? - // we get the memory exception after loading the data to the last table, jo_rejection. $this->loadArchiveFiles($archive_files, $year); - // update rider's active_jo_id and current_jo_id to null - // so we can delete the old job orders (foreign key constraint) - $this->updateRiderJobOrders($jo_id_list); + error_log('Done loading files into database...'); - // at this point, all the job order and related data have been archived into the database - $this->deleteData($jo_id_list, $related_tables); + $jo_id_array = array_chunk($jo_id_list, 10000); + + unset($jo_id_list); + + foreach($jo_id_array as $key => $jo_ids) + { + // update rider's active_jo_id and current_jo_id to null + // so we can delete the old job orders (foreign key constraint) + $this->updateRiderActiveJobOrders($jo_ids); + $this->updateRiderCurrentJobOrders($jo_ids); + + $this->deleteData($jo_ids, $related_tables); + } } protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year) @@ -279,6 +286,8 @@ class GetJobOrderArchiveDataCommand extends Command if (count($ii_id_list) > 0) $this->deleteInvoiceItems($ii_id_list); + unset($ii_id_list); + return $file; } @@ -848,23 +857,38 @@ class GetJobOrderArchiveDataCommand extends Command $jo_stmt->execute($jo_id_list); } - protected function updateRiderJobOrders($jo_id_list) + protected function updateRiderActiveJobOrders($jo_id_list) { $db = $this->em->getConnection(); + $db->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); + + $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; + + $update_active_sql = 'UPDATE rider SET active_jo_id = NULL WHERE active_jo_id IN (' . $jo_ids . ')'; + $update_active_stmt = $db->prepare($update_active_sql); + + error_log('Updating active rider job orders...'); + + $update_active_stmt->execute($jo_id_list); + + unset($update_active_stmt); + } + + protected function updateRiderCurrentJobOrders($jo_id_list) + { + $db = $this->em->getConnection(); + $db->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; $update_curr_sql = 'UPDATE rider SET current_jo_id = NULL WHERE current_jo_id IN (' . $jo_ids . ')'; $update_curr_stmt = $db->prepare($update_curr_sql); - $update_active_sql = 'UPDATE rider SET active_jo_id = NULL WHERE active_jo_id IN (' . $jo_ids . ')'; - $update_active_stmt = $db->prepare($update_active_sql); + error_log('Updating current rider job orders...'); $update_curr_stmt->execute($jo_id_list); - $update_active_stmt->execute($jo_id_list); unset($update_curr_stmt); - unset($update_active_stmt); } protected function loadDataFileForArchiveData($load_stmt)