Sha256: 73e6b52cf12c48e74374a0dad5df0e63ad6a00770a059f9627366cf63efc2cfa

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

namespace :db do

  desc 'Drop connections to the pg database'
  task :drop_pg_connections => :environment do
    ActiveRecord::Base.connection.execute %{
      SELECT
        pg_terminate_backend(pid)
      FROM
        pg_stat_activity
      WHERE -- don't kill my own connection!
        datname = '#{WCC::RakeHelpers.db_config['database']}' AND
        pid <> pg_backend_pid();
    }
  end
  Rake::Task['db:drop'].enhance ['db:drop_pg_connections']

  desc 'Dump the pg database to the specified file'
  task :dump, :file do |t, args|
    raise "file argument required" unless args[:file]
    db_config = WCC::RakeHelpers.db_config
    command = [
      "pg_dump",
      "--username=#{db_config['username']}",
      "--host=#{db_config['host']}",
      "#{db_config['database']}",
      "> #{args[:file]}"
    ]
    WCC::RakeHelpers.db_cmd_with_password(command, db_config['password'])
  end

  desc 'Restore the pg datbase from the specified file'
  task :restore, :file do |t, args|
    raise "file argument required" unless File.exists?(args[:file])
    command = [
      "cat #{args[:file]} | psql",
      WCC::RakeHelpers.db_config['database'],
    ].join(" ")
    `#{command}`
  end

end if WCC::RakeHelpers.postgresql?

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wcc-base-0.1.0 lib/tasks/db/psql.rake