<commit_before>require 'heroku/command/db'

class Heroku::Command::Db

  # db:parse_db_url [DATABASE_URL] [--format {psql|pgpass|rails_yaml|pg_dump|pg_restore|alias} [-aliasname NAME]
  #
  # generates a string to the format specified, psql by default, DATABASE_URL by default
  #
  # -f, --format FORMAT   # set the output format (psql, pgpass, all)
  #                       # accepts comma seperated list
  # --aliasname NAME      # name of alias used for the alias format option
  #
  #Examples:
  #
  # $ heroku db:parse_db_url HEROKU_POSTGRESQL_NAVY --format=pgpass
  #
  # $ heroku db:parse_db_url HEROKU_POSTGRESQL_NAVY # generates psql string if no --format provided
  #
  # $ heroku db:parse_db_url # generates psql string for DATABASE_URL
  #
  # $ heroku db:parse_db_url --format alias --aliasname pgtest  # generates an alias declaration for a bash_profile
  #

  def parse_db_url
    db = args.detect { |a| a.include?('HEROKU_POSTGRESQL_') } || 'DATABASE_URL'

    format = options[:format] || 'psql'
    formats = format.split(/\s*,\s*/)
    db_info = {}

    heroku.config_vars(app).select do |k, v|
      if k.include?(db)
        db_info[:name] = k
        db_info[:conn_string] = v
      end
    end

    uri = URI.parse(db_info[:conn_string])
    uri_parts = {
      :host   => uri.host,
      :db     => cleanse_path(uri.path),
      :user   => uri.user,
      :pw     => uri.password,
      :scheme => uri.scheme,
      :port   => uri.port
    }

    return "#{uri_parts[:scheme]} not supported yet" if uri_parts[:scheme] != 'postgres'

    formats.each do |f|
      display case f
      when "psql", nil
        psqlify(uri_parts)
      when "pgpass"
        pgpassify(uri_parts)
      when "rails_yaml"
        rails_yamlify(uri_parts)
      when "pg_dump"
        pgdumpify(uri_parts)
      when "pg_restore"
        pgrestorify(uri_parts)
      when "alias"
        aname = options[:aliasname] || 'aliasname'
        "alias #{aname}='#{psqlify(uri_parts)}'"
      else
        "#{f} not known or supported. Please use one of |psql,pgpass,rails_yaml,pg_dump,pg_restore,alias|'"
      end
    end
  end

  protected

  def psqlify(uri_hash)
    "psql -h #{uri_hash[:host]} -d #{uri_hash[:db]} -U #{uri_hash[:user]} -p #{uri_hash[:port]}"
  end

  def pgpassify(uri_hash)
    "#{uri_hash[:host]}:#{uri_hash[:port]}:#{uri_hash[:db]}:#{uri_hash[:user]}:#{uri_hash[:pw]}"
  end

  def rails_yamlify(uri_hash)
    "host: #{uri_hash[:host]}\ndatabase: #{uri_hash[:db]}\nusername: #{uri_hash[:user]}\npassword: #{uri_hash[:pw]}\nport: #{uri_hash[:port]}"
  end

  def pgdumpify(uri_hash)
    "pg_dump #{uri_hash[:db]} -h #{uri_hash[:host]} -p #{uri_hash[:port]} -U #{uri_hash[:user]}"
  end

  def pgrestorify(uri_hash)
    "pg_restore -d #{uri_hash[:db]} -h #{uri_hash[:host]} -p #{uri_hash[:port]} -U #{uri_hash[:user]}"
  end

  def cleanse_path(path)
    path.sub(/^\//,'')
  end

  def append_to_file(file, text)
    raise "unable to write to #{file}" unless File.writable?(file)
    display "Appending #{text} to #{file}"
    open(file, 'a') do |f|
      f.puts text
    end
  end

end<commit_msg>remove append_to_file method no longer used<commit_after>require 'heroku/command/db'

class Heroku::Command::Db

  # db:parse_db_url [DATABASE_URL] [--format {psql|pgpass|rails_yaml|pg_dump|pg_restore|alias} [-aliasname NAME]
  #
  # generates a string to the format specified, psql by default, DATABASE_URL by default
  #
  # -f, --format FORMAT   # set the output format (psql, pgpass, all)
  #                       # accepts comma seperated list
  # --aliasname NAME      # name of alias used for the alias format option
  #
  #Examples:
  #
  # $ heroku db:parse_db_url HEROKU_POSTGRESQL_NAVY --format=pgpass
  #
  # $ heroku db:parse_db_url HEROKU_POSTGRESQL_NAVY # generates psql string if no --format provided
  #
  # $ heroku db:parse_db_url # generates psql string for DATABASE_URL
  #
  # $ heroku db:parse_db_url --format alias --aliasname pgtest  # generates an alias declaration for a bash_profile
  #

  def parse_db_url
    db = args.detect { |a| a.include?('HEROKU_POSTGRESQL_') } || 'DATABASE_URL'

    format = options[:format] || 'psql'
    formats = format.split(/\s*,\s*/)
    db_info = {}

    heroku.config_vars(app).select do |k, v|
      if k.include?(db)
        db_info[:name] = k
        db_info[:conn_string] = v
      end
    end

    uri = URI.parse(db_info[:conn_string])
    uri_parts = {
      :host   => uri.host,
      :db     => cleanse_path(uri.path),
      :user   => uri.user,
      :pw     => uri.password,
      :scheme => uri.scheme,
      :port   => uri.port
    }

    return "#{uri_parts[:scheme]} not supported yet" if uri_parts[:scheme] != 'postgres'

    formats.each do |f|
      display case f
      when "psql", nil
        psqlify(uri_parts)
      when "pgpass"
        pgpassify(uri_parts)
      when "rails_yaml"
        rails_yamlify(uri_parts)
      when "pg_dump"
        pgdumpify(uri_parts)
      when "pg_restore"
        pgrestorify(uri_parts)
      when "alias"
        aname = options[:aliasname] || 'aliasname'
        "alias #{aname}='#{psqlify(uri_parts)}'"
      else
        "#{f} not known or supported. Please use one of |psql,pgpass,rails_yaml,pg_dump,pg_restore,alias|'"
      end
    end
  end

  protected

  def psqlify(uri_hash)
    "psql -h #{uri_hash[:host]} -d #{uri_hash[:db]} -U #{uri_hash[:user]} -p #{uri_hash[:port]}"
  end

  def pgpassify(uri_hash)
    "#{uri_hash[:host]}:#{uri_hash[:port]}:#{uri_hash[:db]}:#{uri_hash[:user]}:#{uri_hash[:pw]}"
  end

  def rails_yamlify(uri_hash)
    "host: #{uri_hash[:host]}\ndatabase: #{uri_hash[:db]}\nusername: #{uri_hash[:user]}\npassword: #{uri_hash[:pw]}\nport: #{uri_hash[:port]}"
  end

  def pgdumpify(uri_hash)
    "pg_dump #{uri_hash[:db]} -h #{uri_hash[:host]} -p #{uri_hash[:port]} -U #{uri_hash[:user]}"
  end

  def pgrestorify(uri_hash)
    "pg_restore -d #{uri_hash[:db]} -h #{uri_hash[:host]} -p #{uri_hash[:port]} -U #{uri_hash[:user]}"
  end

  def cleanse_path(path)
    path.sub(/^\//,'')
  end

end
{'fraction_non_alphanumeric': 0.15098071534531068, 'fraction_numerical': 0.0, 'mean_word_length': 3.0752182672934856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1851175', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2234, 'n_words': 484}