Disabling a Rails job: reset

To avoid the accidental "rake db: reset" in our production environments, I was thinking of 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 need to override the rake task?

Is there a better alternative?

+4
source share
6 answers

In the Rake file you can add

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

and the team is no longer available. If you want to disable several commands, put them in the remove_task method for reading.

But the best alternative does not seem to just enter the rake db:reset command, which is not something you accidentally typed.

Having a good backup of your (production) database is also the best solution, I suppose.

+7
source

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 .

+2
source

Add this to your Rakefile.

 namespace :db do task :drop => :abort_on_production end task :abort_on_production do abort "Don't drop production database. aborted. " if Rails.env.production? end 

It also blocks rake db:reset and rake db:migrate:reset , because they call db:drop

+1
source

You can 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 
0
source

For the record We have an application related to our production base, we use the data to enter the system and from there we exit the test database.

The last thing we wanted was to clear the rails or the database schema when running the rspec / unit tests.

use the information, here is this question here: Is it possible to get a list of all available rake tasks in the namespace?

I was able to come up with the following solution:

 Rake.application.in_namespace(:db){|x| x.tasks.map{|t| Rake.application.instance_variable_get('@tasks').delete(t.name) } } 

This is placed at the end of our Rakefile, which allowed us to remove all db: rake tasks, as can be seen here:

 [ davidc@david-macbookp app1{master}]$ rake -T db [ davidc@david-macbookp app1{master}]$ 

With a little tweaking, this can be done to shutdown based on the environment.

Update:

A warning word doing this for the db namespace broke testunit and addison needed to be added:

 namespace :db do task 'test:prepare' do end end 
0
source

You can also use rails_db_protect gem .

You simply add a gem and it automatically prevents the launch of the following dangerous tasks:

 db:setup db:reset db:drop db:create db:schema:load 

If you have a production environment, such as an intermediate environment where you want to perform these tasks, you can configure the environment to allow it:

 ENV['ALLOW_DANGEROUS_TASKS'] = 'true' 

There is also a rails-safe-tasks gem that allows more configuration, but doesn't seem to support any tests.

0
source

All Articles