Sha256: 84109be3c6ebd9579b62150f8558a54d66ccf3d812adf0d466313e447554f2c2

Contents?: true

Size: 994 Bytes

Versions: 21

Compression:

Stored size: 994 Bytes

Contents

class SplitPricesFromVariants < ActiveRecord::Migration[4.2]
  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

21 entries across 21 versions & 1 rubygems

Version Path
solidus_core-2.3.1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.2.2 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.1.1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.0.3 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.3.0 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.3.0.rc3 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.3.0.rc2 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.3.0.rc1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.3.0.beta1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.0.2 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.2.1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.2.0 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.2.0.rc1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.2.0.beta1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.1.0 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.1.0.rc1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.0.1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.1.0.beta1 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.0.0 db/migrate/20121031162139_split_prices_from_variants.rb
solidus_core-2.0.0.rc1 db/migrate/20121031162139_split_prices_from_variants.rb