Last active
June 26, 2020 16:55
Rake task to reset and seed Rails database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Originally written by Justin French (2008): | |
# http://justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-database | |
# | |
# Modified to work with Rails 4. | |
desc 'Raise an error unless development environment' | |
task :safety_check do | |
raise "You can only use this in dev!" unless Rails.env.development? | |
end | |
namespace :db do | |
desc 'Drop, create, migrate then seed the development database' | |
task reseed: [ | |
'environment', | |
'safety_check', | |
'db:reset', | |
'db:seed' | |
] | |
end |
Just what I was looking for.
And yes I agree that you could drop the drop, create, and migrate
calls in favor of reset
.
Nice work.
➕ 1
@emarthinsen Thanks for the suggestion. I've updated the script.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
I think you should just do
'db:reset'
Also, it's a best practice not to use
db:migrate
when loading up the schema for a new DB. Instead, just rundb:schema:load
. It's a moot point if you are using `db:reset' though.