Sha256: a05684023a8d1b944dbb66035c204912293e2b695aecd204879bb6348164bda0

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

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

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-1.1.6 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.5 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.4 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.3 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.2 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.2.rc1 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.1 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.0 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb
spree_core-1.1.0.rc2 db/migrate/20120315064358_migrate_images_from_products_to_variants.rb