Disable Rake Commands in a Rails Project

May 15, 2015 Rake, Ruby on Rails

I had a project where I wanted to limit the use of db rake tasks to keep anyone from accidentally deleting the database.

To delete a rake task, simply add this method to your Rakefile:

Rakefile

Rake.application.instance_variable_get('@tasks').delete('task')

Here task is the name of your task.

If you want to do this multiple times, you can loop over your tasks like so:

Rakefile

def remove_task(task)
  Rake.application.instance_variable_get('@tasks').delete(task)
end

tasks = ['db:drop', 'db:reset', 'db:setup', 'db:rollback', 'db:seed']
tasks.each { |t| remove_task(t) }

Did you learn something or find this article interesting?

If so, why not