diff --git a/db/migrate/20231030102203_add_original_batch_id_to_samples.rb b/db/migrate/20231030102203_add_original_batch_id_to_samples.rb index d1cf2eaec..356773572 100644 --- a/db/migrate/20231030102203_add_original_batch_id_to_samples.rb +++ b/db/migrate/20231030102203_add_original_batch_id_to_samples.rb @@ -3,10 +3,24 @@ def change add_column :samples, :original_batch_id, :integer Sample.preload(:batch).find_each do |sample| - batch = sample.batch || Batch.find_by(batch_number: sample.old_batch_number) - if batch - sample.original_batch_id = batch.id + if sample.batch + sample.original_batch_id = sample.batch.id sample.save + elsif sample.box_id && sample.old_batch_number + institution_ids = TransferPackage + .select(:sender_institution_id) + .joins(:box_transfers) + .where(box_transfers: { box_id: sample.box_id }) + .distinct + + original_batch = Batch.where( + institution_id: institution_ids, + batch_number: sample.old_batch_number + ).take + if original_batch + sample.original_batch_id = original_batch.id + sample.save + end end end end