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