-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpsql_helpers.rb
More file actions
39 lines (33 loc) · 1.4 KB
/
psql_helpers.rb
File metadata and controls
39 lines (33 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Capistrano
module Postgresql
module PsqlHelpers
def psql(type, database, *args)
if fetch(:pg_without_sudo)
# Add the :pg_system_user to psql command since we aren't using sudo anymore
cmd = [ :psql, "-d #{database}", *args.unshift("-U #{fetch(:pg_system_user)}") ]
else
cmd = [:sudo, "-i -u #{fetch(:pg_system_user)}", :psql, *args]
end
# Allow us to execute the different sshkit commands
if type == 'test'
test *cmd
elsif type == 'capture'
capture *cmd
else
execute *cmd
end
end
def database_user_exists?
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),"-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), "-p #{fetch(:pg_port)} -tAc", %Q{"SELECT 1 FROM pg_database WHERE datname='#{fetch(:pg_database)}';" | grep -q 1}
end
end
end
end