class CreateDefaultStock < ActiveRecord::Migration
  def up
    location = Spree::StockLocation.create(name: 'default')
    Spree::Variant.all.each do |variant|
      location.stock_items.create(variant: variant, count_on_hand: variant.count_on_hand)
    end

    remove_column :spree_variants, :count_on_hand
  end

  def down
    add_column :spree_variants, :count_on_hand, :integer

    Spree::StockItem.all.each do |stock_item|
      stock_item.variant.update_column :count_on_hand, stock_item.count_on_hand
    end

    Spree::StockLocation.delete_all
    Spree::StockItem.delete_all
  end
end

class CreateDefaultStock < ActiveRecord::Migration
  def up
    Spree::StockLocation.skip_callback(:create, :after, :create_stock_items)
    Spree::StockItem.skip_callback(:save, :after, :process_backorders)
    location = Spree::StockLocation.create(name: 'default')
    Spree::Variant.all.each do |variant|
      stock_item = location.stock_items.build(variant: variant)
      stock_item.send(:count_on_hand=, variant.count_on_hand)
      stock_item.save!
    end

    remove_column :spree_variants, :count_on_hand
  end

  def down
    add_column :spree_variants, :count_on_hand, :integer

    Spree::StockItem.all.each do |stock_item|
      stock_item.variant.update_column :count_on_hand, stock_item.count_on_hand
    end

    Spree::StockLocation.delete_all
    Spree::StockItem.delete_all
  end
end

Change migration to avoid tiggering callback and use private send:
Change migration to avoid tiggering callback and use private send:

* Skip :after callbacks on StockLocation and StockItem when creating
intitial instances. Regression due to 0ba60a08f934af9d6a7f17f6e9c90a1749547e9b and 630afbb7f2549556fb1bc914e16a2e3802cc8f15
* Use private send to set StockItem.count_on_hand
because :count_on_hand= is now private. Regression due to 9acf57f6baf55993ca0f4f032e9b7ad684712c9b

[Closes #2789]
