Last active
March 11, 2016 20:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Updated for the new PGBackups (https://blog.heroku.com/archives/2015/3/11/pgbackups-levels-up)