Tag Archives: heroku

Rake Task To Load Production Data

When I’m working on a Rails app that relies on live data, like a reporting app for example, I find myself constantly wanting to pull live data to my local dev database. So I made a rake task to do this:

namespace :db do
  namespace :heroku do
    desc "Load the PROD database from Heroku to the local dev database"
    task :load => :environment do
      if Rails.env == 'development'
        Bundler.with_clean_env do
          config = Rails.configuration.database_configuration[Rails.env]
          system <<-CMD
            heroku pgbackups:capture
            curl -o latest.dump `heroku pgbackups:url`
            pg_restore --verbose --clean --no-acl --no-owner -h localhost \
              -U #{config["username"]} -d #{config["database"]} latest.dump
            rm -rf latest.dump
          CMD
        end
      end
    end
  end
end

3 Comments

Filed under programming, ruby