aboutsummaryrefslogtreecommitdiff
blob: c71032dcbff8a422fe8c8b94133128aba5c1ab64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class AddQuestionCategoryPivot < ActiveRecord::Migration
  def self.up
    create_table :question_categories do |t|
      t.integer :question_id, :null => false
      t.integer :category_id, :null => false
    end
    add_index :question_categories, [:question_id, :category_id], :unique => true

    execute 'INSERT INTO question_categories (question_id, category_id)
             SELECT id, category_id FROM questions WHERE category_id IS NOT NULL'

    remove_column :questions, :category_id

    remove_index :questions, :name => :index_questions_on_category_id rescue ActiveRecord::StatementInvalid
  end

  def self.down
    add_column :questions, :category_id, :integer

    drop_table :question_categories

    add_index :questions, [:category_id]
  end
end