Commit f44412f01fb98dfaec2877a9fe64112fb28b829f

Authored by Luke Baker
1 parent 14a2322e

add initial start of conversion task

Showing 1 changed file with 69 additions and 1 deletions   Show diff stats
lib/tasks/prune_db.rake
1 1 namespace :prune_db do
2 2  
3   - task :all => [:invalidate_votes_with_bad_response_times]
  3 + desc "Converts all dates from PT to UTC"
  4 + task :convert_dates_to_utc => :environment do
  5 + time_spans = [
  6 + #{ :gt => "2009-11-01 02:00:00", :lt => "2010-03-14 02:00:00", :h => 8},
  7 + #{ :gt => "2010-03-14 02:00:00", :lt => "2010-11-07 01:00:00", :h => 7},
  8 + { :gt => "2010-11-07 01:00:00", :lt => "2010-11-07 02:00:00", :h => nil},
  9 + #{ :gt => "2010-11-07 02:00:00", :lt => "2011-03-13 02:00:00", :h => 8},
  10 + #{ :gt => "2011-03-13 02:00:00", :lt => "2011-11-06 01:00:00", :h => 7},
  11 + { :gt => "2011-11-06 01:00:00", :lt => "2011-11-06 02:00:00", :h => nil},
  12 + #{ :gt => "2011-11-06 02:00:00", :lt => "2012-03-11 02:00:00", :h => 8},
  13 + #{ :gt => "2012-03-11 02:00:00", :lt => "2012-11-04 01:00:00", :h => 7}
  14 + ]
  15 + # UTC because Rails will be thinking DB is in UTC when we run this
  16 + time_spans.map! do |t|
  17 + { :gt => Time.parse("#{t[:gt]} UTC"),
  18 + :lt => Time.parse("#{t[:lt]} UTC"),
  19 + :h => t[:h] }
  20 + end
  21 + datetime_fields = {
  22 + :appearances => ['created_at', 'updated_at'],
  23 + :choices => ['created_at', 'updated_at'],
  24 + :clicks => ['created_at', 'updated_at'],
  25 + :densities => ['created_at', 'updated_at'],
  26 + :flags => ['created_at', 'updated_at'],
  27 + :prompts => ['created_at', 'updated_at'],
  28 + :skips => ['created_at', 'updated_at'],
  29 + :votes => ['created_at', 'updated_at'],
  30 + :visitors => ['created_at', 'updated_at'],
  31 + :users => ['created_at', 'updated_at'],
  32 + :questions => ['created_at', 'updated_at'],
  33 + :question_versions => ['created_at', 'updated_at'],
  34 + :delayed_jobs => ['created_at', 'updated_at', 'run_at', 'locked_at', 'failed_at'],
  35 + }
  36 +
  37 + STDOUT.sync = true
  38 + datetime_fields.each do |table, columns|
  39 + print "#{table}"
  40 + batch_size = 10000
  41 + i = 0
  42 + while true do
  43 + rows = ActiveRecord::Base.connection.select_all(
  44 + "SELECT id, #{columns.join(", ")} FROM #{table} ORDER BY id LIMIT #{i*batch_size}, #{batch_size}"
  45 + )
  46 + print "."
  47 +
  48 + rows.each do |row|
  49 + row.each do |column, value|
  50 + if value.class == Time
  51 + time_spans.each do |span|
  52 + # TODO: update row if match and span has :h
  53 + # log if :h is nil these will be manually sorted out
  54 + # log if no time_span found
  55 + if value < span[:lt] && value > span[:gt]
  56 + if span[:h].blank?
  57 + puts "#{row.inspect}: #{span.inspect}"
  58 + end
  59 + break
  60 + end
  61 + end
  62 + end
  63 + end
  64 + end
  65 +
  66 + i+= 1
  67 + break if rows.length < 1000
  68 + end
  69 + print "\n"
  70 + end
  71 + end
4 72  
5 73 desc "Fixes a mis-match between a vote's prompt_id and its appearance's prompt_id. Sets the appearance prompt_id to match the vote's prompt_id"
6 74 task :fix_promptid_mismatch => :environment do
... ...