Sha256: 65960259b01c5bded490d7063257ac4a101e7397da237133b9a7907eb0e0f12a

Contents?: true

Size: 1.32 KB

Versions: 37

Compression:

Stored size: 1.32 KB

Contents

class ChangeRolesToSupportPolymorphicAssociations < ActiveRecord::Migration
  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

37 entries across 37 versions & 1 rubygems

Version Path
blacklight-spotlight-0.34.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.34.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.33.3 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.33.2 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.33.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.33.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.32.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.31.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.30.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.29.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.29.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.28.3 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.28.2 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.28.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.28.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.27.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.26.1 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.26.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.25.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb
blacklight-spotlight-0.24.0 db/migrate/20151215141516_change_roles_to_support_polymorphic_associations.rb