Skip to content

Instantly share code, notes, and snippets.

@matthewtodd
Created August 6, 2010 12:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewtodd/511224 to your computer and use it in GitHub Desktop.
Save matthewtodd/511224 to your computer and use it in GitHub Desktop.
heroku rake db:pull --remote staging
require 'heroku'
require 'taps/operation'
class Database
def self.pull(*args)
new.pull(*args)
end
def initialize(uri=ENV['DATABASE_URL'])
@uri = uri
end
def pull(*args)
options = args.extract_options!
application = args.shift
username = args.shift || ENV['DB_PULL_USERNAME']
password = args.shift || ENV['DB_PULL_PASSWORD']
if options[:unless] == application
raise 'Cannot pull into self.'
else
Session.new(application, username, password).pull(@uri)
end
end
private
class Session
def initialize(application, username, password)
@session = Heroku::Client.new(username, password).database_session(application)
end
def pull(local_uri)
STDOUT.sync = true
STDERR.sync = true
Taps::Pull.new(local_uri, remote_uri, options).run
end
private
def options
{ :default_chunksize => 1000, :session_uri => session_uri }
end
def remote_uri
@session['url']
end
def session_uri
@session['session']
end
end
end
namespace :db do
desc "Copy production data into the current app's database."
task :pull => :environment do
Database.pull 'prancing-igloo-42', :unless => ENV['APP_NAME']
end
end
@matthewtodd
Copy link
Author

Part a Rails app I'm working on. database.rb lives in lib, databases.rake in lib/tasks, of course, and I've included heroku and taps in my Gemfile.

To configure, set ENV['DB_PULL_USERNAME'] and ENV['DB_PULL_PASSWORD'] in your staging app to the credentials of a user with access to your production app, and replace prancing-igloo-42 with the name of your production app.

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