From f19200728a7adfb749105b4be27dbb102d98ed0a Mon Sep 17 00:00:00 2001 From: "Restarone Solutions Inc. Software Engineering" Date: Sat, 13 Dec 2025 15:21:58 -0500 Subject: [PATCH] fix: optimize database queries to use find_each instead of all.each - Replace ActiveStorage::Attachment.all.each with find_each in subdomain.rb - Replace ActiveStorage::Blob.all.each with find_each in subdomain.rb - Replace executed_api_actions.where(...).each with find_each in api_namespace.rb - Improves memory efficiency for large datasets by processing records in batches - Prevents memory exhaustion when dealing with large numbers of attachments/blobs This addresses performance issues with large dataset processing. --- app/models/api_namespace.rb | 2 +- app/models/subdomain.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/api_namespace.rb b/app/models/api_namespace.rb index 5f1bc94cb..7a9487dba 100755 --- a/app/models/api_namespace.rb +++ b/app/models/api_namespace.rb @@ -146,7 +146,7 @@ def redirect_actions(trigger) end def rerun_api_actions - executed_api_actions.where(lifecycle_stage: 'failed').each(&:execute_action) + executed_api_actions.where(lifecycle_stage: 'failed').find_each(&:execute_action) end def discard_failed_actions diff --git a/app/models/subdomain.rb b/app/models/subdomain.rb index 74690fe8e..db0b9dc02 100755 --- a/app/models/subdomain.rb +++ b/app/models/subdomain.rb @@ -235,8 +235,8 @@ def change_2fa_setting def purge_stored_files Apartment::Tenant.switch(self.name) do - ActiveStorage::Attachment.all.each { |attachment| attachment.purge } - ActiveStorage::Blob.all.each { |blob| blob.purge } + ActiveStorage::Attachment.find_each { |attachment| attachment.purge } + ActiveStorage::Blob.find_each { |blob| blob.purge } end end