Sha256: d718dd39cdf67e51a041a20b905823a50499c4f7c21274c1d314adde4c85a3bc

Contents?: true

Size: 1.01 KB

Versions: 137

Compression:

Stored size: 1.01 KB

Contents

class SplitPricesFromVariants < ActiveRecord::Migration
  def up
    create_table :spree_prices do |t|
      t.integer :variant_id, :null => false
      t.decimal :amount, :precision => 8, :scale => 2, :null => false
      t.string :currency
    end

    Spree::Variant.all.each do |variant|
      Spree::Price.create!(
        :variant_id => variant.id,
        :amount => variant[:price],
        :currency => Spree::Config[:currency]
      )
    end

    remove_column :spree_variants, :price
  end

  def down
    prices = ActiveRecord::Base.connection.execute("select variant_id, amount from spree_prices")
    add_column :spree_variants, :price, :decimal, :after => :sku, :scale => 2, :precision => 8

    prices.each do |price|
      ActiveRecord::Base.connection.execute("update spree_variants set price = #{price['amount']} where id = #{price['variant_id']}")
    end
    
    change_column :spree_variants, :price, :decimal, :after => :sku, :scale => 2, :precision => 8, :null => false
    drop_table :spree_prices
  end
end

Version data entries

137 entries across 137 versions & 2 rubygems

Version Path
spree_core-3.1.14 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.13 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.12 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-1.2.3 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-1.1.4 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-1.0.7 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.11 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.10 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.9 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.8 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.7 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.6 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.5 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.4 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.3 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.2 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.0.10 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.1 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.0 db/migrate/20121031162139_split_prices_from_variants.rb
spree_core-3.1.0.rc4 db/migrate/20121031162139_split_prices_from_variants.rb