class Postgressor::CLI

Public Instance Methods

__print_version() click to toggle source
# File lib/postgressor/cli.rb, line 98
def __print_version
  puts VERSION
end
createdb() click to toggle source
# File lib/postgressor/cli.rb, line 40
def createdb
  preload!

  command = ["createdb", @conf[:db]] + @pg_cli_args
  if execute command, with_env: true
    say "Created database #{@conf[:db]}", :green
  end
end
createuser() click to toggle source
# File lib/postgressor/cli.rb, line 12
def createuser
  preload!

  # Use psql `CREATE USER` instead of `createuser` CLI command, to automatically provide user password:
  is_superuser = "SUPERUSER" if options[:superuser]
  psql_command = "CREATE USER #{@conf[:user]} WITH CREATEDB LOGIN #{is_superuser} PASSWORD '#{@conf[:password]}';"

  command = %W(sudo -i -u postgres psql -c #{psql_command})
  if execute command
    say "Created user #{@conf[:user]}", :green
  end
end
dropdb() click to toggle source
# File lib/postgressor/cli.rb, line 51
def dropdb
  preload!

  command = ["dropdb", @conf[:db]] + @pg_cli_args
  if execute command, with_env: true
    say "Dropped database #{@conf[:db]}", :green
  end
end
dropuser() click to toggle source
# File lib/postgressor/cli.rb, line 27
def dropuser
  preload!

  command = %W(sudo -i -u postgres dropuser #{@conf[:user]})
  if execute command
    say "Dropped user #{@conf[:user]}", :green
  end
end
dumpdb(dump_file_path = nil) click to toggle source
# File lib/postgressor/cli.rb, line 64
def dumpdb(dump_file_path = nil)
  preload!

  unless dump_file_path
    dump_file_path = "#{@conf[:db]}.dump"
  end

  command = %W(pg_dump #{@conf[:db]} -Fc --no-acl --no-owner -f #{dump_file_path}) + @pg_cli_args

  if execute command, with_env: true
    say "Dumped database #{@conf[:db]} to #{dump_file_path} file", :green
  end
end
print_db_url() click to toggle source
restoredb(dump_file_path) click to toggle source
# File lib/postgressor/cli.rb, line 81
def restoredb(dump_file_path)
  preload!

  set_user_to_superuser if options[:switch_to_superuser]

  command = %W(pg_restore #{dump_file_path} -d #{@conf[:db]} --no-acl --no-owner --verbose) + @pg_cli_args
  if execute command, with_env: true
    say "Restored database #{@conf[:db]} from #{dump_file_path} file", :green
  end

  set_user_to_nosuperuser if options[:switch_to_superuser]
end

Private Instance Methods

env() click to toggle source
# File lib/postgressor/cli.rb, line 139
def env
  { "PGPASSWORD" => @conf[:password].to_s }
end
execute(command, with_env: false) click to toggle source
# File lib/postgressor/cli.rb, line 127
def execute(command, with_env: false)
  if with_env
    verbose_command = env.map { |k, v| "#{k}=#{v}" } + command
    say("Executing: #{verbose_command.join(' ')}", :yellow) if ENV["VERBOSE"] == "true"

    system env, *command
  else
    say("Executing: #{command.join(' ')}", :yellow) if ENV["VERBOSE"] == "true"
    system *command
  end
end
preload!() click to toggle source
# File lib/postgressor/cli.rb, line 143
def preload!
  url = ENV["DATABASE_URL"]

  if url.nil? || url.strip.empty?
    # If DATABASE_URL env not present, try to read from config/database.yml (Rails)
    if File.exist?("config/database.yml")
      settings = YAML.load(ERB.new(File.read "config/database.yml").result)

      # By default, use production config, if RAILS_ENV not provided
      config = ENV["RAILS_ENV"] ? settings[ENV["RAILS_ENV"]] : settings["production"]

      preload_from_database_yml(config)
    else
      raise "Env variable DATABASE_URL or config/database.yml file not provided"
    end
  else
    preload_from_database_url(url)
  end

  @pg_cli_args = ["-h", @conf[:host], "-U", @conf[:user]]
  @pg_cli_args += ["-p", @conf[:port].to_s] if @conf[:port]
end
preload_from_database_url(url) click to toggle source
# File lib/postgressor/cli.rb, line 166
def preload_from_database_url(url)
  uri = URI.parse(url)
  raise "DB adapter is not postgres" unless uri.scheme.include?("postgres")

  @conf = {
    url: url,
    db: uri.path.sub("/", ""),
    host: uri.host,
    port: uri.port,
    user: uri.user,
    password: uri.password
  }
end
preload_from_database_yml(config) click to toggle source
# File lib/postgressor/cli.rb, line 180
def preload_from_database_yml(config)
  raise "DB adapter is not postgres" unless config["adapter"].include?("postgres")

  @conf = {
    db: config["database"],
    host: config["host"],
    port: config["port"],
    user: config["username"],
    password: config["password"]
  }

  @conf[:url] = "postgres://#{@conf[:user]}:#{@conf[:password]}@#{@conf[:host]}/#{@conf[:db]}"
end
set_user_to_nosuperuser() click to toggle source
# File lib/postgressor/cli.rb, line 119
def set_user_to_nosuperuser
  psql_command = "ALTER ROLE #{@conf[:user]} NOSUPERUSER;"
  command = %W(sudo -i -u postgres psql -c #{psql_command})
  if execute command
    say "Set user #{@conf[:user]} to NOSUPERUSER", :green
  end
end
set_user_to_superuser() click to toggle source
# File lib/postgressor/cli.rb, line 111
def set_user_to_superuser
  psql_command = "ALTER ROLE #{@conf[:user]} SUPERUSER;"
  command = %W(sudo -i -u postgres psql -c #{psql_command})
  if execute command
    say "Set user #{@conf[:user]} to SUPERUSER", :green
  end
end