Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Rails' db:reset task

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?

like image 939
readonly Avatar asked Jan 10 '10 03:01

readonly


People also ask

What does Rails DB Reset do?

db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous n migrations, pass STEP=n to this task.

What is environment rake task?

Including => :environment will tell Rake to load full the application environment, giving the relevant task access to things like classes, helpers, etc. Without the :environment , you won't have access to any of those extras.

What does DB seed do?

Database seeding is populating a database with an initial set of data. It's common to load seed data such as initial user accounts or dummy data upon initial setup of an application.

What are Rake tasks in Rails?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


2 Answers

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.

like image 108
Veger Avatar answered Sep 30 '22 14:09

Veger


Put this in lib/tasks/db.rake:

if Rails.env == 'production'
  tasks = Rake.application.instance_variable_get '@tasks'
  tasks.delete 'db:reset'
  tasks.delete 'db:drop'
  namespace :db do
    desc 'db:reset not available in this environment'
    task :reset do
      puts 'db:reset has been disabled'
    end
    desc 'db:drop not available in this environment'
    task :drop do
      puts 'db:drop has been disabled'
    end
  end
end

Found here.

like image 24
Ri4a Avatar answered Sep 30 '22 13:09

Ri4a