Skip to content

Instantly share code, notes, and snippets.

@nithinbekal
Last active June 26, 2020 16:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nithinbekal/3423153 to your computer and use it in GitHub Desktop.
Save nithinbekal/3423153 to your computer and use it in GitHub Desktop.
Rake task to reset and seed Rails database
# 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
@emarthinsen
Copy link

Instead of

'db:drop',
'db:create',
'db:migrate'

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 run db:schema:load. It's a moot point if you are using `db:reset' though.

@cgcardona
Copy link

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

@nithinbekal
Copy link
Author

@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