Sha256: 8babd4a6774331c30a6c4359dfefce41b9017b90b68df3f84b192ec17a050c1e

Contents?: true

Size: 1.2 KB

Versions: 120

Compression:

Stored size: 1.2 KB

Contents

class CreateDefaultStock < ActiveRecord::Migration[4.2]
  def up
    unless column_exists? :spree_stock_locations, :default
      add_column :spree_stock_locations, :default, :boolean, null: false, default: false
    end

    Spree::StockLocation.skip_callback(:create, :after, :create_stock_items)
    Spree::StockLocation.skip_callback(:save, :after, :ensure_one_default)
    location = Spree::StockLocation.new(name: 'default')
    location.save(validate: false)

    Spree::Variant.find_each do |variant|
      stock_item = Spree::StockItem.unscoped.build(stock_location: location, variant: variant)
      stock_item.send(:count_on_hand=, variant.count_on_hand)
      # Avoid running default_scope defined by acts_as_paranoid, related to #3805,
      # validations would run a query with a delete_at column that might not be present yet
      stock_item.save! validate: false
    end

    remove_column :spree_variants, :count_on_hand
  end

  def down
    add_column :spree_variants, :count_on_hand, :integer

    Spree::StockItem.find_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

Version data entries

120 entries across 120 versions & 1 rubygems

Version Path
spree_core-4.0.9 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.1.15 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.7 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.3 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.6 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.2 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.0 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.0.rc3 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.0.rc2 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.3.0.rc1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.5 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.1.14 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.0.8 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.4 db/migrate/20130213191427_create_default_stock.rb
spree_core-3.7.14.1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.3.1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.1.13.1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.0.7.1 db/migrate/20130213191427_create_default_stock.rb
spree_core-4.2.3 db/migrate/20130213191427_create_default_stock.rb