Sha256: ad585aac9197317de2148d54d68e7c8a8f392681190a0e01dca2f475810e2a6b

Contents?: true

Size: 1.23 KB

Versions: 57

Compression:

Stored size: 1.23 KB

Contents

class AddMissingUniqueIndexesForUniqueAttributes < ActiveRecord::Migration[5.0]
  def change
    tables = {
      country: [:name, :iso_name],
      refund_reason: [:name],
      reimbursement_type: [:name],
      return_authorization_reason: [:name],
      role: [:name],
      store: [:code]
    }

    tables.each do |table, columns|
      table_class = "Spree::#{table.to_s.classify}".constantize
      table_name = table_class.table_name

      columns.each do |column|
        unless index_exists?(table_name, column, unique: true)
          attributes = table_class.unscoped.group(column).having('sum(1) > 1').pluck(column)
          instances = table_class.where(column => [nil, attributes])

          instances.find_each do |instance|
            column_value = 'Unique String ' + SecureRandom.urlsafe_base64(8).upcase.delete('/+=_-')[0, 8]
            instance.send("#{column}=", column_value)
            instance.save
          end

          remove_index table_name, column if index_exists?(table_name, column)
          if supports_expression_index?
            add_index table_name, "(lower(#{column}))", unique: true
          else
            add_index table_name, column, unique: true
          end
        end
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

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