Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster way to write this rake command - rake db:drop db:create db:migrate db:seed

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

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

Is there a prebuilt equivalent way to do this?

I thought from what I've read that rake db:reset doesn't quite do the same thing, but i could be wrong.

like image 392
Catfish Avatar asked Feb 02 '13 16:02

Catfish


People also ask

What does rake db seed do?

When you run rake db:seed it will load all the admin data into your application. Rails seeding is generally for development and/or staging environments, there are only a few uses in production. You don't want your production application to seed dummy users!

What does rake db setup?

rake db:schema:load will set up the schema as provided in schema.rb file. This is useful for a fresh install of app as it doesn't take as much time as db:migrate. Important note, db:schema:load will delete data on server. rake db:migrate makes changes to the existing schema. Its like creating versions of schema.

What is the difference between rails db Migrate and rake db migrate?

What happens internally is that when rails db:migrate command is executed, Rails checks if db:migrate is something that rails natively supports or not. In this case db:migrate is not natively supported by rails, so Rails delegates the execution to Rake via Rake Proxy.


2 Answers

you could create a custom rake task for this - 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

like image 193
house9 Avatar answered Sep 24 '22 02:09

house9


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

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

But why are you dropping and recreating your database everytime you have new migrations? That's what the migrations are there for, to make incremental changes to your existing database.

like image 27
Zajn Avatar answered Sep 22 '22 02:09

Zajn