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.
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!
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 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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With