Sha256: caa6f40271334975ca1e56fdd7bc7a0927252ba5b9432c1a0a8e8fbf49d84b3a

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

class ReplaceUserByPolymorphicResourceInCandidatures < ActiveRecord::Migration
  def up
    add_column :vacancies, :resource_type, :string
    add_column :vacancies, :resource_id, :integer
    
    Vacancy.where('user_id IS NOT NULL').update_all(resource_type: "User")
    Vacancy.where('user_id IS NOT NULL').update_all('resource_id = user_id')
    
    remove_column :vacancies, :user_id
    
    add_column :candidatures, :resource_type, :string
    add_column :candidatures, :resource_id, :integer
    
    Candidature.where('user_id IS NOT NULL').update_all(resource_type: "User")
    Candidature.where('user_id IS NOT NULL').update_all('resource_id = user_id')
    
    remove_column :candidatures, :user_id
    
    begin
      remove_index :candidatures, [:user_id, :vacancy_id]
    rescue
    end
    
    add_index :candidatures, [:resource_id, :resource_type, :vacancy_id], unique: true, name: 'index_candidatures_on_resource_and_vacancy'
  end

  def down
    add_column :vacancies, :user_id, :integer
    
    Vacancy.where('resource_type = "User"').update_all('user_id = resource_id')
    Vacancy.where('resource_type <> "User"').destroy_all
    
    remove_column :vacancies, :resource_type
    remove_column :vacancies, :resource_id
    
    add_column :candidatures, :user_id, :integer
    
    add_index :candidatures, [:user_id, :vacancy_id]
    
    Candidature.where('resource_type = "User"').update_all('user_id = resource_id')
    Candidature.where('resource_type <> "User"').destroy_all
    
    remove_column :candidatures, :resource_type
    remove_column :candidatures, :resource_id
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voluntary-0.4.0 db/migrate/20131018143613_replace_user_by_polymorphic_resource_in_candidatures.rb