Disable unsafe rake tasks in Rails production environment
In my previous post, I wrote about dropping and reseeding the DB of your Rails app. This is useful in development, but you might want to disable such tasks in production. Let’s look at how we can do that.
First, let’s define a rake task that simply raises an exception if you run it in production environment:
rake environment task
loads the Rails environment
and makes methods like
so we added that as a dependency
of our task.
Now we can change the
to run this before
dropping the database.
This ensures that the task
always fails in production.
But what about the other tasks
that are already defined by Rails
that could cause similar damage,
We could add the
to these tasks.
the very beginning of the dependencies.
So the exception is raised
before the database is dropped.
Even if you have made sure that your data won’t get deleted by an accidental rake command, do make sure that you’ve set up regular backups of the database. You never know what else could mess up your data.