Sha256: b154f9ae1c83c07d61fb8b27babaae81d8eac2d4f216e18879869d26604e809f

Contents?: true

Size: 662 Bytes

Versions: 8

Compression:

Stored size: 662 Bytes

Contents

# This migration comes from spree (originally 20170331121725)
class AddIndexToRoleUsers < ActiveRecord::Migration[5.0]
  def change

    duplicates = Spree::RoleUser.group(:role_id, :user_id).having('sum(1) > 1').size

    duplicates.each do |f|
      role_id, user_id = f.first
      count = f.last - 1 # we want to leave one record
      roles = Spree::RoleUser.where(role_id: role_id, user_id: user_id).last(count)
      roles.map(&:destroy)
    end

    if index_exists? :spree_role_users, [:role_id, :user_id]
      remove_index :spree_role_users, [:role_id, :user_id]
      add_index :spree_role_users, [:role_id, :user_id], unique: true
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
spree_purchase_order-3.7.0 spec/dummy/db/migrate/20191113195960_add_index_to_role_users.spree.rb
spree_billing_sisow-0.9.2 spec/dummy/db/migrate/20190729091901_add_index_to_role_users.spree.rb
spree_billing_sisow-0.9.1 spec/dummy/db/migrate/20190729091901_add_index_to_role_users.spree.rb
spree_purchase_order-3.6.0 spec/dummy/db/migrate/20180516191825_add_index_to_role_users.spree.rb
spree_purchase_order-3.5.0 spec/dummy/db/migrate/20180516191825_add_index_to_role_users.spree.rb
spree_purchase_order-3.5.0.rc1 spec/dummy/db/migrate/20180516191825_add_index_to_role_users.spree.rb
spree_purchase_order-3.4.0 spec/dummy/db/migrate/20180516191825_add_index_to_role_users.spree.rb
spree_purchase_order-3.3.0 spec/dummy/db/migrate/20180516191825_add_index_to_role_users.spree.rb