diff --git a/lib/capistrano/postgresql/helper_methods.rb b/lib/capistrano/postgresql/helper_methods.rb index d7fa8cf..bbcce8c 100644 --- a/lib/capistrano/postgresql/helper_methods.rb +++ b/lib/capistrano/postgresql/helper_methods.rb @@ -5,7 +5,7 @@ module Postgresql module HelperMethods def extension_exists?(extension) - psql 'test', fetch(:pg_database), '-tAc', %Q{"SELECT 1 FROM pg_extension WHERE extname='#{extension}';" | grep -q 1} + psql 'test', fetch(:pg_database), "-p #{fetch(:pg_port)} -tAc", %Q{"SELECT 1 FROM pg_extension WHERE extname='#{extension}';" | grep -q 1} end def remove_extensions @@ -14,7 +14,7 @@ def remove_extensions # remove in reverse order if extension is present Array( fetch(:pg_extensions) ).reverse.each do |ext| next if [nil, false, ""].include?(ext) - psql 'execute', fetch(:pg_database), '-c', %Q{"DROP EXTENSION IF EXISTS #{ext};"} if extension_exists?(ext) + psql 'execute', fetch(:pg_database), "-p #{fetch(:pg_port)} -c", %Q{"DROP EXTENSION IF EXISTS #{ext};"} if extension_exists?(ext) end end end diff --git a/lib/capistrano/postgresql/psql_helpers.rb b/lib/capistrano/postgresql/psql_helpers.rb index 027b2a3..8c8b8cf 100644 --- a/lib/capistrano/postgresql/psql_helpers.rb +++ b/lib/capistrano/postgresql/psql_helpers.rb @@ -20,17 +20,17 @@ def psql(type, database, *args) end def database_user_exists? - psql 'test', fetch(:pg_system_db),'-tAc', %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1} + psql 'test', fetch(:pg_system_db),"-p #{fetch(:pg_port)} -tAc", %Q{"SELECT 1 FROM pg_roles WHERE rolname='#{fetch(:pg_username)}';" | grep -q 1} end def database_user_password_different? - current_password_md5 = psql 'capture', fetch(:pg_system_db),'-tAc', %Q{"select passwd from pg_shadow WHERE usename='#{fetch(:pg_username)}';"} + current_password_md5 = psql 'capture', fetch(:pg_system_db),"-p #{fetch(:pg_port)} -tAc", %Q{"select passwd from pg_shadow WHERE usename='#{fetch(:pg_username)}';"} new_password_md5 = "md5#{Digest::MD5.hexdigest("#{fetch(:pg_password)}#{fetch(:pg_username)}")}" current_password_md5 == new_password_md5 ? false : true end def database_exists? - psql 'test', fetch(:pg_system_db), '-tAc', %Q{"SELECT 1 FROM pg_database WHERE datname='#{fetch(:pg_database)}';" | grep -q 1} + psql 'test', fetch(:pg_system_db), "-p #{fetch(:pg_port)} -tAc", %Q{"SELECT 1 FROM pg_database WHERE datname='#{fetch(:pg_database)}';" | grep -q 1} end end diff --git a/lib/capistrano/tasks/postgresql.rake b/lib/capistrano/tasks/postgresql.rake index 089d37e..8bb612f 100644 --- a/lib/capistrano/tasks/postgresql.rake +++ b/lib/capistrano/tasks/postgresql.rake @@ -51,8 +51,8 @@ namespace :postgresql do execute :rm, archetype_database_yml_file if test "[ -e #{archetype_database_yml_file} ]" end on roles :db do - psql'execute', fetch(:pg_system_db), '-c', %Q{"DROP database \\"#{fetch(:pg_database)}\\";"} if database_exists? - psql 'execute', fetch(:pg_system_db),'-c', %Q{"DROP user \\"#{fetch(:pg_username)}\\";"}if database_user_exists? + psql'execute', fetch(:pg_system_db), "-p #{fetch(:pg_port)} -c", %Q{"DROP database \\"#{fetch(:pg_database)}\\";"} if database_exists? + psql 'execute', fetch(:pg_system_db),"-p #{fetch(:pg_port)} -c", %Q{"DROP user \\"#{fetch(:pg_username)}\\";"}if database_user_exists? remove_extensions end puts 'Removed database.yml from all hosts, Database, Database User, and Removed Extensions' @@ -76,7 +76,7 @@ namespace :postgresql do if Array( fetch(:pg_extensions) ).any? Array( fetch(:pg_extensions) ).each do |ext| next if [nil, false, ''].include?(ext) - psql 'execute', fetch(:pg_database), '-c', %Q{"CREATE EXTENSION IF NOT EXISTS #{ext};"}unless extension_exists?(ext) + psql 'execute', fetch(:pg_database), "-p #{fetch(:pg_port)} -c", %Q{"CREATE EXTENSION IF NOT EXISTS #{ext};"}unless extension_exists?(ext) end end end @@ -87,11 +87,11 @@ namespace :postgresql do on roles :db do unless database_user_exists? # If you use CREATE USER instead of CREATE ROLE the LOGIN right is granted automatically; otherwise you must specify it in the WITH clause of the CREATE statement. - psql 'execute', fetch(:pg_system_db), '-c', %Q{"CREATE USER \\"#{fetch(:pg_username)}\\" PASSWORD}, redact("'#{fetch(:pg_password)}'"), %Q{;"} + psql 'execute', fetch(:pg_system_db), "-p #{fetch(:pg_port)} -c", %Q{"CREATE USER \\"#{fetch(:pg_username)}\\" PASSWORD}, redact("'#{fetch(:pg_password)}'"), %Q{;"} end if database_user_password_different? # Ensure updating the password in your deploy/ENV.rb files updates the user, server side - psql 'execute', fetch(:pg_system_db), '-c', %Q{"ALTER USER \\"#{fetch(:pg_username)}\\" WITH PASSWORD}, redact("'#{fetch(:pg_password)}'"), %Q{;"} + psql 'execute', fetch(:pg_system_db), "-p #{fetch(:pg_port)} -c", %Q{"ALTER USER \\"#{fetch(:pg_username)}\\" WITH PASSWORD}, redact("'#{fetch(:pg_password)}'"), %Q{;"} end end end @@ -100,7 +100,7 @@ namespace :postgresql do task :create_database do on roles :db do unless database_exists? - psql 'execute', fetch(:pg_system_db), '-c', %Q{"CREATE DATABASE \\"#{fetch(:pg_database)}\\" OWNER \\"#{fetch(:pg_username)}\\";"} + psql 'execute', fetch(:pg_system_db), "-p #{fetch(:pg_port)} -c", %Q{"CREATE DATABASE \\"#{fetch(:pg_database)}\\" OWNER \\"#{fetch(:pg_username)}\\";"} end end end