From aa92dd877109cafcdbe6c89ecd3c0d5610fbc5cc Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Wed, 12 Dec 2018 01:27:52 +0800 Subject: [PATCH] Declare VariantOverride in migration for safe use --- ...simplify_variant_override_stock_settings.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/db/migrate/20181128054803_simplify_variant_override_stock_settings.rb b/db/migrate/20181128054803_simplify_variant_override_stock_settings.rb index ed42708d0d..7bb370d9f4 100644 --- a/db/migrate/20181128054803_simplify_variant_override_stock_settings.rb +++ b/db/migrate/20181128054803_simplify_variant_override_stock_settings.rb @@ -12,17 +12,27 @@ # Furthermore, this will allow all existing variant overrides to satisfy the newly added model # validation rules. class SimplifyVariantOverrideStockSettings < ActiveRecord::Migration + class VariantOverride < ActiveRecord::Base + scope :with_count_on_hand, -> { where("count_on_hand IS NOT NULL") } + scope :without_count_on_hand, -> { where(count_on_hand: nil) } + end + def up # When on_demand is nil but count_on_hand is set, force limited stock. - VariantOverride.where(on_demand: nil).where("count_on_hand IS NOT NULL") - .update_all(on_demand: false) + VariantOverride.where(on_demand: nil).with_count_on_hand.find_each do |variant_override| + variant_override.update_attributes!(on_demand: false) + end # Clear count_on_hand if forcing on demand. - VariantOverride.where(on_demand: true).update_all(count_on_hand: nil) + VariantOverride.where(on_demand: true).with_count_on_hand.find_each do |variant_override| + variant_override.update_attributes!(count_on_hand: nil) + end # When on_demand is false but count on hand is not specified, set this to use producer stock # settings. - VariantOverride.where(on_demand: false, count_on_hand: nil).update_all(on_demand: nil) + VariantOverride.where(on_demand: false).without_count_on_hand.find_each do |variant_override| + variant_override.update_attributes!(on_demand: nil) + end end def down; end