Sha256: c65a130d0946fe642e58f8f205b94de2ebfcf40c10b39947b70a4326d596007d

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

# This migration comes from spree (originally 20090923100315)
class AddCountOnHandToVariantsAndProducts < ActiveRecord::Migration
  def up
    add_column :variants, :count_on_hand, :integer, :default => 0, :null => false
    add_column :products, :count_on_hand, :integer, :default => 0, :null => false

    # Due to our namespacing changes, this migration (from earlier Spree versions) is broken
    # To fix it, temporarily set table name on each of the models involved
    # And then...
    Spree::Variant.table_name = 'variants'
    Spree::Product.table_name = 'products'
    Spree::InventoryUnit.table_name = 'inventory_units'

    # In some cases needed to reflect changes in table structure
    Spree::Variant.reset_column_information
    Spree::Product.reset_column_information

    say_with_time 'Transfering inventory units with status on_hand to variants table...' do
      Spree::Variant.all.each do |v|
        v.update_column(:count_on_hand, v.inventory_units.with_state('on_hand').size)
        Spree::InventoryUnit.destroy_all(:variant_id => v.id, :state => 'on_hand')
      end
    end

    say_with_time 'Updating products count on hand' do
      Spree::Product.all.each do |p|
        product_count_on_hand = p.has_variants? ?
            p.variants.inject(0) { |acc, v| acc + v.count_on_hand } :
            (p.master ? p.master.count_on_hand : 0)
        p.update_column(:count_on_hand, product_count_on_hand)
      end
    end

    # ... Switch things back at the end of the migration
    Spree::Variant.table_name = 'spree_variants'
    Spree::Product.table_name = 'spree_products'
    Spree::InventoryUnit.table_name = 'spree_inventory_units'
  end

  def down
   Spree::Variant.all.each do |v|
      v.count_on_hand.times do
        Spree::InventoryUnit.create(:variant => variant, :state => 'on_hand')
      end
    end

    remove_column :variants, :count_on_hand
    remove_column :products, :count_on_hand
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
spree_mercado_pago_payment_method-0.0.2 spec/dummy/db/migrate/20121121030142_add_count_on_hand_to_variants_and_products.spree.rb
spree_mercado_pago_payment_method-0.1.1 spec/dummy/db/migrate/20121121030142_add_count_on_hand_to_variants_and_products.spree.rb
spree_mercado_pago_payment_method-0.1.0 spec/dummy/db/migrate/20121121030142_add_count_on_hand_to_variants_and_products.spree.rb
datashift_spree-0.3.0 spec/sandbox/db/migrate/20121023154361_add_count_on_hand_to_variants_and_products.spree.rb
datashift_spree-0.2.1 spec/sandbox/db/migrate/20121015151154_add_count_on_hand_to_variants_and_products.spree.rb
datashift_spree-0.2.0 spec/sandbox/db/migrate/20120925192722_add_count_on_hand_to_variants_and_products.spree.rb
datashift_spree-0.1.0 spec/sandbox/db/migrate/20120918081453_add_count_on_hand_to_variants_and_products.spree.rb