Skip to content

Support for non-default pg_port #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/capistrano/postgresql/helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/capistrano/postgresql/psql_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/capistrano/tasks/postgresql.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down