class Database::Base

Constants

DBCONFIG_BEGIN_FLAG
DBCONFIG_END_FLAG

Attributes

config[RW]
output_file[RW]

Public Class Methods

new(cap_instance) click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 8
def initialize(cap_instance)
  @cap = cap_instance
end

Public Instance Methods

compressor() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 51
def compressor
  @compressor ||= begin
    compressor_klass = @cap.fetch(:compressor).to_s.split('_').collect(&:capitalize).join
    klass = Object.module_eval("::Compressors::#{compressor_klass}", __FILE__, __LINE__)
    klass
  end
end
credentials() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 20
def credentials
  credential_params = ""
  username = @config['username'] || @config['user']

  if mysql?
    credential_params << " -u #{username} " if username
    credential_params << " -p'#{@config['password']}' " if @config['password']
    credential_params << " -h #{@config['host']} " if @config['host']
    credential_params << " -S #{@config['socket']} " if @config['socket']
    credential_params << " -P #{@config['port']} " if @config['port']
  elsif postgresql?
    credential_params << " -U #{username} " if username
    credential_params << " -h #{@config['host']} " if @config['host']
    credential_params << " -p #{@config['port']} " if @config['port']
  end

  credential_params
end
current_time() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 43
def current_time
  Time.now.strftime("%Y-%m-%d-%H%M%S")
end
database() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 39
def database
  @config['database']
end
mysql?() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 12
def mysql?
  @config['adapter'] =~ /^mysql/
end
postgresql?() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 16
def postgresql?
  %w(postgresql pg postgis).include? @config['adapter']
end

Private Instance Methods

dump_cmd() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 65
def dump_cmd
  if mysql?
    "mysqldump #{credentials} #{database} #{dump_cmd_opts}"
  elsif postgresql?
    "#{pgpass} pg_dump #{credentials} #{database} #{dump_cmd_opts}"
  end
end
dump_cmd_ignore_data_tables_opts() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 99
def dump_cmd_ignore_data_tables_opts
  ignore_tables = @cap.fetch(:db_ignore_data_tables, [])
  ignore_tables.map { |t| "--exclude-table-data=#{t}" }.join(" ") if postgresql?
end
dump_cmd_ignore_tables_opts() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 90
def dump_cmd_ignore_tables_opts
  ignore_tables = @cap.fetch(:db_ignore_tables, [])
  if mysql?
    ignore_tables.map { |t| "--ignore-table=#{database}.#{t}" }.join(" ")
  elsif postgresql?
    ignore_tables.map { |t| "--exclude-table=#{t}" }.join(" ")
  end
end
dump_cmd_opts() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 82
def dump_cmd_opts
  if mysql?
    "--lock-tables=false #{dump_cmd_ignore_tables_opts} #{dump_cmd_ignore_data_tables_opts}"
  elsif postgresql?
    "--no-acl --no-owner #{dump_cmd_ignore_tables_opts} #{dump_cmd_ignore_data_tables_opts}"
  end
end
import_cmd(file) click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 73
def import_cmd(file)
  if mysql?
    "mysql #{credentials} -D #{database} < #{file}"
  elsif postgresql?
    terminate_connection_sql = "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '#{database}' AND pid <> pg_backend_pid();"
    "#{pgpass} psql -c \"#{terminate_connection_sql};\" #{credentials} #{database}; #{pgpass} dropdb #{credentials} #{database}; #{pgpass} createdb #{credentials} #{database}; #{pgpass} psql #{credentials} -d #{database} < #{file}"
  end
end
pgpass() click to toggle source
# File lib/capistrano-db-tasks/database.rb, line 61
def pgpass
  @config['password'] ? "PGPASSWORD='#{@config['password']}'" : ""
end