1. db:fresh - Easily rebuild your DB from scratch

    I suppose the idea behind Rails’ migration is that once you create them, you don’t change them.  This is usually the right concept, and from this decision grows many of the rake db:* tasks.  Such tasks — migrate, reset, schema:load, schema:dump, and setup — do not re-run migrations as they assume schema.rb is up to date (rightfully so).  But oftentimes (especially on greenfield development) I will tweak my migrations and want to start from scratch.  So here’s a quick hitter I now have my application templates add to every project I start:

    module DBTasks
      namespace :db do
        
        desc "drops, creates, migrates and seeds the DB"
        task :fresh => :environment do
          puts "Dropping DB..."
          Rake::Task["db:drop"].invoke; puts "done."
          puts "Creating DB..."
          Rake::Task["db:create"].invoke; puts "done."
          puts "Migrating DB..."
          Rake::Task["db:migrate"].invoke; puts "done."
          puts "Seeding DB..."
          Rake::Task["db:seed"].invoke; puts "done."
          puts "Your DB is so fresh and so clean clean!"
        end
        
      end
    end
    

     

    tags:  rails  ruby  db 

    Comments
  2. blog comments powered by Disqus