Created
July 13, 2015 14:03
-
-
Save jwood/afec203b0d80b47d82a5 to your computer and use it in GitHub Desktop.
Example of rake task that migrates production data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :question do | |
desc "Update Questions and Tasks to support newly added validations" | |
task :update_for_validations => :environment do | |
ActiveRecord::Base.transaction do | |
# Set question_type to "yesno" for following questions: | |
%w(need_long_term_rental need_short_term_rental find_eye_doctor cleaning need_home).each do |identifier| | |
q = Question.find_by_identifier(identifier) | |
q.update_attributes!(question_type: "yesno") if q.present? | |
end | |
# Delete 6 tasks with name "Master Question - NO TASK" | |
master_question_tasks = Task.where(name: "Master Question - NO TASK") | |
master_question_tasks.each(&:destroy!) | |
end | |
puts "Verifying validity of remaining questions..." | |
all_questions_valid = true | |
Question.find_each do |q| | |
unless q.valid? | |
puts "Invalid question: [#{q.id}/#{q.identifier}] #{q.errors.full_messages}" | |
all_questions_valid = false | |
end | |
end | |
puts "All Questions are valid." if all_questions_valid | |
puts "Verifying validity of remaining tasks..." | |
all_tasks_valid = true | |
Task.find_each do |t| | |
unless t.valid? | |
puts "Invalid task: [#{t.id}/#{t.name}] #{t.errors.full_messages}" | |
all_tasks_valid = false | |
end | |
end | |
puts "All Tasks are valid" if all_tasks_valid | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment