Sha256: 0bce82daa94aa9247d8d035f59834b73fcfb919fdea0e91512dcaad7d458e9d7

Contents?: true

Size: 625 Bytes

Versions: 1

Compression:

Stored size: 625 Bytes

Contents

class AddValidationIndexesToGemgentoProducts < ActiveRecord::Migration
  def change
    add_index :gemgento_products, :magento_id, unique: true
    add_index :gemgento_products, [:sku, :deleted_at], unique: true
  end

  def migrate(direction)
    if direction == :up
      # remove duplicates on magento_id
      products = Gemgento::Product.order(:deleted_at)
      grouped = products.group_by{ |model| model.magento_id }

      grouped.values.each do |duplicates|
        next if duplicates.size == 1
        original = duplicates.shift
        duplicates.each { |dup| dup.destroy }
      end
    end

    super
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 db/migrate/20151201231300_add_validation_indexes_to_gemgento_products.rb