views:

39

answers:

2

To avoid an accidental 'rake db:reset' on our production environments, I was thinking about disabling 'rake db:reset' and related tasks that drop the database in the production environment. Is there an easy way to do this, or do I have to redefine the rake task?

Is there a better alternative?

+2  A: 

In your Rake file you can add

Rake.application.instance_variable_get('@tasks').delete('db:reset')

and the command is not available any more. If you want to disable multiple commands, put it in a remove_task method for readability.

But a better alternative seem to just not type the rake db:reset command, which is not something you'd accidentally type.

Having a nice backup of your (production) database is also a better solution I suppose.

Veger
Rake::Task['db:reset'].clear_actions seems easier to use
Veger
I second the backups, not to mention locking down your production environment so only a couple people have access.
Adam Stegman
A: 

You could always overwrite the db:reset task with something like this in lib/db.rake:

namespace :db do
  desc 'Resets your database using your migrations for the current environment'
  task :reset do
    if RAILS_ENV == 'production'
      Rake::Task["db:drop"].invoke
      Rake::Task["db:create"].invoke
      Rake::Task["db:migrate"].invoke
    end
  end
end
Andrew Nesbitt