Sha256: 78f62361b686c36ce9a5b71f5e78296441a4e58df2dcbccaf514330dc8fabf10

Contents?: true

Size: 1.32 KB

Versions: 107

Compression:

Stored size: 1.32 KB

Contents

class ChangeRolesToSupportPolymorphicAssociations < ActiveRecord::Migration[4.2]
  def up
    add_column :spotlight_roles, :resource_id, :integer
    add_column :spotlight_roles, :resource_type, :string

    migrate_role_data_to_polymorphic_resource

    remove_index :spotlight_roles, [:exhibit_id, :user_id]
    remove_column :spotlight_roles, :exhibit_id

    add_index :spotlight_roles, [:resource_type, :resource_id, :user_id], unique: true, name: 'index_spotlight_roles_on_resource_and_user_id'
  end

  def down
    add_column :spotlight_roles, :exhibit_id, :integer
    add_index(:spotlight_roles, [:exhibit_id])

    Spotlight::Role.reset_column_information

    Spotlight::Role.find_each do |e|
      e.update(exhibit_id: e.resource_id) if e.exhibit_id.nil? && e.resource_type == 'Spotlight::Exhibit'
    end
    
    remove_index :spotlight_roles, name: 'index_spotlight_roles_on_resource_and_user_id'

    remove_column :spotlight_roles, :resource_id
    remove_column :spotlight_roles, :resource_type

    add_index :spotlight_roles, [:exhibit_id, :user_id], unique: true
  end

  private

  def migrate_role_data_to_polymorphic_resource
    Spotlight::Role.reset_column_information

    Spotlight::Role.find_each do |e|
      e.update(resource_id: e.exhibit_id, resource_type: 'Spotlight::Exhibit') unless e.resource_id
    end
  end
end

Version data entries

107 entries across 107 versions & 1 rubygems

Version Path
blacklight-spotlight-4.6.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.6.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.5.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.4.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.6 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.5 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.4 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.3 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.2 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.3.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.2.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.1.2 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.1.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.1.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.0.3 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.0.2 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.0.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-4.0.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-3.6.0.beta10 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb