Skip to content

Instantly share code, notes, and snippets.

@barrettclark
Last active March 11, 2016 20:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barrettclark/c94467e3872d16b3f8b0 to your computer and use it in GitHub Desktop.
Save barrettclark/c94467e3872d16b3f8b0 to your computer and use it in GitHub Desktop.
Rake task to load production data (from Heroku) to the local dev database
namespace :db do
namespace :heroku do
desc "capture DB Backup"
task :capture_backup => :environment do
if Rails.env == 'development'
Bundler.with_clean_env do
config = Rails.configuration.database_configuration[Rails.env]
system "heroku pg:backups capture"
end
end
end
desc "Pull DB Backup"
task :download_backup => :capture_backup do
if Rails.env == 'development'
Bundler.with_clean_env do
config = Rails.configuration.database_configuration[Rails.env]
system "curl -o latest.dump `heroku pg:backups public-url`"
end
end
end
desc "Load the PROD database from Heroku to the local dev database"
task :load => :download_backup do
if Rails.env == 'development'
Bundler.with_clean_env do
config = Rails.configuration.database_configuration[Rails.env]
system <<-CMD
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
@barrettclark
Copy link
Author

@barrettclark
Copy link
Author

In discussing this with a member of the Heroku Postgres staff, I have created a second version that uses pg:pull. The requirement there is that the target local database must not already exist. The workflow that I was trying to enable was pulling a prod copy for local dev, so there are a few additional steps around dropping and recreating some of the infrastructure.

I also learned that there are a lot of config settings that can effect how a queryplan executes, so benchmarking a query locally won't necessarily give the same plan as in prod.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment