Sha256: c31c729d11b92482e2192a930b468ff1ac874caeaa6eed2935d319e564419980

Contents?: true

Size: 950 Bytes

Versions: 141

Compression:

Stored size: 950 Bytes

Contents

class UniqueShippingMethodCategories < ActiveRecord::Migration[4.2]
  def change
    klass   = Spree::ShippingMethodCategory
    columns = %w[shipping_category_id shipping_method_id]

    say "Find duplicate #{klass} records"
    duplicates = klass.
      select((columns + %w[COUNT(*)]).join(',')).
      group(columns.join(',')).
      having('COUNT(*) > 1').
      map { |row| row.attributes.slice(*columns) }

    say "Delete all but the oldest duplicate #{klass} record"
    duplicates.each do |conditions|
      klass.where(conditions).order(:created_at).drop(1).each(&:destroy)
    end

    say "Add unique index to #{klass.table_name} for #{columns.inspect}"
    add_index klass.table_name, columns, unique: true, name: 'unique_spree_shipping_method_categories'

    say "Remove redundant simple index on #{klass.table_name}"
    remove_index klass.table_name, name: 'index_spree_shipping_method_categories_on_shipping_category_id'
  end
end

Version data entries

141 entries across 141 versions & 2 rubygems

Version Path
spree_core-4.0.9 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.1.15 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.7 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.3 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.6 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.2 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.0 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.0.rc3 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.0.rc2 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.3.0.rc1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.5 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.1.14 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.0.8 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.4 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-3.7.14.1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.3.1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.1.13.1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.0.7.1 db/migrate/20131211192741_unique_shipping_method_categories.rb
spree_core-4.2.3 db/migrate/20131211192741_unique_shipping_method_categories.rb