Sha256: 399d3203c3471844815fe8718abd9d38ded79ecf9088a6182e54bfe3106d4333

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

require "heroku/client/pgbackups"
class Fourchette::Pgbackups
  include Fourchette::Logger

  def initialize
    @heroku = Fourchette::Heroku.new
  end

  def copy from, to
    ensure_pgbackups_is_present(from)
    ensure_pgbackups_is_present(to)

    from_url, from_name = pg_details_for(from)
    to_url, to_name = pg_details_for(to)

    @client =  Heroku::Client::Pgbackups.new pgbackup_url(from)+'/api'
    @client.create_transfer(from_url, from_name, to_url, to_name)
  end

  private
  def ensure_pgbackups_is_present heroku_app_name
    unless @heroku.client.addon.list(heroku_app_name).select { |addon| addon['name'] == 'pgbackups' }.any?
      logger.info "Adding pgbackups to #{heroku_app_name}"
      @heroku.client.addon.create(heroku_app_name, { plan: 'pgbackups' })
    end
  end

  def pg_details_for app_name
    @heroku.config_vars(app_name).each do |key, value|
      return [value, key] if key.start_with?('HEROKU_POSTGRESQL_') && key.end_with?('_URL')
    end
  end

  def pgbackup_url app_name
    @heroku.config_vars(app_name).each do |k, v|
      return v if k == 'PGBACKUPS_URL'
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fourchette-0.1.2 lib/fourchette/pgbackups.rb
fourchette-0.1.1 lib/fourchette/pgbackups.rb
fourchette-0.1.0 lib/fourchette/pgbackups.rb
fourchette-0.0.8 lib/fourchette/pgbackups.rb
fourchette-0.0.7 lib/fourchette/pgbackups.rb
fourchette-0.0.6 lib/fourchette/pgbackups.rb
fourchette-0.0.5 lib/fourchette/pgbackups.rb
fourchette-0.0.4 lib/fourchette/pgbackups.rb
fourchette-0.0.3 lib/fourchette/pgbackups.rb
fourchette-0.0.2 lib/fourchette/pgbackups.rb
fourchette-0.0.1 lib/fourchette/pgbackups.rb