Commit 3a77473350d575b98668a8658d64ed9c851049d5

Authored by Luke Baker
1 parent c8a96d31

update to choices to act_as_versioned

CHANGELOG.md
  1 + * Update choices to act_as_versioned
  2 +
1 3 ## Pairwise 3.0.1 (Apr 16, 2012) ###
2 4  
3 5 * Added votes_per_uploaded_choice call to API
... ...
app/models/choice.rb
1 1 class Choice < ActiveRecord::Base
  2 + acts_as_versioned
2 3  
3 4 belongs_to :question, :counter_cache => true
4 5 belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id"
... ...
db/migrate/20120426144214_add_version_to_choice.rb 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +class AddVersionToChoice < ActiveRecord::Migration
  2 + def self.up
  3 + # if you want to manually run the query to create
  4 + # all the initial versions in choice_versions table set this to true
  5 + run_query_manually = true
  6 + # default of 1 so all existing rows have a version of 1
  7 + add_column :choices, :version, :integer, :default => 1
  8 + # make version nil by default
  9 + change_column :choices, :version, :integer, :default => nil
  10 + Choice.create_versioned_table
  11 + query = "INSERT INTO choice_versions (SELECT null, id, version, item_id, question_id, position, ratings, created_at, updated_at, request_id, prompt_id, active, tracking, score, local_identifier, prompts_on_the_left_count, prompts_on_the_right_count, wins, losses, prompts_count, data, creator_id FROM choices)"
  12 + if run_query_manually
  13 + puts "!!!!!!!!!!!!!!!"
  14 + puts "RUN THIS QUERY:"
  15 + puts "!!!!!!!!!!!!!!!"
  16 + puts ""
  17 + puts query
  18 + puts ""
  19 + else
  20 + Choice.connection.execute(query)
  21 + end
  22 + end
  23 +
  24 + def self.down
  25 + remove_column :choices, :version
  26 + Choice.drop_versioned_table
  27 + end
  28 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20111017171903) do
  12 +ActiveRecord::Schema.define(:version => 20120426144214) do
13 13  
14 14 create_table "appearances", :force => true do |t|
15 15 t.integer "voter_id"
... ... @@ -30,6 +30,32 @@ ActiveRecord::Schema.define(:version =&gt; 20111017171903) do
30 30 add_index "appearances", ["prompt_id"], :name => "index_appearances_on_prompt_id"
31 31 add_index "appearances", ["question_id", "voter_id"], :name => "index_appearances_on_question_id_voter_id"
32 32  
  33 + create_table "choice_versions", :force => true do |t|
  34 + t.integer "choice_id"
  35 + t.integer "version"
  36 + t.integer "item_id"
  37 + t.integer "question_id"
  38 + t.integer "position"
  39 + t.integer "ratings"
  40 + t.datetime "created_at"
  41 + t.datetime "updated_at"
  42 + t.integer "request_id"
  43 + t.integer "prompt_id"
  44 + t.boolean "active", :default => false
  45 + t.text "tracking"
  46 + t.float "score"
  47 + t.string "local_identifier"
  48 + t.integer "prompts_on_the_left_count", :default => 0
  49 + t.integer "prompts_on_the_right_count", :default => 0
  50 + t.integer "wins", :default => 0
  51 + t.integer "losses", :default => 0
  52 + t.integer "prompts_count", :default => 0
  53 + t.string "data"
  54 + t.integer "creator_id"
  55 + end
  56 +
  57 + add_index "choice_versions", ["choice_id"], :name => "index_choice_versions_on_choice_id"
  58 +
33 59 create_table "choices", :force => true do |t|
34 60 t.integer "item_id"
35 61 t.integer "question_id"
... ... @@ -50,6 +76,7 @@ ActiveRecord::Schema.define(:version =&gt; 20111017171903) do
50 76 t.integer "prompts_count", :default => 0
51 77 t.string "data"
52 78 t.integer "creator_id"
  79 + t.integer "version"
53 80 end
54 81  
55 82 add_index "choices", ["creator_id"], :name => "index_choices_on_creator_id"
... ...