Sha256: bdbef3c580ae8f19dca82e8f3c172213d72872b0b9335107bc1263689563de14

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

class CreateSpreeProductsStores < ActiveRecord::Migration[5.2]
  def up
    unless table_exists?(:spree_products_stores)
      create_table :spree_products_stores do |t|
        t.references :product, index: true
        t.references :store,  index: true
        t.timestamps

        t.index [:product_id, :store_id], unique: true
      end

      stores = Spree::Store.all
      product_ids = Spree::Product.with_deleted.order(:id).ids

      stores.find_each do |store|
        prepared_values = product_ids.map { |id| "(#{id}, #{store.id}, '#{Time.current.to_s(:db)}', '#{Time.current.to_s(:db)}')" }.join(', ')
        next if prepared_values.empty?

        begin
          execute "INSERT INTO spree_products_stores (product_id, store_id, created_at, updated_at) VALUES #{prepared_values};"
        rescue ActiveRecord::RecordNotUnique; end
      end
    end
  end

  def down
    drop_table :spree_products_stores
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_core-4.3.0.rc1 db/migrate/20210527094055_create_spree_products_stores.rb