From 413d2eed91e18aa154f3a161b747847c72f2ed09 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 23 Jan 2024 05:30:42 -0800 Subject: [PATCH] Improve species/color name migration performance & correctness Oh right, this should only run in the up direction! And also we can get a lot faster by preloading the translations. --- ...0123125509_add_name_to_species_and_color.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/db/migrate/20240123125509_add_name_to_species_and_color.rb b/db/migrate/20240123125509_add_name_to_species_and_color.rb index d1c7a152..0ced4e8b 100644 --- a/db/migrate/20240123125509_add_name_to_species_and_color.rb +++ b/db/migrate/20240123125509_add_name_to_species_and_color.rb @@ -3,14 +3,18 @@ class AddNameToSpeciesAndColor < ActiveRecord::Migration[7.1] add_column :species, :name, :string, null: false add_column :colors, :name, :string, null: false - Species.find_each do |species| - species.name = species.translation_for(:en).name - species.save! - end + reversible do |direction| + direction.up do + Species.includes(:translations).find_each do |species| + species.name = species.translation_for(:en).name + species.save! + end - Color.find_each do |color| - color.name = color.translation_for(:en).name - color.save! + Color.includes(:translations).find_each do |color| + color.name = color.translation_for(:en).name + color.save! + end + end end end end