Skip to content

Instantly share code, notes, and snippets.

@itskingori
Created December 17, 2014 06:54
Show Gist options
  • Save itskingori/a210867de22d83098aef to your computer and use it in GitHub Desktop.
Save itskingori/a210867de22d83098aef to your computer and use it in GitHub Desktop.
Disable dangerous rake tasks in production. It's a matter of adding a prerequisite to those dangerous tasks, that checks if they are being run in production, and exit accordingly. Also added is a flag to override this safeguard, together with some code to backup the DB.
# Could be in lib/tasks/disable_db_tasks_on_production.rake
# See original http://www.developingandstuff.com/2014/06/disable-dangerous-rake-tasks-in.html
DISABLED_TASKS = [
'db:drop',
'db:migrate:reset',
'db:schema:load',
'db:seed',
# ...
]
namespace :db do
desc "Disable a task in production environment"
task :guard_for_production do
if Rails.env.production?
if ENV['I_KNOW_THIS_MAY_SCREW_THE_DB'] != "1"
puts 'This task is disabled in production.'
puts 'If you really want to run it, call it again with `I_KNOW_THIS_MAY_SCREW_THE_DB=1`'
exit
else
require 'heroku'
puts 'Making a backup of the database, just in case...'
puts `heroku pgbackups:capture`
end
end
end
end
DISABLED_TASKS.each do |task|
Rake::Task[task].enhance ['db:guard_for_production']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment