Created
October 22, 2012 15:27
-
-
Save tcmacdonald/3932066 to your computer and use it in GitHub Desktop.
Rake tasks for Postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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