Skip to content

Instantly share code, notes, and snippets.

@coffeesam
Forked from tcmacdonald/postgresql.rake
Created March 6, 2013 13:30
Show Gist options
  • Save coffeesam/5099303 to your computer and use it in GitHub Desktop.
Save coffeesam/5099303 to your computer and use it in GitHub Desktop.
require 'yaml'
namespace :pg do
namespace :mac do
def conf
@conf ||= YAML.load_file("#{Rails.root}/config/database.yml")
end
def username
@username ||= conf['development']['username']
end
def database
@database ||= conf['development']['database'][/[^_]*/]
end
desc "Create a Mac brew installed Postgres DB and user."
task :create do
%x[createuser -d -R -S -w #{username}]
Rake::Task['pg:mac:load'].invoke
end
task :load do
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
Rake::Task['db:seed'].invoke
Rake::Task['db:test:prepare'].invoke
end
desc "Drop user and DB."
task :drop do
Rake::Task['pg:mac:kill_sessions'].invoke
Rake::Task['db:drop'].invoke
%x[dropuser #{username}]
end
desc "Kill all existing PG sessions."
task :kill_sessions do
%x[ps -ef | grep "postgres: #{username} #{database}_development" | grep -v 'grep' | awk '{print $2}' | xargs kill]
end
desc "Drop, create, migrate, seed"
task :reload do
Rake::Task['pg:mac:kill_sessions'].invoke
Rake::Task['db:drop'].invoke
Rake::Task['pg:mac:load'].invoke
end
desc "Starts a Mac brew installed Postgres DB."
task :start do
result = %x[pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start]
puts result
end
desc "Stops a Mac brew installed Postgres DB."
task :stop do
result = %x[pg_ctl -D /usr/local/var/postgres stop -s -m fast]
puts result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment