A faster way to write this rake command is rake db: drop db: create db: migrate db: seed

Every time I have changes in my schema or new migration files, I run this command:

rake db:drop db:create db:migrate db:seed 

Is there a predefined equivalent way to do this?

I thought from what I read that rake db:reset does not do the same, but I could be wrong.

+6
source share
4 answers

for this you can create a custom rake task - lib / tasks / db_rebuild_all.rake

 namespace :db_tasks do desc "Rebuild database" task :rebuild, [] => :environment do raise "Not allowed to run on production" if Rails.env.production? Rake::Task['db:drop'].execute Rake::Task['db:create'].execute Rake::Task['db:migrate'].execute Rake::Task['db:seed'].execute end end 

then just run bundle exec rake db_tasks:rebuild

+6
source
  • Run rake db:reset && rake db:seed (Note: you must have the db / schema.rb file updated) <b>
    OR
  • Run rake db:migrate:reset && rake db:seed
+3
source

You can run rake db:drop and then rake db:setup .

db:setup will run rake db:create db:schema:load and db:seed

But why do you drop and recreate your database every time you have new migrations? Why do we need migrations to make additional changes to the existing database.

+2
source

First create a task file (lib / tasks / db.rake) with

 rails g task db reseed 

Then write in it:

 namespace :db do desc "Reseed database task" task reseed: [ 'db:drop', 'db:create', 'db:migrate', 'db:seed' ] do puts 'Reseeding completed.' end end 
0
source

All Articles