Last active
August 29, 2015 14:06
-
-
Save sao/4cc7148ceae952b2c6ce to your computer and use it in GitHub Desktop.
Adds `rake db:import` to automatically download the newest backup from pg:backups and import the data into your current local database.
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
# | |
# This requires the Heroku toolbelt | |
# | |
# Add 'gem install httparty' to your Gemfile | |
# and make sure you have a default Heroku remote setup | |
# in your git config (make sure you can run 'heroku ps' | |
# without specifying an app name. | |
# | |
require 'httparty' | |
namespace :db do | |
desc "Create new db and import most recent data from Heroku" | |
task :import => [:create] do | |
connection = Rails.configuration.database_configuration[Rails.env] | |
migrate_db = ->{ `rake db:migrate` } | |
url = ->{ `heroku pgbackups:url` } | |
File.open('/tmp/latest.dump', 'wb') do |f| | |
f.write HTTParty.get(url.call).parsed_response | |
end | |
import = -> { `pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{connection['username']} -d #{connection['database']} -p 5432 '/tmp/latest.dump'` } | |
puts "Start import.." | |
import.call | |
puts "Import finished." | |
puts "Migrating database for good measure.." | |
migrate_db.call | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thrown together, feel free to refactor. Wrote it because I was tired of using a dynamic database.yml and having to manually create a new database and import live data.