Custom Rails Task to Drop, Create, Migrate and Seed a Database

If you have been using Ruby on Rails to create your websites or web applications, i’m pretty sure that you are looking for a fast and easy way to drop your database and recreate it, as well as feed it with your fixtures data. This procedure is actually 4 different tasks, but using a simple trick, you can make it very easy for you.
With rails, you can create your own custom task that will enable you to do that very quickly. The procedure is simple. In your Rails application folder, go to /lib/tasks and create a new rake file. I prefer to name it “reset_db.rake“. Open it and inside write the lines :
namespace :db do desc "Drop, create, migrate then seed the database" task :reset_db => :environment do Rake::Task['db:drop'].invoke Rake::Task['db:create'].invoke Rake::Task['db:migrate'].invoke Rake::Task['db:fixtures:load'].invoke end end
Now, whenever you want to recreate and totally reset your database, you just have to execute the command “rake db:reset_db”. This will automatically drop the database, recreate it, run the migrations and ultimately load the fixtures located at /test/fixtures. Hope that saves you as much time as it saves for me
Or we can just use this command: “rake db:migrate:reset”
@Neel Pathak : Yes, that’s possibly the better way also. It won’t load your fixtures though. I find the name db:reset_db way more descriptive and just use it in a simple task.
You actually make it apoear really easy together with yur presentation but
I find this matter to be really oone thing which I think I might by no means understand.
It kind off feels too complicated and extremely huge for me.
I’mhaving a look ahead in your next publish, I’ll try to get the cling of it!