Last active
October 18, 2017 01:02
-
-
Save brettchalupa/d1ed9507ce30fd963cb05258e6220509 to your computer and use it in GitHub Desktop.
A Simple Rake Task for Importing Heroku Postgres Databases Locally for Rails Apps
This file contains 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
# lib/tasks/db_import.rake | |
namespace :db do | |
namespace :import do | |
desc 'Import Heroku Staging database locally' | |
task :staging do | |
import_db('staging', 'YOUR_STAGING_HEROKU_APP_HERE') | |
end | |
desc 'Import Heroku Production database locally' | |
task :production do | |
import_db('production', 'YOUR_PRODUCTION_HEROKU_APP_HERE') | |
end | |
# Creates a unique database dump based on the current time and specified environment. | |
def import_db(environment, heroku_app_name) | |
puts("Importing #{environment} database locally...") | |
file = "tmp/#{environment}-#{date}.dump" | |
dump_local | |
capture_and_download_heroku_db(heroku_app_name) | |
`mv latest.dump #{file}` | |
import_locally(file) | |
run_migrations | |
puts("#{environment} database successfully imported") | |
end | |
# Returns the readable date in YYYY-MM-DD with an | |
# appended integer time to make the filename unique. | |
def date | |
"#{Time.now.to_date.to_s}-#{Time.now.to_i}" | |
end | |
# Returns the current machine's user for use with PG commands | |
def user | |
`whoami`.strip | |
end | |
# Creates and downloads a Heroku database back-up. | |
# Requires the Heroku toolchain to be installed and setup. | |
def capture_and_download_heroku_db(app) | |
`heroku pg:backups:capture --app #{app}` | |
`heroku pg:backups:download --app #{app}` | |
end | |
# Creates a back-up of your current local development | |
# database in case you want to restore it. | |
def dump_local | |
`pg_dump -Fc --no-acl --no-owner -h localhost -U #{user} YOUR_LOCAL_DB_NAME_HERE > tmp/development-#{date}.dump` | |
end | |
# Imports the downloaded database dump into your local development database. | |
def import_locally(file) | |
`pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{user} -d YOUR_LOCAL_DB_NAME_HERE #{file}` | |
end | |
# Runs migrations against the just imported database dump from Heroku. | |
def run_migrations | |
`bin/rake db:migrate` | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist adds commands for importing databases from Heroku into your local Rails development environment using
bin/rake db:import:staging
orbin/rake db:import:production
. Here's the blog post with more details.