Created
August 6, 2010 12:05
-
-
Save matthewtodd/511224 to your computer and use it in GitHub Desktop.
heroku rake db:pull --remote staging
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
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 |
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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Part a Rails app I'm working on.
database.rb
lives inlib
,databases.rake
inlib/tasks
, of course, and I've includedheroku
andtaps
in myGemfile
.To configure, set
ENV['DB_PULL_USERNAME']
andENV['DB_PULL_PASSWORD']
in your staging app to the credentials of a user with access to your production app, and replaceprancing-igloo-42
with the name of your production app.