class Capistrano::DBSync::Postgres::CLI

Attributes

config[R]
session_id[R]

Public Class Methods

new(config) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 2
def initialize(config)
  @config = config
end

Public Instance Methods

copy_and_compress_to_file(to_compressed_file, db, query) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 42
def copy_and_compress_to_file(to_compressed_file, db, query)
  psql "\\copy (#{query}) TO PROGRAM 'gzip > #{to_compressed_file}' WITH CSV", db
end
copy_from_compressed_file(from_compressed_file, db, table) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 46
def copy_from_compressed_file(from_compressed_file, db, table)
  psql "\\copy #{table} FROM PROGRAM 'gunzip --to-stdout #{from_compressed_file}' WITH CSV", db
end
create_db(db) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 20
def create_db(db)
  psql %Q|CREATE DATABASE "#{db}";|
end
drop_db(db) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 16
def drop_db(db)
  psql %Q|DROP DATABASE IF EXISTS "#{db}";|
end
dump(to_file, db, options = []) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 6
def dump(to_file, db, options = [])
  args = to_string_args(options)
  "#{with_pw} pg_dump #{credentials} #{format_args} -f #{to_file} #{args} #{db}".strip
end
kill_processes_for_db(db) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 33
  def kill_processes_for_db(db)
    psql <<-SQL.gsub("$", "\\$")
      SELECT pg_terminate_backend(pg_stat_activity.pid)
        FROM pg_stat_activity
       WHERE pg_stat_activity.datname = $$#{db}$$
         AND pid <> pg_backend_pid();
    SQL
  end
psql(command, db = "postgres") click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 28
def psql(command, db = "postgres")
  normalized_command = command.gsub('"', '\"').gsub(/\s\s+|\n/, " ")
  %Q|#{with_pw} psql #{credentials} -d #{db} -c "#{normalized_command}"|.strip
end
rename_db(old_db, new_db) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 24
def rename_db(old_db, new_db)
  psql %Q|ALTER DATABASE "#{old_db}" RENAME TO "#{new_db}";|
end
restore(from_file, db, options = []) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 11
def restore(from_file, db, options = [])
  args = to_string_args(options)
  "#{with_pw} pg_restore #{credentials} #{format_args} -d #{db} #{args} #{from_file}".strip
end

Private Instance Methods

credentials() click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 56
def credentials
  credentials_params = []
  credentials_params << "-U #{config['username']}" if config.has_key?('username')
  credentials_params << "-h #{config['host']}"     if config.has_key?('host')
  credentials_params << "-p #{config['port']}"     if config.has_key?('port')
  credentials_params.join(" ")
end
format_args() click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 52
def format_args
  "--no-acl --no-owner --format=custom"
end
to_string_args(options) click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 70
def to_string_args(options)
  options.nil? ? "" : options.join(" ")
end
with_pw() click to toggle source
# File lib/capistrano/db_sync/postgres/cli.rb, line 64
def with_pw
  if config.has_key?('password')
    "PGPASSWORD='#{config['password']}'"
  end
end