Sha256: c7a403286ebe8a5b30f56c6494b27fd3c5a50391cef7d97249254298081f8d74

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

# This migration comes from spree (originally 20120315064358)
class MigrateImagesFromProductsToVariants < ActiveRecord::Migration
  def up
    images = select_all("SELECT spree_assets.* FROM spree_assets
                         WHERE spree_assets.type IN ('Spree::Image')
                         AND spree_assets.viewable_type = 'Spree::Product'")

    images.each do |image|
      master_variant_id = select_value("SELECT id FROM spree_variants
                                        WHERE product_id = #{image['viewable_id']}
                                        AND is_master = #{quoted_true}")

      execute("UPDATE spree_assets SET viewable_type = 'Spree::Variant', viewable_id = #{master_variant_id}
               WHERE id = #{image['id']}") if master_variant_id
    end
  end

  def down
    images = select_all("SELECT spree_assets.* FROM spree_assets
                         JOIN spree_variants
                         ON spree_variants.id = spree_assets.viewable_id
                         AND spree_variants.is_master = #{quoted_true}
                         WHERE spree_assets.type IN ('Spree::Image')
                         AND spree_assets.viewable_type = 'Spree::Variant'")

    images.each do |image|
      product_id = select_value("SELECT spree_products.id FROM spree_products
                                 JOIN spree_variants
                                 ON spree_variants.id = #{image['viewable_id']}
                                 AND spree_products.id = spree_variants.product_id")

      execute("UPDATE spree_assets SET viewable_type = 'Spree::Product', viewable_id = #{product_id}
               WHERE id = #{image['id']}") if product_id
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
datashift_spree-0.3.0 spec/sandbox/db/migrate/20121023154452_migrate_images_from_products_to_variants.spree.rb
datashift_spree-0.2.1 spec/sandbox/db/migrate/20121015151245_migrate_images_from_products_to_variants.spree.rb
datashift_spree-0.2.0 spec/sandbox/db/migrate/20120925192813_migrate_images_from_products_to_variants.spree.rb
datashift_spree-0.1.0 spec/sandbox/db/migrate/20120918081544_migrate_images_from_products_to_variants.spree.rb