From da3af14b02d1808dca1777853d4af8373b919b0c Mon Sep 17 00:00:00 2001 From: Daniel Jackson Date: Thu, 26 Sep 2024 10:31:48 +0200 Subject: [PATCH] Fix heroku rate limits on memory usage? --- .../20240925180933_add_score_to_space_facility.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/db/migrate/20240925180933_add_score_to_space_facility.rb b/db/migrate/20240925180933_add_score_to_space_facility.rb index 06f46b8c..1134bbbb 100644 --- a/db/migrate/20240925180933_add_score_to_space_facility.rb +++ b/db/migrate/20240925180933_add_score_to_space_facility.rb @@ -2,12 +2,16 @@ class AddScoreToSpaceFacility < ActiveRecord::Migration[7.1] def up add_column :space_facilities, :score, :integer, default: 0 + total_count = SpaceFacility.count.to_i + processed_count = 0 # In a transaction, calculate score for all space facilities - SpaceFacility.transaction do - count = SpaceFacility.count.to_i - SpaceFacility.all.each_with_index do |space_facility, index| - print "\rCalculating score for #{index + 1} / #{count} " - space_facility.calculate_score + SpaceFacility.find_in_batches(batch_size: 200) do |space_facilities| + SpaceFacility.transaction do + space_facilities.each do |space_facility| + space_facility.calculate_score + processed_count += 1 + print "\rCalculated score for #{processed_count} / #{total_count}\r " + end end end end