class ActiveRecord::Tasks::PostgreSQLDatabaseTasks

Public Instance Methods

data_dump(filename) click to toggle source
# File lib/pg_tasks.rb, line 73
def data_dump(filename)
  set_psql_env
  command = 'pg_dump -F c -a -T schema_migrations -x -O -f ' \
    "#{Shellwords.escape(filename.to_s)} " \
    "#{Shellwords.escape(configuration['database'])}"
  unless Kernel.system(command)
    raise 'Error during data_dump'
  else
    $stdout.puts "The data of '#{configuration['database']} " \
      "has been dumped to '#{filename}'"
  end
end
data_restore(filename) click to toggle source
# File lib/pg_tasks.rb, line 86
def data_restore(filename)
  set_psql_env
  command = 'pg_restore --disable-triggers --exit-on-error ' \
    '--single-transaction -a -x -O ' \
    "-d #{Shellwords.escape(configuration['database'])} " \
    "#{Shellwords.escape(filename.to_s)}"
  unless Kernel.system(command)
    raise 'Error during data_restore '
  else
    $stdout.puts "Data from '#{filename}' has been restored to \
                  '#{configuration['database']}'"
  end
end
structure_and_data_dump(filename) click to toggle source
# File lib/pg_tasks.rb, line 100
def structure_and_data_dump(filename)
  set_psql_env
  command = "pg_dump -F c -x -O -f \
  #{Shellwords.escape(filename)} \
  #{Shellwords.escape(configuration['database'])}"
  unless Kernel.system(command)
    raise 'Error during structure_and_data_dump'
  else
    $stdout.puts 'Structure and data of ' \
      "'#{configuration['database']}' has been dumped to '#{filename}'"
  end
end
structure_and_data_restore(filename) click to toggle source
# File lib/pg_tasks.rb, line 113
def structure_and_data_restore(filename)
  set_psql_env
  command = 'pg_restore --disable-triggers --exit-on-error ' \
    '--single-transaction -x -O -d ' \
    "#{Shellwords.escape(configuration['database'])} " \
    "#{Shellwords.escape(filename.to_s)}"
  unless Kernel.system(command)
    raise 'Error during structure_and_data_restore '
  else
    $stdout.puts "Structure and data of '#{configuration['database']}' " \
      "has been restored to '#{filename}'"
  end
end
terminate_connections() click to toggle source
# File lib/pg_tasks.rb, line 127
def terminate_connections
  set_psql_env
  database = configuration['database']
  command = "psql -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) " \
    ' FROM pg_stat_activity ' \
    "WHERE pg_stat_activity.datname = '#{database}' " \
    " AND pid <> pg_backend_pid();\" '#{database}'"
  unless Kernel.system(command)
    raise 'Error during terminate_connections'
  else
    $stdout.puts "Connections to '#{database}' have been terminated."
  end
end