Sha256: 32110a3e828de67b9a6f6d2135d8233dd4e469115c5db84da10eedaa1f26a5bc

Contents?: true

Size: 1.84 KB

Versions: 24

Compression:

Stored size: 1.84 KB

Contents

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_attribute(: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_attribute(: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

24 entries across 24 versions & 3 rubygems

Version Path
spree_core-1.0.7 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.6 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.7 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.2.rc1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.6 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.5 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.4 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.0 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.4 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.0.rc2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.1.0.rc1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.3 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_pag_seguro-1.0.2 spec/dummy/db/migrate/20120301154052_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.3 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_flat_in_range-0.1.0 spec/dummy/db/migrate/20120218160359_add_count_on_hand_to_variants_and_products.rb
spree_core-1.0.0 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb