'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.
Solution 1:[1]
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.
Solution 2:[2]
- Run
rake db:reset && rake db:seed(Note: You must have db/schema.rb file updated)
OR- Run
rake db:migrate:reset && rake db:seed
Solution 3:[3]
First create the 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
Solution 4:[4]
As of rails 6 you can run rake db:prepare which creates the db, runs migrations and seeds
If you want to completely reset the db, run rake db:drop && rake db:prepare
https://guides.rubyonrails.org/v6.0/6_0_release_notes.html#active-record-notable-changes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zajn |
| Solution 2 | Manish |
| Solution 3 | G. I. Joe |
| Solution 4 | Armel Larcier |
